Skip to content

Commit

Permalink
go/analysis/passes/tests: enable fuzz checks in 'tests' analysis pass…
Browse files Browse the repository at this point in the history
… for cmd/vet

This will remove the flag analysisinternal.DiagnoseFuzzTests created during golang/go#50198.Malformed fuzz target check will be enabled for cmd/vet.

For golang/go#46218

Change-Id: I5cc8d685a57060f8dd84c1957f0d296a6205ddb6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/471295
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Nooras Saba‎ <saba@golang.org>
  • Loading branch information
ansaba authored and gopherbot committed Mar 30, 2023
1 parent 6546e8e commit f4e613e
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go/analysis/passes/tests/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
// tests: check for common mistaken usages of tests and examples
//
// The tests checker walks Test, Benchmark and Example functions checking
// The tests checker walks Test, Benchmark, Fuzzing and Example functions checking
// malformed names, wrong signatures and examples documenting non-existent
// identifiers.
//
Expand Down
5 changes: 1 addition & 4 deletions go/analysis/passes/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/internal/analysisinternal"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -68,9 +67,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
checkTest(pass, fn, "Test")
case strings.HasPrefix(fn.Name.Name, "Benchmark"):
checkTest(pass, fn, "Benchmark")
}
// run fuzz tests diagnostics only for 1.18 i.e. when analysisinternal.DiagnoseFuzzTests is turned on.
if strings.HasPrefix(fn.Name.Name, "Fuzz") && analysisinternal.DiagnoseFuzzTests {
case strings.HasPrefix(fn.Name.Name, "Fuzz"):
checkTest(pass, fn, "Fuzz")
checkFuzz(pass, fn)
}
Expand Down
9 changes: 0 additions & 9 deletions go/analysis/passes/tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ package tests_test
import (
"testing"

"golang.org/x/tools/internal/analysisinternal"

"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/tests"
"golang.org/x/tools/internal/typeparams"
)

func Test(t *testing.T) {
// In 1.18, diagnostic for Fuzz Tests must not be used by cmd/vet.
// So the code for Fuzz tests diagnostics is guarded behind flag analysisinternal.DiagnoseFuzzTests
// Turn on the flag DiagnoseFuzzTests for analysis tests and then turn it off.
analysisinternal.DiagnoseFuzzTests = true
defer func() {
analysisinternal.DiagnoseFuzzTests = false
}()
testdata := analysistest.TestData()
pkgs := []string{
"a", // loads "a", "a [a.test]", and "a.test"
Expand Down
2 changes: 1 addition & 1 deletion gopls/doc/analyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ started by the test. For example:

check for common mistaken usages of tests and examples

The tests checker walks Test, Benchmark and Example functions checking
The tests checker walks Test, Benchmark, Fuzzing and Example functions checking
malformed names, wrong signatures and examples documenting non-existent
identifiers.

Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/lsp/source/api_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions gopls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ import (

"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/gopls/internal/lsp/cmd"
"golang.org/x/tools/internal/analysisinternal"
"golang.org/x/tools/internal/tool"
)

func main() {
// In 1.18, diagnostics for Fuzz tests must not be used by cmd/vet.
// So the code for Fuzz tests diagnostics is guarded behind flag analysisinternal.DiagnoseFuzzTests
// Turn on analysisinternal.DiagnoseFuzzTests for gopls
analysisinternal.DiagnoseFuzzTests = true
ctx := context.Background()
tool.Main(ctx, cmd.New("gopls", "", nil, hooks.Options), os.Args[1:])
}
4 changes: 0 additions & 4 deletions internal/analysisinternal/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"strconv"
)

// DiagnoseFuzzTests controls whether the 'tests' analyzer diagnoses fuzz tests
// in Go 1.18+.
var DiagnoseFuzzTests bool = false

func TypeErrorEndPos(fset *token.FileSet, src []byte, start token.Pos) token.Pos {
// Get the end position for the type error.
offset, end := fset.PositionFor(start, false).Offset, start
Expand Down

0 comments on commit f4e613e

Please sign in to comment.