Skip to content

Commit

Permalink
update: warn message
Browse files Browse the repository at this point in the history
Signed-off-by: karamaru-alpha <mrnk3078@gmail.com>
  • Loading branch information
karamaru-alpha committed Feb 13, 2024
1 parent 62ff078 commit a75afc3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ cf. [Fixing For Loops in Go 1.22](https://go.dev/blog/loopvar-preview)
## Example
```go
for i, v := range []int{1, 2, 3} {
i := i // The loop variable "i" doesn't need to be copied
v := v // The loop variable "v" doesn't need to be copied
i := i // It's unnecessary to copy the loop variable "i"
v := v // It's unnecessary to copy the loop variable "v"
_, _ = i, v
}

for i := 1; i <= 3; i++ {
i := i // The loop variable "i" doesn't need to be copied``
i := i // It's unnecessary to copy the loop variable "i"
_ = i
}
```
Expand Down
4 changes: 2 additions & 2 deletions copyloopvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
}
pass.Report(analysis.Diagnostic{
Pos: assignStmt.Pos(),
Message: fmt.Sprintf(`The loop variable "%s" doesn't need to be copied`, right.Name),
Message: fmt.Sprintf(`It's unnecessary to copy the loop variable "%s"`, right.Name),
})
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func checkForStmt(pass *analysis.Pass, forStmt *ast.ForStmt) {
}
pass.Report(analysis.Diagnostic{
Pos: assignStmt.Pos(),
Message: fmt.Sprintf(`The loop variable "%s" doesn't need to be copied`, right.Name),
Message: fmt.Sprintf(`It's unnecessary to copy the loop variable "%s"`, right.Name),
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions testdata/src/a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package a

func f() {
for i, v := range []int{1, 2, 3} {
i := i // want `The loop variable "i" doesn't need to be copied`
_v := v // want `The loop variable "v" doesn't need to be copied`
i := i // want `It's unnecessary to copy the loop variable "i"`
_v := v // want `It's unnecessary to copy the loop variable "v"`
_ = i
_ = _v
}

for i := 1; i <= 3; i++ {
i := i // want `The loop variable "i" doesn't need to be copied`
i := i // want `It's unnecessary to copy the loop variable "i"`
_ = i
}

for i, j := 1, 1; i+j <= 3; i++ {
i := i // want `The loop variable "i" doesn't need to be copied`
j, _ := j, 1 // want `The loop variable "j" doesn't need to be copied`
i := i // want `It's unnecessary to copy the loop variable "i"`
j, _ := j, 1 // want `It's unnecessary to copy the loop variable "j"`
_, _ = i, j
}
}

0 comments on commit a75afc3

Please sign in to comment.