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

Add linter wastedassign #1651

Merged
merged 8 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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.2
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
Expand Down
3 changes: 3 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/golinters/wastedassign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package golinters

import (
"github.com/sanposhiho/wastedassign"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewWastedAssign() *goanalysis.Linter {
analyzers := []*analysis.Analyzer{
wastedassign.Analyzer,
}

return goanalysis.NewLinter(
"wastedassign",
"wastedassign finds wasted assignment statements.",
analyzers,
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/charithe/durationcheck"),
linter.NewConfig(golinters.NewWastedAssign()).
WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle).
WithURL("https://github.com/sanposhiho/wastedassign"),
SVilgelm marked this conversation as resolved.
Show resolved Hide resolved

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
107 changes: 107 additions & 0 deletions test/testdata/wastedassign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//args: -Ewastedassign
package testdata

import (
"strings"
)

func p(x int) int {
return x + 1
}

func typeSwitchNoError(val interface{}, times uint) interface{} {
switch hoge := val.(type) {
case int:
return 12
case string:
return strings.Repeat(hoge, int(times))
default:
return nil
}
}

func noUseParamsNoError(params string) int {
a := 12
println(a)
return a
}

func manyif(param int) int {
println(param)
useOutOfIf := 1212121 // ERROR "wasted assignment"
ret := 0
if false {
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
return 0
} else if param == 100 {
useOutOfIf = 100 // ERROR "wasted assignment"
useOutOfIf = 201
useOutOfIf = p(useOutOfIf)
useOutOfIf += 200 // ERROR "wasted assignment"
} else {
useOutOfIf = 100
useOutOfIf += 100
useOutOfIf = p(useOutOfIf)
useOutOfIf += 200 // ERROR "wasted assignment"
}

if false {
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
return 0
} else if param == 200 {
useOutOfIf = 100 // ERROR "wasted assignment"
useOutOfIf = 201
useOutOfIf = p(useOutOfIf)
useOutOfIf += 200
} else {
useOutOfIf = 100
useOutOfIf += 100
useOutOfIf = p(useOutOfIf)
useOutOfIf += 200
}
println(useOutOfIf)
useOutOfIf = 192
useOutOfIf += 100
useOutOfIf += 200 // ERROR "reassigned, but never used afterwards"
return ret
}

func checkLoopTest() int {
hoge := 12
noUse := 1111
println(noUse)

noUse = 1111 // ERROR "reassigned, but never used afterwards"
for {
if hoge == 14 {
break
}
hoge = hoge + 1
}
return hoge
}

func infinity() {
var i int
var hoge int
for {
hoge = 5 // ERROR "reassigned, but never used afterwards"
}

println(i)
println(hoge)
return
}

func infinity2() {
var i int
var hoge int
for {
hoge = 5
break
}

println(i)
println(hoge)
return
}