Debian Codegraph @

gopls/internal/test/integration/misc/staticcheck_test.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package misc
6
7 import (
8 "testing"
9
10 . "golang.org/x/tools/gopls/internal/test/integration"
11 )
12
13 func TestStaticcheckGenerics(t *testing.T) {
14 // CL 583778 causes buildir not to run on packages that use
15 // range-over-func, since it might otherwise crash. But nearly
16 // all packages will soon meet this description, so the
17 // analyzers in this test will not run, and the test will fail.
18 // TODO(adonovan): reenable once dominikh/go-tools#1494 is fixed.
19 t.Skip("disabled until buildir supports range-over-func (dominikh/go-tools#1494)")
20
21 const files = `
22 -- go.mod --
23 module mod.com
24
25 go 1.18
26 -- a/a.go --
27 package a
28
29 import (
30 "errors"
31 "sort"
32 "strings"
33 )
34
35 func Zero[P any]() P {
36 var p P
37 return p
38 }
39
40 type Inst[P any] struct {
41 Field P
42 }
43
44 func testGenerics[P *T, T any](p P) {
45 // Calls to instantiated functions should not break checks.
46 slice := Zero[string]()
47 sort.Slice(slice, func(i, j int) bool {
48 return slice[i] < slice[j]
49 })
50
51 // Usage of instantiated fields should not break checks.
52 g := Inst[string]{"hello"}
53 g.Field = strings.TrimLeft(g.Field, "12234")
54
55 // Use of type parameters should not break checks.
56 var q P
57 p = q // SA4009: p is overwritten before its first use
58 q = &*p // SA4001: &* will be simplified
59 }
60
61
62 // FooErr should be called ErrFoo (ST1012)
63 var FooErr error = errors.New("foo")
64 `
65
66 WithOptions(
67 Settings{"staticcheck": true},
68 ).Run(t, files, func(t *testing.T, env *Env) {
69 env.OpenFile("a/a.go")
70 env.AfterChange(
71 Diagnostics(env.AtRegexp("a/a.go", "sort.Slice"), FromSource("sortslice")),
72 Diagnostics(env.AtRegexp("a/a.go", "sort.Slice.(slice)"), FromSource("SA1028")),
73 Diagnostics(env.AtRegexp("a/a.go", "var (FooErr)"), FromSource("ST1012")),
74 Diagnostics(env.AtRegexp("a/a.go", `"12234"`), FromSource("SA1024")),
75 Diagnostics(env.AtRegexp("a/a.go", "testGenerics.*(p P)"), FromSource("SA4009")),
76 Diagnostics(env.AtRegexp("a/a.go", "q = (&\\*p)"), FromSource("SA4001")),
77 )
78 })
79 }
80
81 // Test for golang/go#56270: an analysis with related info should not panic if
82 // analysis.RelatedInformation.End is not set.
83 func TestStaticcheckRelatedInfo(t *testing.T) {
84 // CL 583778 causes buildir not to run on packages that use
85 // range-over-func, since it might otherwise crash. But nearly
86 // all packages will soon meet this description, so the
87 // analyzers in this test will not run, and the test will fail.
88 // TODO(adonovan): reenable once dominikh/go-tools#1494 is fixed.
89 t.Skip("disabled until buildir supports range-over-func (dominikh/go-tools#1494)")
90
91 const files = `
92 -- go.mod --
93 module mod.test
94
95 go 1.18
96 -- p.go --
97 package p
98
99 import (
100 "fmt"
101 )
102
103 func Foo(enabled interface{}) {
104 if enabled, ok := enabled.(bool); ok {
105 } else {
106 _ = fmt.Sprintf("invalid type %T", enabled) // enabled is always bool here
107 }
108 }
109 `
110
111 WithOptions(
112 Settings{"staticcheck": true},
113 ).Run(t, files, func(t *testing.T, env *Env) {
114 env.OpenFile("p.go")
115 env.AfterChange(
116 Diagnostics(env.AtRegexp("p.go", ", (enabled)"), FromSource("SA9008")),
117 )
118 })
119 }