From 1af23bdac9a48b89e1324abcf52338213d25abc5 Mon Sep 17 00:00:00 2001 From: Zvonimir Pavlinovic Date: Thu, 7 Oct 2021 10:58:47 -0700 Subject: [PATCH] go/analysis/passes/assign: add typeparams test This CL adds a test for the assign pass that involves use of generics. Update golang/go#48704 Change-Id: I355e73130c54bdc2363c686a5b28fe3140a307b5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/354610 Reviewed-by: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Go Bot Trust: Emmanuel Odeke --- go/analysis/passes/assign/assign_test.go | 7 +++- .../testdata/src/typeparams/typeparams.go | 33 +++++++++++++++++++ .../src/typeparams/typeparams.go.golden | 33 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 go/analysis/passes/assign/testdata/src/typeparams/typeparams.go create mode 100644 go/analysis/passes/assign/testdata/src/typeparams/typeparams.go.golden diff --git a/go/analysis/passes/assign/assign_test.go b/go/analysis/passes/assign/assign_test.go index f793e087282..146385f9596 100644 --- a/go/analysis/passes/assign/assign_test.go +++ b/go/analysis/passes/assign/assign_test.go @@ -9,9 +9,14 @@ import ( "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/passes/assign" + "golang.org/x/tools/internal/typeparams" ) func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.RunWithSuggestedFixes(t, testdata, assign.Analyzer, "a") + tests := []string{"a"} + if typeparams.Enabled { + tests = append(tests, "typeparams") + } + analysistest.RunWithSuggestedFixes(t, testdata, assign.Analyzer, tests...) } diff --git a/go/analysis/passes/assign/testdata/src/typeparams/typeparams.go b/go/analysis/passes/assign/testdata/src/typeparams/typeparams.go new file mode 100644 index 00000000000..345db277933 --- /dev/null +++ b/go/analysis/passes/assign/testdata/src/typeparams/typeparams.go @@ -0,0 +1,33 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains tests for the useless-assignment checker. + +//go:build go1.18 + +package testdata + +import "math/rand" + +type ST[T interface{ ~int }] struct { + x T + l []T +} + +func (s *ST[T]) SetX(x T, ch chan T) { + // Accidental self-assignment; it should be "s.x = x" + x = x // want "self-assignment of x to x" + // Another mistake + s.x = s.x // want "self-assignment of s.x to s.x" + + s.l[0] = s.l[0] // want "self-assignment of s.l.0. to s.l.0." + + // Bail on any potential side effects to avoid false positives + s.l[num()] = s.l[num()] + rng := rand.New(rand.NewSource(0)) + s.l[rng.Intn(len(s.l))] = s.l[rng.Intn(len(s.l))] + s.l[<-ch] = s.l[<-ch] +} + +func num() int { return 2 } diff --git a/go/analysis/passes/assign/testdata/src/typeparams/typeparams.go.golden b/go/analysis/passes/assign/testdata/src/typeparams/typeparams.go.golden new file mode 100644 index 00000000000..d9384ed5aab --- /dev/null +++ b/go/analysis/passes/assign/testdata/src/typeparams/typeparams.go.golden @@ -0,0 +1,33 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains tests for the useless-assignment checker. + +//go:build go1.18 + +package testdata + +import "math/rand" + +type ST[T interface{ ~int }] struct { + x T + l []T +} + +func (s *ST[T]) SetX(x T, ch chan T) { + // Accidental self-assignment; it should be "s.x = x" + // want "self-assignment of x to x" + // Another mistake + // want "self-assignment of s.x to s.x" + + // want "self-assignment of s.l.0. to s.l.0." + + // Bail on any potential side effects to avoid false positives + s.l[num()] = s.l[num()] + rng := rand.New(rand.NewSource(0)) + s.l[rng.Intn(len(s.l))] = s.l[rng.Intn(len(s.l))] + s.l[<-ch] = s.l[<-ch] +} + +func num() int { return 2 }