diff --git a/go.mod b/go.mod index 1e76ab4e7472..2923438a65c0 100644 --- a/go.mod +++ b/go.mod @@ -57,7 +57,7 @@ require ( github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f github.com/ryancurrah/gomodguard v1.2.0 github.com/ryanrolds/sqlclosecheck v0.3.0 - github.com/sanposhiho/wastedassign v0.1.3 + github.com/sanposhiho/wastedassign v0.2.0 github.com/securego/gosec/v2 v2.6.1 github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c github.com/shirou/gopsutil/v3 v3.21.1 diff --git a/go.sum b/go.sum index 9571cb8a4ef3..b25418406adc 100644 --- a/go.sum +++ b/go.sum @@ -360,8 +360,8 @@ github.com/ryancurrah/gomodguard v1.2.0/go.mod h1:rNqbC4TOIdUDcVMSIpNNAzTbzXAZa6 github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8OUZI9xFw= github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/sanposhiho/wastedassign v0.1.3 h1:qIMpTh4NGZYRbFJ+DSpLoVn8F4SLciX2afRvXPefC7w= -github.com/sanposhiho/wastedassign v0.1.3/go.mod h1:LGpq5Hsv74QaqM47WtIsRSF/ik9kqk07kchgv66tLVE= +github.com/sanposhiho/wastedassign v0.2.0 h1:0vycy8D/Ky55U5ub8oJFqyDv9M4ICM/wte9sAp2/7Mc= +github.com/sanposhiho/wastedassign v0.2.0/go.mod h1:LGpq5Hsv74QaqM47WtIsRSF/ik9kqk07kchgv66tLVE= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/securego/gosec/v2 v2.6.1 h1:+KCw+uz16FYfFyJ/A5aU6uP7mnrL+j1TbDnk1yN+8R0= github.com/securego/gosec/v2 v2.6.1/go.mod h1:I76p3NTHBXsGhybUW+cEQ692q2Vp+A0Z6ZLzDIZy+Ao= diff --git a/test/testdata/wastedassign.go b/test/testdata/wastedassign.go index 923ab7aae0ed..c84623d8ea1a 100644 --- a/test/testdata/wastedassign.go +++ b/test/testdata/wastedassign.go @@ -1,64 +1,64 @@ //args: -Ewastedassign package testdata -import ( - "strings" -) +import "strings" -func p(x int) int { +func pa(x int) int { return x + 1 } -func typeSwitchNoError(val interface{}, times uint) interface{} { - switch hoge := val.(type) { +func multiple(val interface{}, times uint) interface{} { + + switch hogehoge := val.(type) { case int: return 12 case string: - return strings.Repeat(hoge, int(times)) + return strings.Repeat(hogehoge, int(times)) default: return nil } } -func noUseParamsNoError(params string) int { +func noUseParams(params string) int { a := 12 println(a) return a } -func manyif(param int) int { +func f(param int) int { println(param) - useOutOfIf := 1212121 // ERROR "wasted assignment" + useOutOfIf := 1212121 // ERROR "reassigned, but reassigned without using the value" ret := 0 if false { useOutOfIf = 200 // ERROR "reassigned, but never used afterwards" return 0 } else if param == 100 { - useOutOfIf = 100 // ERROR "wasted assignment" + useOutOfIf = 100 // ERROR "reassigned, but reassigned without using the value" useOutOfIf = 201 - useOutOfIf = p(useOutOfIf) - useOutOfIf += 200 // ERROR "wasted assignment" + useOutOfIf = pa(useOutOfIf) + useOutOfIf += 200 // ERROR "reassigned, but reassigned without using the value" } else { useOutOfIf = 100 useOutOfIf += 100 - useOutOfIf = p(useOutOfIf) - useOutOfIf += 200 // ERROR "wasted assignment" + useOutOfIf = pa(useOutOfIf) + useOutOfIf += 200 // ERROR "reassigned, but reassigned without using the value" } if false { useOutOfIf = 200 // ERROR "reassigned, but never used afterwards" return 0 } else if param == 200 { - useOutOfIf = 100 // ERROR "wasted assignment" + useOutOfIf = 100 // ERROR "reassigned, but reassigned without using the value" useOutOfIf = 201 - useOutOfIf = p(useOutOfIf) + useOutOfIf = pa(useOutOfIf) useOutOfIf += 200 } else { useOutOfIf = 100 useOutOfIf += 100 - useOutOfIf = p(useOutOfIf) + useOutOfIf = pa(useOutOfIf) useOutOfIf += 200 } + // useOutOfIf = 12 println(useOutOfIf) useOutOfIf = 192 useOutOfIf += 100 @@ -81,11 +81,35 @@ func checkLoopTest() int { return hoge } -func infinity() { +func r(param int) int { + println(param) + useOutOfIf := 1212121 + ret := 0 + if false { + useOutOfIf = 200 // ERROR "reassigned, but never used afterwards" + return 0 + } else if param == 100 { + ret = useOutOfIf + } else if param == 200 { + useOutOfIf = 100 // ERROR "reassigned, but reassigned without using the value" + useOutOfIf = 100 + useOutOfIf = pa(useOutOfIf) + useOutOfIf += 200 // ERROR "reassigned, but reassigned without using the value" + } + useOutOfIf = 12 + println(useOutOfIf) + useOutOfIf = 192 + useOutOfIf += 100 + useOutOfIf += 200 // ERROR "reassigned, but never used afterwards" + return ret +} + +func mugen() { var i int var hoge int for { - hoge = 5 // ERROR "reassigned, but never used afterwards" + hoge = 5 // ERROR "reassigned, but reassigned without using the value" + // break } println(i) @@ -93,7 +117,7 @@ func infinity() { return } -func infinity2() { +func noMugen() { var i int var hoge int for { @@ -105,3 +129,26 @@ func infinity2() { 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) +}