File tree 2 files changed +26
-8
lines changed
testdata/go1.0/CheckIfReturn
2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change 5
5
"go/ast"
6
6
"go/constant"
7
7
"go/token"
8
+ "strings"
8
9
9
10
"honnef.co/go/tools/analysis/code"
10
11
"honnef.co/go/tools/analysis/facts/generated"
@@ -88,8 +89,26 @@ func run(pass *analysis.Pass) (any, error) {
88
89
return
89
90
}
90
91
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
+
91
110
// 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 ) {
93
112
return
94
113
}
95
114
Original file line number Diff line number Diff line change @@ -31,13 +31,12 @@ func cmt4(x string) bool {
31
31
// B
32
32
}
33
33
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
+ }
41
40
42
41
func cmt6 (x string ) bool {
43
42
if len (x ) > 0 { //@ diag(`should use 'return len(x) == 0'`)
You can’t perform that action at this time.
0 commit comments