sa4028.go raw
1 package sa4028
2
3 import (
4 "go/ast"
5
6 "honnef.co/go/tools/analysis/code"
7 "honnef.co/go/tools/analysis/lint"
8 "honnef.co/go/tools/analysis/report"
9 "honnef.co/go/tools/pattern"
10
11 "golang.org/x/tools/go/analysis"
12 "golang.org/x/tools/go/analysis/passes/inspect"
13 )
14
15 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
16 Analyzer: &analysis.Analyzer{
17 Name: "SA4028",
18 Run: run,
19 Requires: []*analysis.Analyzer{inspect.Analyzer},
20 },
21 Doc: &lint.RawDocumentation{
22 Title: `\'x % 1\' is always zero`,
23 Since: "2022.1",
24 Severity: lint.SeverityWarning,
25 MergeIf: lint.MergeIfAny, // MergeIfAny if we only flag literals, not named constants
26 },
27 })
28
29 var Analyzer = SCAnalyzer.Analyzer
30
31 var moduloOneQ = pattern.MustParse(`(BinaryExpr _ "%" (IntegerLiteral "1"))`)
32
33 func run(pass *analysis.Pass) (interface{}, error) {
34 fn := func(node ast.Node) {
35 _, ok := code.Match(pass, moduloOneQ, node)
36 if !ok {
37 return
38 }
39 report.Report(pass, node, "x % 1 is always zero")
40 }
41 code.Preorder(pass, fn, (*ast.BinaryExpr)(nil))
42 return nil, nil
43 }
44