Skip to content

Commit

Permalink
Merge pull request #11 from karamaru-alpha/fix/karamaru/false-positive
Browse files Browse the repository at this point in the history
fix:  false positive when rangeStmt doesn't have value
  • Loading branch information
karamaru-alpha committed Mar 27, 2024
2 parents e45640c + d7df1c7 commit 1954e6f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion copyloopvar.go
Expand Up @@ -69,7 +69,7 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
if !ok {
continue
}
if right.Name != key.Name && (value != nil && right.Name != value.Name) {
if right.Name != key.Name && (value == nil || right.Name != value.Name) {
continue
}
if ignoreAlias {
Expand Down
14 changes: 12 additions & 2 deletions testdata/src/basic/main.go
Expand Up @@ -8,7 +8,8 @@ func main() {
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
a, b := 1, i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
c, d := 1, v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
_, _, _, _, _, _, _, _ = i, _i, v, _v, a, b, c, d
e := false
_, _, _, _, _, _, _, _, _ = i, _i, v, _v, a, b, c, d, e
}

for i, j := 1, 1; i+j <= 3; i++ {
Expand All @@ -18,7 +19,16 @@ func main() {
_j := j // want `The copy of the 'for' variable "j" can be deleted \(Go 1\.22\+\)`
a, b := 1, i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
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
e := false
_, _, _, _, _, _, _, _, _ = i, _i, j, _j, a, b, c, d, e
}

for i := range []int{1, 2, 3} {
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
_i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
a, b := 1, i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
c := false
_, _, _, _, _ = i, _i, a, b, c
}

var t struct {
Expand Down
14 changes: 12 additions & 2 deletions testdata/src/ignorealias/main.go
Expand Up @@ -8,7 +8,8 @@ func main() {
_v := v
a, b := 1, i
c, d := 1, v
_, _, _, _, _, _, _, _ = i, _i, v, _v, a, b, c, d
e := false
_, _, _, _, _, _, _, _, _ = i, _i, v, _v, a, b, c, d, e
}

for i, j := 1, 1; i+j <= 3; i++ {
Expand All @@ -18,7 +19,16 @@ func main() {
_j := j
a, b := 1, i
c, d := 1, j
_, _, _, _, _, _, _, _ = i, _i, j, _j, a, b, c, d
e := false
_, _, _, _, _, _, _, _, _ = i, _i, j, _j, a, b, c, d, e
}

for i := range []int{1, 2, 3} {
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
_i := i
a, b := 1, i
c := false
_, _, _, _, _ = i, _i, a, b, c
}

var t struct {
Expand Down

0 comments on commit 1954e6f

Please sign in to comment.