Skip to content

Commit c3ae702

Browse files
committed
SA4023: generate better message when we know that the related information will be dropped
Updates gh-972
1 parent e604f91 commit c3ae702

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

analysis/report/report.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ func shortRange(node ast.Node) (pos, end token.Pos) {
130130
}
131131
}
132132

133+
func HasRange(node Positioner) bool {
134+
// we don't know if getRange will be called with shortRange set to
135+
// true, so make sure that both work.
136+
_, _, ok := getRange(node, false)
137+
if !ok {
138+
return false
139+
}
140+
_, _, ok = getRange(node, true)
141+
return ok
142+
}
143+
133144
func getRange(node Positioner, short bool) (pos, end token.Pos, ok bool) {
134145
switch n := node.(type) {
135146
case sourcer:

staticcheck/lint.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,8 +4306,13 @@ func CheckTypedNilInterface(pass *analysis.Pass) (interface{}, error) {
43064306
default:
43074307
panic("unreachable")
43084308
}
4309-
report.Report(pass, binop, fmt.Sprintf("this comparison is %s true", qualifier),
4310-
report.Related(x.X, "the lhs of the comparison gets its value from here and has a concrete type"))
4309+
if report.HasRange(x.X) {
4310+
report.Report(pass, binop, fmt.Sprintf("this comparison is %s true", qualifier),
4311+
report.Related(x.X, "the lhs of the comparison gets its value from here and has a concrete type"))
4312+
} else {
4313+
// we can't generate related information for this, so make the diagnostic itself slightly more useful
4314+
report.Report(pass, binop, fmt.Sprintf("this comparison is %s true; the lhs of the comparison has been assigned a concretely typed value", qualifier))
4315+
}
43114316
continue
43124317
}
43134318
if obj == nil {

staticcheck/testdata/src/CheckTypedNilInterface/CheckTypedNilInterface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,7 @@ func test() {
204204
_ = gen20() == nil
205205
_ = gen21() == nil
206206
_ = gen22() == nil // want `never true`
207+
208+
var v1 interface{} = 0
209+
_ = v1 == nil // want `never true; the lhs`
207210
}

0 commit comments

Comments
 (0)