aboutsummaryrefslogtreecommitdiffstats
path: root/tools/syz-linter
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-07-05 11:42:38 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-07-06 08:23:03 +0200
commit3e0ce485a16298c2b5ffd75f482e3845ac21c66c (patch)
treec8877f362d6c5cb6f2ce2fff974825f2b3ce79c1 /tools/syz-linter
parented2ced4c694aef828d8dfbaa9d8a459ac2c72043 (diff)
tools/syz-linter: check variable declarations
Warn about: var x int = foo In most cases this can be expressed shorter as: var x int x := foo x := int(foo) Update #1876
Diffstat (limited to 'tools/syz-linter')
-rw-r--r--tools/syz-linter/linter.go20
-rw-r--r--tools/syz-linter/testdata/src/lintertest/lintertest.go9
2 files changed, 29 insertions, 0 deletions
diff --git a/tools/syz-linter/linter.go b/tools/syz-linter/linter.go
index 44fb91fbc..327e8b977 100644
--- a/tools/syz-linter/linter.go
+++ b/tools/syz-linter/linter.go
@@ -60,6 +60,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
checkFuncArgs(pass, n)
case *ast.CallExpr:
checkLogErrorFormat(pass, n)
+ case *ast.GenDecl:
+ checkVarDecl(pass, n)
}
return true
})
@@ -216,3 +218,21 @@ func checkLogErrorFormat(pass *analysis.Pass, n *ast.CallExpr) {
}
var publicIdentifier = regexp.MustCompile(`^[A-Z][[:alnum:]]+(\.[[:alnum:]]+)+ `)
+
+// checkVarDecl warns about unnecessary long variable declarations "var x type = foo".
+func checkVarDecl(pass *analysis.Pass, n *ast.GenDecl) {
+ if n.Tok != token.VAR {
+ return
+ }
+ for _, s := range n.Specs {
+ spec, ok := s.(*ast.ValueSpec)
+ if !ok || spec.Type == nil || len(spec.Values) == 0 || spec.Names[0].Name == "_" {
+ continue
+ }
+ pass.Report(analysis.Diagnostic{
+ Pos: n.Pos(),
+ Message: "Don't use both var, type and value in variable declarations\n" +
+ "Use either \"var x type\" or \"x := val\" or \"x := type(val)\"",
+ })
+ }
+}
diff --git a/tools/syz-linter/testdata/src/lintertest/lintertest.go b/tools/syz-linter/testdata/src/lintertest/lintertest.go
index 7b8722693..9a8c965b8 100644
--- a/tools/syz-linter/testdata/src/lintertest/lintertest.go
+++ b/tools/syz-linter/testdata/src/lintertest/lintertest.go
@@ -66,3 +66,12 @@ func logErrorMessages() {
log.Print("no new lines\n") // want "Don't use \\\\n at the end of log/error messages"
log.Print("") // want "Don't use empty log/error messages"
}
+
+func varDecls() {
+ var a int
+ b := 0
+ c := int64(0)
+ var _ int = 0
+ var d int = 0 // want "Don't use both var, type and value in variable declarations"
+ _, _, _, _ = a, b, c, d
+}