Skip to content

Commit 378ee7f

Browse files
authored
feature: the compiler can warn when you use the implicit 'result' variable (nim-lang#17988) [backport:1.2]
* implements nim-lang#17855
1 parent a9ae5fe commit 378ee7f

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

compiler/lineinfos.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type
6060
warnInconsistentSpacing = "Spacing", warnCaseTransition = "CaseTransition",
6161
warnCycleCreated = "CycleCreated", warnObservableStores = "ObservableStores",
6262
warnStrictNotNil = "StrictNotNil",
63+
warnResultUsed = "ResultUsed",
6364
warnCannotOpen = "CannotOpen",
6465
warnFileChanged = "FileChanged",
6566
warnUser = "User",
@@ -138,6 +139,7 @@ const
138139
warnCycleCreated: "$1",
139140
warnObservableStores: "observable stores to '$1'",
140141
warnStrictNotNil: "$1",
142+
warnResultUsed: "used 'result' variable",
141143
warnCannotOpen: "cannot open: $1",
142144
warnFileChanged: "file changed: $1",
143145
warnUser: "$1",
@@ -193,7 +195,7 @@ type
193195
TNoteKinds* = set[TNoteKind]
194196

195197
proc computeNotesVerbosity(): array[0..3, TNoteKinds] =
196-
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores}
198+
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores, warnResultUsed}
197199
result[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext, hintDeclaredLoc}
198200
result[1] = result[2] - {warnProveField, warnProveIndex,
199201
warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,

compiler/options.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ proc hasHint*(conf: ConfigRef, note: TNoteKind): bool =
402402
note in conf.mainPackageNotes
403403
else: note in conf.notes
404404

405-
proc hasWarn*(conf: ConfigRef, note: TNoteKind): bool =
405+
proc hasWarn*(conf: ConfigRef, note: TNoteKind): bool {.inline.} =
406406
optWarns in conf.options and note in conf.notes
407407

408408
proc hcrOn*(conf: ConfigRef): bool = return optHotCodeReloading in conf.globalOptions

compiler/semexprs.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,9 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
12461246
# not sure the symbol really ends up being used:
12471247
# var len = 0 # but won't be called
12481248
# genericThatUsesLen(x) # marked as taking a closure?
1249+
if hasWarn(c.config, warnResultUsed):
1250+
message(c.config, n.info, warnResultUsed)
1251+
12491252
of skGenericParam:
12501253
onUse(n.info, s)
12511254
if s.typ.kind == tyStatic:

0 commit comments

Comments
 (0)