From 4ce3d87c7ee4fe2d323711623c80d406621f481c Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 10 Aug 2026 13:38:49 +0800 Subject: [PATCH] =?UTF-8?q?fixes=20#26062;=20ResultUsed=20warning=20behave?= =?UTF-8?q?s=20inconsistently=20with=20manual=20c=E2=80=A6=20(#26087)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …haracterization with--warning:ResultUsed:on fixes #26062 > A return statement with no expression is shorthand for return result. > ResultUsed: Warn about the usage of the built-in result variable. > A procedure that does not have any return statement and does not use the special result variable returns the value of its last expression. (cherry picked from commit 0ec8682abe296165453eb83bb160f71f867c3496) --- compiler/semexprs.nim | 8 +++++++- tests/msgs/tresultused.nim | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/msgs/tresultused.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 683bde602a..6814f522e8 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1409,7 +1409,11 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode = # not sure the symbol really ends up being used: # var len = 0 # but won't be called # genericThatUsesLen(x) # marked as taking a closure? - if hasWarn(c.config, warnResultUsed): + # Lowered returns use resolved symbol nodes internally; warn only for + # source-level references to the implicit result variable. + if s.kind == skResult and + (n.kind != nkSym or nfFromTemplate in n.flags) and + hasWarn(c.config, warnResultUsed): message(c.config, n.info, warnResultUsed) of skGenericParam: @@ -2089,6 +2093,8 @@ proc semReturn(c: PContext, n: PNode): PNode = # optimize away ``result = result``: if result[0][1].kind == nkSym and result[0][1].sym == c.p.resultSym: result[0] = c.graph.emptyNode + elif c.p.resultSym != nil and hasWarn(c.config, warnResultUsed): + message(c.config, n.info, warnResultUsed) else: localError(c.config, n.info, "'return' not allowed here") diff --git a/tests/msgs/tresultused.nim b/tests/msgs/tresultused.nim new file mode 100644 index 0000000000..b40a7f33d3 --- /dev/null +++ b/tests/msgs/tresultused.nim @@ -0,0 +1,42 @@ +discard """ + action: compile + cmd: "nim check --hints:off --warning:ResultUsed:on $file" + nimout: ''' +tresultused.nim(34, 3) Warning: used 'result' variable [ResultUsed] +tresultused.nim(37, 3) Warning: used 'result' variable [ResultUsed] +''' +""" + +let value = 0 +discard value + +for item in 0 .. 0: + discard item + +proc usesLocal() = + var local = 0 + discard local + +block: + let result = 0 + discard result + +proc voidReturn() = + return + +proc implicitReturn(): int = + 0 + +proc explicitReturn(): int = + return 0 + +proc usesResult(): int = + result = 0 + +proc usesBareReturn(): int = + return + +doAssert implicitReturn() == 0 +doAssert explicitReturn() == 0 +doAssert usesResult() == 0 +doAssert usesBareReturn() == 0