Skip to content

Commit de48f96

Browse files
committed
S1008: ignore instructions used in our tests
1 parent 2585709 commit de48f96

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

Diff for: simple/s1008/s1008.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/ast"
66
"go/constant"
77
"go/token"
8+
"strings"
89

910
"honnef.co/go/tools/analysis/code"
1011
"honnef.co/go/tools/analysis/facts/generated"
@@ -88,8 +89,26 @@ func run(pass *analysis.Pass) (any, error) {
8889
return
8990
}
9091

92+
hasComments := func(n ast.Node) bool {
93+
cmf := cm.Filter(n)
94+
for _, groups := range cmf {
95+
for _, group := range groups {
96+
for _, cmt := range group.List {
97+
if strings.HasPrefix(cmt.Text, "//@ diag") {
98+
// Staticcheck test cases use comments to mark
99+
// expected diagnostics. Ignore these comments so we
100+
// can test this check.
101+
continue
102+
}
103+
return true
104+
}
105+
}
106+
}
107+
return false
108+
}
109+
91110
// Don't flag if both are commented.
92-
if len(cm.Filter(n1)) > 0 && len(cm.Filter(n2)) > 0 {
111+
if hasComments(n1) && hasComments(n2) {
93112
return
94113
}
95114

Diff for: simple/s1008/testdata/go1.0/CheckIfReturn/comment.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ func cmt4(x string) bool {
3131
// B
3232
}
3333

34-
// Hard to test, as the diag line adds a comment.
35-
//func cmt5(x string) bool {
36-
// if len(x) > 0 { // diag(`should use 'return len(x) == 0'`)
37-
// return false
38-
// }
39-
// return true // A
40-
//}
34+
func cmt5(x string) bool {
35+
if len(x) > 0 { //@ diag(`should use 'return len(x) == 0'`)
36+
return false
37+
}
38+
return true // A
39+
}
4140

4241
func cmt6(x string) bool {
4342
if len(x) > 0 { //@ diag(`should use 'return len(x) == 0'`)

0 commit comments

Comments
 (0)