| 1 |
Test of warning diagnostics from various analyzers: |
| 2 |
copylocks, printf, slog, tests, timeformat, nilness, and cgocall. |
| 3 |
|
| 4 |
-- settings.json -- |
| 5 |
{ |
| 6 |
"pullDiagnostics": true |
| 7 |
} |
| 8 |
|
| 9 |
-- go.mod -- |
| 10 |
module example.com |
| 11 |
go 1.23 |
| 12 |
|
| 13 |
-- flags -- |
| 14 |
-min_go_command=go1.23 |
| 15 |
-cgo |
| 16 |
|
| 17 |
-- bad/bad_test.go -- |
| 18 |
package bad |
| 19 |
|
| 20 |
import ( |
| 21 |
"fmt" |
| 22 |
"iter" |
| 23 |
"log/slog" |
| 24 |
"sync" |
| 25 |
"testing" |
| 26 |
"time" |
| 27 |
) |
| 28 |
|
| 29 |
// copylocks |
| 30 |
func _() { |
| 31 |
var x sync.Mutex |
| 32 |
_ = x //@diag("x", re"assignment copies lock value to _: sync.Mutex") |
| 33 |
} |
| 34 |
|
| 35 |
// printf |
| 36 |
func _() { |
| 37 |
printfWrapper("%s") //@diag(re`%s`, re"example.com/bad.printfWrapper format %s reads arg #1, but call has 0 args") |
| 38 |
} |
| 39 |
|
| 40 |
func printfWrapper(format string, args ...any) { |
| 41 |
fmt.Printf(format, args...) |
| 42 |
} |
| 43 |
|
| 44 |
// tests |
| 45 |
func Testbad(t *testing.T) { //@diag("Testbad", re"Testbad has malformed name: first letter after 'Test' must not be lowercase") |
| 46 |
} |
| 47 |
|
| 48 |
// timeformat |
| 49 |
func _() { |
| 50 |
now := time.Now() |
| 51 |
fmt.Println(now.Format("2006-02-01")) //@diag("2006-02-01", re"2006-02-01 should be 2006-01-02") |
| 52 |
} |
| 53 |
|
| 54 |
// nilness |
| 55 |
func _(ptr *int) { |
| 56 |
if ptr == nil { |
| 57 |
_ = *ptr //@diag("*ptr", re"nil dereference in load") |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// unusedwrite |
| 62 |
func _(s struct{x int}) { |
| 63 |
s.x = 1 //@diag("x", re"unused write to field x") |
| 64 |
} |
| 65 |
|
| 66 |
// slog |
| 67 |
func _() { |
| 68 |
slog.Info("msg", 1) //@diag("1", re`slog.Info arg "1" should be a string or a slog.Attr`) |
| 69 |
} |
| 70 |
|
| 71 |
// waitgroup |
| 72 |
func _() { |
| 73 |
var wg sync.WaitGroup |
| 74 |
go func() { |
| 75 |
wg.Add(1) //@diag("(", re"WaitGroup.Add called from inside new goroutine") |
| 76 |
}() |
| 77 |
} |
| 78 |
|
| 79 |
// inline |
| 80 |
func _() { |
| 81 |
f() //@diag("f", re"Call of bad.f should be inlined") |
| 82 |
} |
| 83 |
|
| 84 |
//go:fix inline |
| 85 |
func f() { fmt.Println(1) } |
| 86 |
|
| 87 |
// recursiveiter |
| 88 |
func F() iter.Seq[int] { |
| 89 |
return func(yield func(int) bool) { |
| 90 |
for range F() {} //@ diag("range", re"inefficient recursion in iterator F") |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
-- cgocall/cgocall.go -- |
| 95 |
package cgocall |
| 96 |
|
| 97 |
// Note: this test must be in a separate package, as the unsafe import |
| 98 |
// silences the unusedwrite analyzer. |
| 99 |
import "unsafe" |
| 100 |
|
| 101 |
// void f(void *ptr) {} |
| 102 |
import "C" |
| 103 |
|
| 104 |
// cgocall |
| 105 |
func _(c chan bool) { |
| 106 |
C.f(unsafe.Pointer(&c)) //@ diag("unsafe", re"passing Go type with embedded pointer to C") |
| 107 |
} |
| 108 |
|
| 109 |
-- maprange/maprange.go -- |
| 110 |
package maprange |
| 111 |
|
| 112 |
import "maps" |
| 113 |
|
| 114 |
func _(m map[int]int) { |
| 115 |
for range maps.Keys(m) {} //@ diag("maps.Keys", re"unnecessary and inefficient call of maps.Keys") |
| 116 |
} |
| 117 |
|
| 118 |
-- unusedresult/unusedresult.go -- |
| 119 |
package unusedresult |
| 120 |
|
| 121 |
import "fmt" |
| 122 |
|
| 123 |
func _() { |
| 124 |
fmt.Appendf(nil, "%d", 1) //@ diag("fmt.Appendf", re"result.*not used") |
| 125 |
} |
| 126 |
|
| 127 |
-- staticcheck/staticcheck.go -- |
| 128 |
package staticcheck |
| 129 |
|
| 130 |
// staticcheck includes hundreds of other analyzers. |
| 131 |
// Here we test only two: one enabled by default, one disabled. |
| 132 |
|
| 133 |
func S1000(ch chan int) { |
| 134 |
select { case <-ch: } //@ diag("select", re"use .*receive instead of select") |
| 135 |
} |
| 136 |
|
| 137 |
func S1011(x, y []int) { |
| 138 |
for _, e := range y { |
| 139 |
x = append(x, e) // no "replace loop with append" diagnostic |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
-- sqlrowserr/sqlrowserr.go -- |
| 144 |
package sqlrowserr |
| 145 |
|
| 146 |
import "database/sql" |
| 147 |
|
| 148 |
func _(db *sql.DB) { |
| 149 |
rows, err := db.Query("") //@ diag("db.Query", re`sql.Rows "rows" is used in Next loop at line .. without final check of rows.Err\(\)`) |
| 150 |
if err != nil { |
| 151 |
return |
| 152 |
} |
| 153 |
defer rows.Close() // ignore error |
| 154 |
for rows.Next() { |
| 155 |
rows.Scan() // ignore error |
| 156 |
} |
| 157 |
} |