add astmsgs; add declared in msg for usage lint errors (#17961)
* add astmsgs; add `declared in` msg for usage lint errors * fix test * fix tests/tools/tlinter.nim * std prefix
This commit is contained in:
parent
d4850b524f
commit
e224865b3b
9 changed files with 43 additions and 39 deletions
|
|
@ -464,6 +464,8 @@ const
|
||||||
# consider renaming as `tyAbstractVarRange`
|
# consider renaming as `tyAbstractVarRange`
|
||||||
abstractVarRange* = {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
|
abstractVarRange* = {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
|
||||||
tyTypeDesc, tyAlias, tyInferred, tySink, tyOwned}
|
tyTypeDesc, tyAlias, tyInferred, tySink, tyOwned}
|
||||||
|
abstractInst* = {tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc, tyAlias,
|
||||||
|
tyInferred, tySink, tyOwned} # xxx what about tyStatic?
|
||||||
|
|
||||||
type
|
type
|
||||||
TTypeKinds* = set[TTypeKind]
|
TTypeKinds* = set[TTypeKind]
|
||||||
|
|
|
||||||
23
compiler/astmsgs.nim
Normal file
23
compiler/astmsgs.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# this module avoids ast depending on msgs or vice versa
|
||||||
|
import std/strutils
|
||||||
|
import options, ast, msgs
|
||||||
|
|
||||||
|
proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
|
||||||
|
result.add " [$1 declared in $2]" % [sym.kind.toHumanStr, toFileLineCol(conf, sym.info)]
|
||||||
|
|
||||||
|
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
|
||||||
|
if optDeclaredLocs in conf.globalOptions and sym != nil:
|
||||||
|
addDeclaredLoc(result, conf, sym)
|
||||||
|
|
||||||
|
proc addDeclaredLoc*(result: var string, conf: ConfigRef; typ: PType) =
|
||||||
|
# xxx figure out how to resolve `tyGenericParam`, e.g. for
|
||||||
|
# proc fn[T](a: T, b: T) = discard
|
||||||
|
# fn(1.1, "a")
|
||||||
|
let typ = typ.skipTypes(abstractInst + {tyStatic} - {tyRange})
|
||||||
|
result.add " [$1" % typ.kind.toHumanStr
|
||||||
|
if typ.sym != nil:
|
||||||
|
result.add " declared in " & toFileLineCol(conf, typ.sym.info)
|
||||||
|
result.add "]"
|
||||||
|
|
||||||
|
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
|
||||||
|
if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
|
||||||
|
|
@ -9,9 +9,10 @@
|
||||||
|
|
||||||
## This module implements the style checker.
|
## This module implements the style checker.
|
||||||
|
|
||||||
import strutils
|
import std/strutils
|
||||||
|
from std/sugar import dup
|
||||||
|
|
||||||
import options, ast, msgs, idents, lineinfos, wordrecg
|
import options, ast, msgs, idents, lineinfos, wordrecg, astmsgs
|
||||||
|
|
||||||
const
|
const
|
||||||
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
||||||
|
|
@ -129,7 +130,7 @@ proc styleCheckUse*(conf: ConfigRef; info: TLineInfo; s: PSym) =
|
||||||
if badName.len > 0:
|
if badName.len > 0:
|
||||||
# special rules for historical reasons
|
# special rules for historical reasons
|
||||||
let forceHint = badName == "nnkArgList" and newName == "nnkArglist" or badName == "nnkArglist" and newName == "nnkArgList"
|
let forceHint = badName == "nnkArgList" and newName == "nnkArglist" or badName == "nnkArglist" and newName == "nnkArgList"
|
||||||
lintReport(conf, info, newName, badName, forceHint = forceHint)
|
lintReport(conf, info, newName, badName, forceHint = forceHint, extraMsg = "".dup(addDeclaredLoc(conf, s)))
|
||||||
|
|
||||||
proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragmaName: string) =
|
proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragmaName: string) =
|
||||||
let wanted = $w
|
let wanted = $w
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
import
|
import
|
||||||
intsets, ast, astalgo, idents, semdata, types, msgs, options,
|
intsets, ast, astalgo, idents, semdata, types, msgs, options,
|
||||||
renderer, nimfix/prettybase, lineinfos, strutils,
|
renderer, nimfix/prettybase, lineinfos, strutils,
|
||||||
modulegraphs
|
modulegraphs, astmsgs
|
||||||
|
|
||||||
proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope)
|
proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -616,8 +616,8 @@ template internalAssert*(conf: ConfigRef, e: bool) =
|
||||||
let arg = info2.toFileLineCol
|
let arg = info2.toFileLineCol
|
||||||
internalErrorImpl(conf, unknownLineInfo, arg, info2)
|
internalErrorImpl(conf, unknownLineInfo, arg, info2)
|
||||||
|
|
||||||
template lintReport*(conf: ConfigRef; info: TLineInfo, beau, got: string, forceHint = false) =
|
template lintReport*(conf: ConfigRef; info: TLineInfo, beau, got: string, forceHint = false, extraMsg = "") =
|
||||||
let m = "'$1' should be: '$2'" % [got, beau]
|
let m = "'$1' should be: '$2'$3" % [got, beau, extraMsg]
|
||||||
let msg = if optStyleError in conf.globalOptions and not forceHint: errGenerated else: hintName
|
let msg = if optStyleError in conf.globalOptions and not forceHint: errGenerated else: hintName
|
||||||
liMessage(conf, info, msg, m, doNothing, instLoc())
|
liMessage(conf, info, msg, m, doNothing, instLoc())
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ import
|
||||||
intsets, transf, vmdef, vm, aliases, cgmeth, lambdalifting,
|
intsets, transf, vmdef, vm, aliases, cgmeth, lambdalifting,
|
||||||
evaltempl, patterns, parampatterns, sempass2, linter, semmacrosanity,
|
evaltempl, patterns, parampatterns, sempass2, linter, semmacrosanity,
|
||||||
lowerings, plugins/active, lineinfos, strtabs, int128,
|
lowerings, plugins/active, lineinfos, strtabs, int128,
|
||||||
isolation_check, typeallowed, modulegraphs, enumtostr, concepts
|
isolation_check, typeallowed, modulegraphs, enumtostr, concepts, astmsgs
|
||||||
|
|
||||||
when defined(nimfix):
|
when defined(nimfix):
|
||||||
import nimfix/prettybase
|
import nimfix/prettybase
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
import
|
import
|
||||||
intsets, ast, astalgo, trees, msgs, strutils, platform, renderer, options,
|
intsets, ast, astalgo, trees, msgs, strutils, platform, renderer, options,
|
||||||
lineinfos, int128, modulegraphs
|
lineinfos, int128, modulegraphs, astmsgs
|
||||||
|
|
||||||
type
|
type
|
||||||
TPreferedDesc* = enum
|
TPreferedDesc* = enum
|
||||||
|
|
@ -65,9 +65,6 @@ const
|
||||||
tyAlias, tyInferred, tySink, tyLent, tyOwned}
|
tyAlias, tyInferred, tySink, tyLent, tyOwned}
|
||||||
abstractRange* = {tyGenericInst, tyRange, tyDistinct, tyOrdinal, tyTypeDesc,
|
abstractRange* = {tyGenericInst, tyRange, tyDistinct, tyOrdinal, tyTypeDesc,
|
||||||
tyAlias, tyInferred, tySink, tyOwned}
|
tyAlias, tyInferred, tySink, tyOwned}
|
||||||
# see also ast.abstractVarRange
|
|
||||||
abstractInst* = {tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc, tyAlias,
|
|
||||||
tyInferred, tySink, tyOwned} # xxx what about tyStatic?
|
|
||||||
abstractInstOwned* = abstractInst + {tyOwned}
|
abstractInstOwned* = abstractInst + {tyOwned}
|
||||||
skipPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyTypeDesc, tyAlias,
|
skipPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyTypeDesc, tyAlias,
|
||||||
tyInferred, tySink, tyLent, tyOwned}
|
tyInferred, tySink, tyLent, tyOwned}
|
||||||
|
|
@ -123,26 +120,6 @@ proc isIntLit*(t: PType): bool {.inline.} =
|
||||||
proc isFloatLit*(t: PType): bool {.inline.} =
|
proc isFloatLit*(t: PType): bool {.inline.} =
|
||||||
result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
|
result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
|
||||||
|
|
||||||
proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
|
|
||||||
result.add " [$1 declared in $2]" % [sym.kind.toHumanStr, toFileLineCol(conf, sym.info)]
|
|
||||||
|
|
||||||
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
|
|
||||||
if optDeclaredLocs in conf.globalOptions and sym != nil:
|
|
||||||
addDeclaredLoc(result, conf, sym)
|
|
||||||
|
|
||||||
proc addDeclaredLoc*(result: var string, conf: ConfigRef; typ: PType) =
|
|
||||||
# xxx figure out how to resolve `tyGenericParam`, e.g. for
|
|
||||||
# proc fn[T](a: T, b: T) = discard
|
|
||||||
# fn(1.1, "a")
|
|
||||||
let typ = typ.skipTypes(abstractInst + {tyStatic} - {tyRange})
|
|
||||||
result.add " [$1" % typ.kind.toHumanStr
|
|
||||||
if typ.sym != nil:
|
|
||||||
result.add " declared in " & toFileLineCol(conf, typ.sym.info)
|
|
||||||
result.add "]"
|
|
||||||
|
|
||||||
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
|
|
||||||
if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
|
|
||||||
|
|
||||||
proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
|
proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
|
||||||
result.add typeToString(typ, prefer)
|
result.add typeToString(typ, prefer)
|
||||||
if getDeclarationPath: result.addDeclaredLoc(conf, typ.sym)
|
if getDeclarationPath: result.addDeclaredLoc(conf, typ.sym)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
discard """
|
discard """
|
||||||
cmd: "nim c --styleCheck:error --styleCheck:usages $file"
|
action: reject
|
||||||
errormsg: "'BAD_STYLE' should be: 'BADSTYLE'"
|
nimout: '''tusages.nim(22, 5) Error: 'BAD_STYLE' should be: 'BADSTYLE' [proc declared in tusages.nim(11, 6)]'''
|
||||||
line: 20
|
cmd: "nim $target --styleCheck:error --styleCheck:usages $file"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# xxx pending bug #17960, use: matrix: "--styleCheck:error --styleCheck:usages"
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
proc BADSTYLE(c: char) = discard
|
proc BADSTYLE(c: char) = discard
|
||||||
|
|
@ -20,4 +22,3 @@ proc toSnakeCase(s: string): string =
|
||||||
BAD_STYLE(s[i])
|
BAD_STYLE(s[i])
|
||||||
|
|
||||||
echo toSnakeCase("fooBarBaz Yes")
|
echo toSnakeCase("fooBarBaz Yes")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ discard """
|
||||||
cmd: '''nim c --styleCheck:hint $file'''
|
cmd: '''nim c --styleCheck:hint $file'''
|
||||||
nimout: '''
|
nimout: '''
|
||||||
tlinter.nim(21, 14) Hint: 'nosideeffect' should be: 'noSideEffect' [Name]
|
tlinter.nim(21, 14) Hint: 'nosideeffect' should be: 'noSideEffect' [Name]
|
||||||
tlinter.nim(21, 28) Hint: 'myown' should be: 'myOwn' [Name]
|
tlinter.nim(21, 28) Hint: 'myown' should be: 'myOwn' [template declared in tlinter.nim(19, 9)] [Name]
|
||||||
tlinter.nim(21, 35) Hint: 'inLine' should be: 'inline' [Name]
|
tlinter.nim(21, 35) Hint: 'inLine' should be: 'inline' [Name]
|
||||||
tlinter.nim(25, 1) Hint: 'tyPE' should be: 'type' [Name]
|
tlinter.nim(25, 1) Hint: 'tyPE' should be: 'type' [Name]
|
||||||
tlinter.nim(23, 1) Hint: 'foO' should be: 'foo' [Name]
|
tlinter.nim(23, 1) Hint: 'foO' should be: 'foo' [proc declared in tlinter.nim(21, 6)] [Name]
|
||||||
tlinter.nim(27, 14) Hint: 'Foo_bar' should be: 'FooBar' [Name]
|
tlinter.nim(27, 14) Hint: 'Foo_bar' should be: 'FooBar' [type declared in tlinter.nim(25, 6)] [Name]
|
||||||
tlinter.nim(29, 6) Hint: 'someVAR' should be: 'someVar' [Name]
|
tlinter.nim(29, 6) Hint: 'someVAR' should be: 'someVar' [var declared in tlinter.nim(27, 5)] [Name]
|
||||||
tlinter.nim(32, 7) Hint: 'i_fool' should be: 'iFool' [Name]
|
tlinter.nim(32, 7) Hint: 'i_fool' should be: 'iFool' [Name]
|
||||||
tlinter.nim(39, 5) Hint: 'meh_field' should be: 'mehField' [Name]
|
tlinter.nim(39, 5) Hint: 'meh_field' should be: 'mehField' [Name]
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue