1
2
3
4
5
6 package assign
7
8
9
10
11 import (
12 "fmt"
13 "go/ast"
14 "go/token"
15 "reflect"
16
17 "golang.org/x/tools/go/analysis"
18 "golang.org/x/tools/go/analysis/passes/inspect"
19 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
20 "golang.org/x/tools/go/ast/inspector"
21 )
22
23 const Doc = `check for useless assignments
24
25 This checker reports assignments of the form x = x or a[i] = a[i].
26 These are almost always useless, and even when they aren't they are
27 usually a mistake.`
28
29 var Analyzer = &analysis.Analyzer{
30 Name: "assign",
31 Doc: Doc,
32 Requires: []*analysis.Analyzer{inspect.Analyzer},
33 Run: run,
34 }
35
36 func run(pass *analysis.Pass) (interface{}, error) {
37 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
38
39 nodeFilter := []ast.Node{
40 (*ast.AssignStmt)(nil),
41 }
42 inspect.Preorder(nodeFilter, func(n ast.Node) {
43 stmt := n.(*ast.AssignStmt)
44 if stmt.Tok != token.ASSIGN {
45 return
46 }
47 if len(stmt.Lhs) != len(stmt.Rhs) {
48
49 return
50 }
51 for i, lhs := range stmt.Lhs {
52 rhs := stmt.Rhs[i]
53 if analysisutil.HasSideEffects(pass.TypesInfo, lhs) ||
54 analysisutil.HasSideEffects(pass.TypesInfo, rhs) {
55 continue
56 }
57 if reflect.TypeOf(lhs) != reflect.TypeOf(rhs) {
58 continue
59 }
60 le := analysisutil.Format(pass.Fset, lhs)
61 re := analysisutil.Format(pass.Fset, rhs)
62 if le == re {
63 pass.Report(analysis.Diagnostic{
64 Pos: stmt.Pos(), Message: fmt.Sprintf("self-assignment of %s to %s", re, le),
65 SuggestedFixes: []analysis.SuggestedFix{
66 {Message: "Remove", TextEdits: []analysis.TextEdit{
67 {Pos: stmt.Pos(), End: stmt.End(), NewText: []byte{}},
68 }},
69 },
70 })
71 }
72 }
73 })
74
75 return nil, nil
76 }
77
View as plain text