Skip to content

Commit 95e71b9

Browse files
adonovangopherbot
authored andcommitted
go/analysis/passes/assign: use refactor.DeleteStmt
Change-Id: I401b48eb81287a749e0e02da7456386e44e99a9a Reviewed-on: https://go-review.googlesource.com/c/tools/+/711322 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Peter Weinberger <pjw@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 0d2befa commit 95e71b9

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

go/analysis/passes/assign/assign.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
2121
"golang.org/x/tools/go/ast/inspector"
2222
"golang.org/x/tools/internal/astutil"
23+
"golang.org/x/tools/internal/refactor"
2324
)
2425

2526
//go:embed doc.go
@@ -34,19 +35,19 @@ var Analyzer = &analysis.Analyzer{
3435
}
3536

3637
func run(pass *analysis.Pass) (any, error) {
37-
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
38+
var (
39+
inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
40+
info = pass.TypesInfo
41+
)
3842

39-
nodeFilter := []ast.Node{
40-
(*ast.AssignStmt)(nil),
41-
}
42-
inspect.Preorder(nodeFilter, func(n ast.Node) {
43-
stmt := n.(*ast.AssignStmt)
43+
for curAssign := range inspect.Root().Preorder((*ast.AssignStmt)(nil)) {
44+
stmt := curAssign.Node().(*ast.AssignStmt)
4445
if stmt.Tok != token.ASSIGN {
45-
return // ignore :=
46+
continue // ignore :=
4647
}
4748
if len(stmt.Lhs) != len(stmt.Rhs) {
4849
// If LHS and RHS have different cardinality, they can't be the same.
49-
return
50+
continue
5051
}
5152

5253
// Delete redundant LHS, RHS pairs, taking care
@@ -61,9 +62,9 @@ func run(pass *analysis.Pass) (any, error) {
6162
isSelfAssign := false
6263
var le string
6364

64-
if !analysisutil.HasSideEffects(pass.TypesInfo, lhs) &&
65-
!analysisutil.HasSideEffects(pass.TypesInfo, rhs) &&
66-
!isMapIndex(pass.TypesInfo, lhs) &&
65+
if !analysisutil.HasSideEffects(info, lhs) &&
66+
!analysisutil.HasSideEffects(info, rhs) &&
67+
!isMapIndex(info, lhs) &&
6768
reflect.TypeOf(lhs) == reflect.TypeOf(rhs) { // short-circuit the heavy-weight gofmt check
6869

6970
le = astutil.Format(pass.Fset, lhs)
@@ -109,13 +110,14 @@ func run(pass *analysis.Pass) (any, error) {
109110
}
110111

111112
if len(exprs) == 0 {
112-
return
113+
continue
113114
}
114115

115116
if len(exprs) == len(stmt.Lhs) {
116117
// If every part of the statement is a self-assignment,
117118
// remove the whole statement.
118-
edits = []analysis.TextEdit{{Pos: stmt.Pos(), End: stmt.End()}}
119+
tokFile := pass.Fset.File(stmt.Pos())
120+
edits = refactor.DeleteStmt(tokFile, curAssign)
119121
}
120122

121123
pass.Report(analysis.Diagnostic{
@@ -126,7 +128,7 @@ func run(pass *analysis.Pass) (any, error) {
126128
TextEdits: edits,
127129
}},
128130
})
129-
})
131+
}
130132

131133
return nil, nil
132134
}

0 commit comments

Comments
 (0)