From a6d84179fc42d797028cf01e550bf621cd950af0 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 10 May 2021 23:41:51 +0200 Subject: [PATCH] feature: the compiler can warn when you use the implicit 'result' variable (#17988) [backport:1.2] * implements #17855 (cherry picked from commit 378ee7f88857eabdd5f82ab4336f691e78a40e54) --- compiler/lineinfos.nim | 4 +++- compiler/options.nim | 2 +- compiler/semexprs.nim | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim index 78d8977438..c8a7ca17d7 100644 --- a/compiler/lineinfos.nim +++ b/compiler/lineinfos.nim @@ -55,6 +55,7 @@ type warnLockLevel = "LockLevel", warnResultShadowed = "ResultShadowed", warnInconsistentSpacing = "Spacing", warnCaseTransition = "CaseTransition", warnCycleCreated = "CycleCreated", warnObservableStores = "ObservableStores", + warnResultUsed = "ResultUsed", warnUser = "User", hintSuccess = "Success", hintSuccessX = "SuccessX", hintCC = "CC", @@ -125,6 +126,7 @@ const warnCaseTransition: "Potential object case transition, instantiate new object instead", warnCycleCreated: "$1", warnObservableStores: "observable stores to '$1'", + warnResultUsed: "used 'result' variable", warnUser: "$1", hintSuccess: "operation successful: $#", # keep in sync with `testament.isSuccess` @@ -176,7 +178,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} result[1] = result[2] - {warnProveField, warnProveIndex, warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd, diff --git a/compiler/options.nim b/compiler/options.nim index 72e92451f8..53546086c3 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -356,7 +356,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 diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index df912e6361..fbf4f0ea01 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1216,6 +1216,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: