feature: the compiler can warn when you use the implicit 'result' variable (#17988) [backport:1.2]

* implements #17855
This commit is contained in:
Andreas Rumpf 2021-05-10 23:41:51 +02:00 • committed by GitHub
commit 378ee7f888
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View file

@ -60,6 +60,7 @@ type
warnInconsistentSpacing = "Spacing", warnCaseTransition = "CaseTransition", warnInconsistentSpacing = "Spacing", warnCaseTransition = "CaseTransition",
warnCycleCreated = "CycleCreated", warnObservableStores = "ObservableStores", warnCycleCreated = "CycleCreated", warnObservableStores = "ObservableStores",
warnStrictNotNil = "StrictNotNil", warnStrictNotNil = "StrictNotNil",
warnResultUsed = "ResultUsed",
warnCannotOpen = "CannotOpen", warnCannotOpen = "CannotOpen",
warnFileChanged = "FileChanged", warnFileChanged = "FileChanged",
warnUser = "User", warnUser = "User",
@ -138,6 +139,7 @@ const
warnCycleCreated: "$1", warnCycleCreated: "$1",
warnObservableStores: "observable stores to '$1'", warnObservableStores: "observable stores to '$1'",
warnStrictNotNil: "$1", warnStrictNotNil: "$1",
warnResultUsed: "used 'result' variable",
warnCannotOpen: "cannot open: $1", warnCannotOpen: "cannot open: $1",
warnFileChanged: "file changed: $1", warnFileChanged: "file changed: $1",
warnUser: "$1", warnUser: "$1",
@ -193,7 +195,7 @@ type
TNoteKinds* = set[TNoteKind] TNoteKinds* = set[TNoteKind]
proc computeNotesVerbosity(): array[0..3, TNoteKinds] = 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[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext, hintDeclaredLoc}
result[1] = result[2] - {warnProveField, warnProveIndex, result[1] = result[2] - {warnProveField, warnProveIndex,
warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd, warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,

View file

@ -402,7 +402,7 @@ proc hasHint*(conf: ConfigRef, note: TNoteKind): bool =
note in conf.mainPackageNotes note in conf.mainPackageNotes
else: note in conf.notes 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 optWarns in conf.options and note in conf.notes
proc hcrOn*(conf: ConfigRef): bool = return optHotCodeReloading in conf.globalOptions proc hcrOn*(conf: ConfigRef): bool = return optHotCodeReloading in conf.globalOptions

View file

@ -1246,6 +1246,9 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
# not sure the symbol really ends up being used: # not sure the symbol really ends up being used:
# var len = 0 # but won't be called # var len = 0 # but won't be called
# genericThatUsesLen(x) # marked as taking a closure? # genericThatUsesLen(x) # marked as taking a closure?
if hasWarn(c.config, warnResultUsed):
message(c.config, n.info, warnResultUsed)
of skGenericParam: of skGenericParam:
onUse(n.info, s) onUse(n.info, s)
if s.typ.kind == tyStatic: if s.typ.kind == tyStatic: