diff --git a/changelog.md b/changelog.md index 82986385de..045d2d512a 100644 --- a/changelog.md +++ b/changelog.md @@ -114,6 +114,7 @@ errors. - Added `--raw` flag when generating JSON docs to not render markup. - Added `--stdinfile` flag to name of the file used when running program from stdin (defaults to `stdinfile.nim`) +- Added `--styleCheck:warning` flag to treat style check violations as warnings. ## Documentation changes diff --git a/compiler/commands.nim b/compiler/commands.nim index 27d62fb3e7..43256bce0c 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -118,7 +118,7 @@ const errInvalidCmdLineOption = "invalid command line option: '$1'" errOnOrOffExpectedButXFound = "'on' or 'off' expected, but '$1' found" errOnOffOrListExpectedButXFound = "'on', 'off' or 'list' expected, but '$1' found" - errOffHintsError = "'off', 'hint', 'error' or 'usages' expected, but '$1' found" + errOffHintsError = "'off', 'hint', 'warning', 'error' or 'usages' expected, but '$1' found" proc invalidCmdLineOption(conf: ConfigRef; pass: TCmdLinePass, switch: string, info: TLineInfo) = if switch == " ": localError(conf, info, errInvalidCmdLineOption % "-") @@ -1131,9 +1131,10 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; defineSymbol(conf.symbols, "nimSeqsV2") of "stylecheck": case arg.normalize - of "off": conf.globalOptions = conf.globalOptions - {optStyleHint, optStyleError} - of "hint": conf.globalOptions = conf.globalOptions + {optStyleHint} - {optStyleError} - of "error": conf.globalOptions = conf.globalOptions + {optStyleError} + of "off": conf.globalOptions = conf.globalOptions - {optStyleHint, optStyleError, optStyleWarning} + of "hint": conf.globalOptions = conf.globalOptions + {optStyleHint} - {optStyleError, optStyleWarning} + of "warning": conf.globalOptions = conf.globalOptions + {optStyleWarning} - {optStyleHint, optStyleError} + of "error": conf.globalOptions = conf.globalOptions + {optStyleError} - {optStyleHint, optStyleWarning} of "usages": conf.globalOptions.incl optStyleUsages else: localError(conf, info, errOffHintsError % arg) of "showallmismatches": diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 769987ba8f..9ebec89be5 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -923,7 +923,7 @@ proc getSymbol(L: var Lexer, tok: var Token) = tok.tokType = tkSymbol else: tok.tokType = TokType(tok.ident.id + ord(tkSymbol)) - if suspicious and {optStyleHint, optStyleError} * L.config.globalOptions != {}: + if suspicious and {optStyleHint, optStyleError, optStyleWarning} * L.config.globalOptions != {}: lintReport(L.config, getLineInfo(L), tok.ident.s.normalize, tok.ident.s) L.bufpos = pos diff --git a/compiler/linter.nim b/compiler/linter.nim index b8358f5de9..490e59d0cb 100644 --- a/compiler/linter.nim +++ b/compiler/linter.nim @@ -95,7 +95,7 @@ proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) = template styleCheckDef*(ctx: PContext; info: TLineInfo; sym: PSym; k: TSymKind) = ## Check symbol definitions adhere to NEP1 style rules. if optStyleCheck in ctx.config.options and # ignore if styleChecks are off - {optStyleHint, optStyleError} * ctx.config.globalOptions != {} and # check only if hint/error is enabled + {optStyleHint, optStyleError, optStyleWarning} * ctx.config.globalOptions != {} and # check only if hint/error/warning is enabled hintName in ctx.config.notes and # ignore if name checks are not requested ctx.config.belongsToProjectPackageMaybeNil(getModule(ctx.graph, info.fileIndex)) and # ignore foreign packages optStyleUsages notin ctx.config.globalOptions and # ignore if requested to only check name usage @@ -136,7 +136,7 @@ proc styleCheckUseImpl(conf: ConfigRef; info: TLineInfo; s: PSym) = template styleCheckUse*(ctx: PContext; info: TLineInfo; sym: PSym) = ## Check symbol uses match their definition's style. - if {optStyleHint, optStyleError} * ctx.config.globalOptions != {} and # ignore if styleChecks are off + if {optStyleHint, optStyleError, optStyleWarning} * ctx.config.globalOptions != {} and # ignore if styleChecks are off hintName in ctx.config.notes and # ignore if name checks are not requested ctx.config.belongsToProjectPackageMaybeNil(getModule(ctx.graph, info.fileIndex)) and # ignore foreign packages sym.kind != skTemp and # ignore temporary variables created by the compiler @@ -152,7 +152,7 @@ proc checkPragmaUseImpl(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragm template checkPragmaUse*(ctx: PContext; info: TLineInfo; w: TSpecialWord; pragmaName: string, sym: PSym) = ## Check builtin pragma uses match their definition's style. ## Note: This only applies to builtin pragmas, not user pragmas. - if {optStyleHint, optStyleError} * ctx.config.globalOptions != {} and # ignore if styleChecks are off + if {optStyleHint, optStyleError, optStyleWarning} * ctx.config.globalOptions != {} and # ignore if styleChecks are off hintName in ctx.config.notes and # ignore if name checks are not requested ctx.config.belongsToProjectPackageMaybeNil(getModule(ctx.graph, info.fileIndex)): # ignore foreign packages checkPragmaUseImpl(ctx.config, info, w, pragmaName) diff --git a/compiler/msgs.nim b/compiler/msgs.nim index c49ca8c9b1..7bd70d8afb 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -648,7 +648,9 @@ template internalAssert*(conf: ConfigRef, e: bool) = template lintReport*(conf: ConfigRef; info: TLineInfo, beau, got: string, extraMsg = "") = let m = "'$1' should be: '$2'$3" % [got, beau, extraMsg] - let msg = if optStyleError in conf.globalOptions: errGenerated else: hintName + let msg = if optStyleError in conf.globalOptions: errGenerated + elif optStyleWarning in conf.globalOptions: warnUser + else: hintName liMessage(conf, info, msg, m, doNothing, instLoc()) proc quotedFilename*(conf: ConfigRef; fi: FileIndex): Rope = diff --git a/compiler/options.nim b/compiler/options.nim index 5dc1942157..f25b52051b 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -68,6 +68,7 @@ type # please make sure we have under 32 options optUseNimcache, # save artifacts (including binary) in $nimcache optStyleHint, # check that the names adhere to NEP-1 optStyleError, # enforce that the names adhere to NEP-1 + optStyleWarning, # emit style checks as warnings optStyleUsages, # only enforce consistent **usages** of the symbol optSkipSystemConfigFile, # skip the system's cfg/nims config file optSkipProjConfigFile, # skip the project's cfg/nims config file diff --git a/tests/tools/tlinter_warnings.nim b/tests/tools/tlinter_warnings.nim new file mode 100644 index 0000000000..b1297ab02b --- /dev/null +++ b/tests/tools/tlinter_warnings.nim @@ -0,0 +1,40 @@ +discard """ + cmd: '''nim c --styleCheck:warning --hints:off $file''' + nimout: ''' +tlinter_warnings.nim(25, 1) Warning: 'tyPE' should be: 'type' [User] +tlinter_warnings.nim(21, 14) Warning: 'nosideeffect' should be: 'noSideEffect' [User] +tlinter_warnings.nim(21, 28) Warning: 'myown' should be: 'myOwn' [template declared in tlinter_warnings.nim(19, 9)] [User] +tlinter_warnings.nim(21, 35) Warning: 'inLine' should be: 'inline' [User] +tlinter_warnings.nim(23, 1) Warning: 'foO' should be: 'foo' [proc declared in tlinter_warnings.nim(21, 6)] [User] +tlinter_warnings.nim(27, 14) Warning: 'Foo_bar' should be: 'FooBar' [type declared in tlinter_warnings.nim(25, 6)] [User] +tlinter_warnings.nim(29, 6) Warning: 'someVAR' should be: 'someVar' [var declared in tlinter_warnings.nim(27, 5)] [User] +tlinter_warnings.nim(32, 7) Warning: 'i_fool' should be: 'iFool' [User] +tlinter_warnings.nim(39, 5) Warning: 'meh_field' should be: 'mehField' [User] +''' + action: "compile" +""" + + + +{.pragma: myOwn.} + +proc foo() {.nosideeffect, myown, inLine.} = debugEcho "hi" + +foO() + +tyPE FooBar = string + +var someVar: Foo_bar = "a" + +echo someVAR + +proc main = + var i_fool = 34 + echo i_fool + +main() + +type + Foo = object + meh_field: int +