-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <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
1 parent
ee04797
commit 1af23bd
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
go/analysis/passes/assign/testdata/src/typeparams/typeparams.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
33 changes: 33 additions & 0 deletions
33
go/analysis/passes/assign/testdata/src/typeparams/typeparams.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |