diff --git a/copyloopvar.go b/copyloopvar.go index dfb0e7d..4c885fa 100644 --- a/copyloopvar.go +++ b/copyloopvar.go @@ -52,7 +52,9 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) { } var value *ast.Ident if rangeStmt.Value != nil { - value = rangeStmt.Value.(*ast.Ident) + if value, ok = rangeStmt.Value.(*ast.Ident); !ok { + return + } } for _, stmt := range rangeStmt.Body.List { assignStmt, ok := stmt.(*ast.AssignStmt) diff --git a/testdata/src/basic/main.go b/testdata/src/basic/main.go index ceaa953..f76bfb1 100644 --- a/testdata/src/basic/main.go +++ b/testdata/src/basic/main.go @@ -20,4 +20,11 @@ func main() { c, d := 1, j // want `The copy of the 'for' variable "j" can be deleted \(Go 1\.22\+\)` _, _, _, _, _, _, _, _ = i, _i, j, _j, a, b, c, d } + + var t struct { + Bool bool + } + for _, t.Bool = range []bool{true, false} { + _ = t + } } diff --git a/testdata/src/ignorealias/main.go b/testdata/src/ignorealias/main.go index 3909d07..7855981 100644 --- a/testdata/src/ignorealias/main.go +++ b/testdata/src/ignorealias/main.go @@ -20,4 +20,11 @@ func main() { c, d := 1, j _, _, _, _, _, _, _, _ = i, _i, j, _j, a, b, c, d } + + var t struct { + Bool bool + } + for _, t.Bool = range []bool{true, false} { + _ = t + } }