Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false-positive: reassign inside an infinite loop and not use after exiting loop #24

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func mugen() {
var i int
var hoge int
for {
hoge = 5 // want "reassigned, but never used afterwards"
hoge = 5 // want "wasted assignment"
// break
}

Expand All @@ -130,3 +130,26 @@ func noMugen() {
println(hoge)
return
}

func reassignInsideLoop() {
bar := func(b []byte) ([]byte, error) { return b, nil }
var err error
var rest []byte
for {
rest, err = bar(rest)
if err == nil {
break
}
}
return
}

func reassignInsideLoop2() {
var x int = 0
var y int = 1
for i := 1; i < 3; i++ {
x += y
y *= 2 * i
}
println(x)
}
10 changes: 5 additions & 5 deletions wastedassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,20 @@ func (wr wastedReason) String() string {
}
}

func isNextOperationToOpIsStore(bls []*ssa.BasicBlock, currentOp *ssa.Value, haveCheckedMap map[int]bool) wastedReason {
func isNextOperationToOpIsStore(bls []*ssa.BasicBlock, currentOp *ssa.Value, haveCheckedMap map[int]int) wastedReason {
var wastedReasons []wastedReason
var wastedReasonsCurrentBls []wastedReason

if haveCheckedMap == nil {
haveCheckedMap = map[int]bool{}
haveCheckedMap = map[int]int{}
}

for _, bl := range bls {
if haveCheckedMap[bl.Index] {
if haveCheckedMap[bl.Index] == 2 {
continue
}

haveCheckedMap[bl.Index] = true
haveCheckedMap[bl.Index]++
breakFlag := false
for _, ist := range bl.Instrs {
if breakFlag {
Expand All @@ -201,7 +201,7 @@ func isNextOperationToOpIsStore(bls []*ssa.BasicBlock, currentOp *ssa.Value, hav
var buf [10]*ssa.Value
for _, op := range ist.Operands(buf[:0]) {
if *op == *currentOp {
// 連続storeではなかった
// It wasn't a continuous store.
return notWasted
}
}
Expand Down