Skip to content

Commit

Permalink
staticcheck: don't flag nil constants as useless assignments
Browse files Browse the repository at this point in the history
Closes gh-733
  • Loading branch information
dominikh committed Jul 30, 2020
1 parent 3c17a0d commit 276c3dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions staticcheck/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,10 @@ func CheckUnreadVariableValues(pass *analysis.Pass) (interface{}, error) {
continue
}

if _, ok := val.(*ir.Const); ok {
// a zero-valued constant, for example in 'foo := []string(nil)'
continue
}
if !hasUse(val, nil) {
report.Report(pass, assign, fmt.Sprintf("this value of %s is never used", lhs))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pkg

import "fmt"

func fn1() {
var x int
x = gen() // want `this value of x is never used`
Expand Down Expand Up @@ -130,3 +132,13 @@ func resolveWeakTypes(types []int) {
}

func findRunLimit(int) int { return 0 }

func fn10() {
slice := []string(nil)
if true {
slice = []string{"1", "2"}
} else {
slice = []string{"3", "4"}
}
fmt.Println(slice)
}

0 comments on commit 276c3dd

Please sign in to comment.