Skip to content

Commit

Permalink
go/analysis/passes/assign: add typeparams test
Browse files Browse the repository at this point in the history
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 <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
  • Loading branch information
zpavlinovic authored and odeke-em committed Oct 10, 2021
1 parent ee04797 commit 1af23bd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
7 changes: 6 additions & 1 deletion go/analysis/passes/assign/assign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
33 changes: 33 additions & 0 deletions go/analysis/passes/assign/testdata/src/typeparams/typeparams.go
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 1af23bd

Please sign in to comment.