From 57b8534ef6888161355574dae61a4ee9a1a913cb Mon Sep 17 00:00:00 2001 From: karamaru-alpha Date: Fri, 22 Mar 2024 13:47:20 +0900 Subject: [PATCH] fix: panic when value is not *ast.Ident Signed-off-by: karamaru-alpha --- copyloopvar.go | 4 +++- testdata/src/basic/main.go | 7 +++++++ testdata/src/ignorealias/main.go | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) 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 + } }