diff --git a/staticcheck/lint.go b/staticcheck/lint.go index 16b6dbd84..22b6bbc2d 100644 --- a/staticcheck/lint.go +++ b/staticcheck/lint.go @@ -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)) } diff --git a/staticcheck/testdata/src/CheckUnreadVariableValues/CheckUnreadVariableValues.go b/staticcheck/testdata/src/CheckUnreadVariableValues/CheckUnreadVariableValues.go index a9efc2ded..da2d1c5d8 100644 --- a/staticcheck/testdata/src/CheckUnreadVariableValues/CheckUnreadVariableValues.go +++ b/staticcheck/testdata/src/CheckUnreadVariableValues/CheckUnreadVariableValues.go @@ -1,5 +1,7 @@ package pkg +import "fmt" + func fn1() { var x int x = gen() // want `this value of x is never used` @@ -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) +}