Skip to content

Commit

Permalink
feature: the compiler can warn when you use the implicit 'result' var…
Browse files Browse the repository at this point in the history
…iable (nim-lang#17988) [backport:1.2]

* implements nim-lang#17855
  • Loading branch information
Araq authored May 10, 2021
1 parent a9ae5fe commit 378ee7f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion compiler/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type
warnInconsistentSpacing = "Spacing", warnCaseTransition = "CaseTransition",
warnCycleCreated = "CycleCreated", warnObservableStores = "ObservableStores",
warnStrictNotNil = "StrictNotNil",
warnResultUsed = "ResultUsed",
warnCannotOpen = "CannotOpen",
warnFileChanged = "FileChanged",
warnUser = "User",
Expand Down Expand Up @@ -138,6 +139,7 @@ const
warnCycleCreated: "$1",
warnObservableStores: "observable stores to '$1'",
warnStrictNotNil: "$1",
warnResultUsed: "used 'result' variable",
warnCannotOpen: "cannot open: $1",
warnFileChanged: "file changed: $1",
warnUser: "$1",
Expand Down Expand Up @@ -193,7 +195,7 @@ type
TNoteKinds* = set[TNoteKind]

proc computeNotesVerbosity(): array[0..3, TNoteKinds] =
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores}
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores, warnResultUsed}
result[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext, hintDeclaredLoc}
result[1] = result[2] - {warnProveField, warnProveIndex,
warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,
Expand Down
2 changes: 1 addition & 1 deletion compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ proc hasHint*(conf: ConfigRef, note: TNoteKind): bool =
note in conf.mainPackageNotes
else: note in conf.notes

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

proc hcrOn*(conf: ConfigRef): bool = return optHotCodeReloading in conf.globalOptions
Expand Down
3 changes: 3 additions & 0 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,9 @@ 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):
message(c.config, n.info, warnResultUsed)

of skGenericParam:
onUse(n.info, s)
if s.typ.kind == tyStatic:
Expand Down

0 comments on commit 378ee7f

Please sign in to comment.