-
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/stringintconv: add support for type parameters
Check for potentially invalid string int conversions via type parameters. Updates golang/go#48704 Change-Id: I0269857f3245909cf60c78c6dd624c1c0090c22d Reviewed-on: https://go-review.googlesource.com/c/tools/+/359294 Trust: 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> Reviewed-by: Tim King <taking@google.com>
- Loading branch information
Showing
4 changed files
with
208 additions
and
29 deletions.
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
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
49 changes: 49 additions & 0 deletions
49
go/analysis/passes/stringintconv/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,49 @@ | ||
// Copyright 2021 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. | ||
|
||
package typeparams | ||
|
||
type ( | ||
Int int | ||
Uintptr = uintptr | ||
String string | ||
) | ||
|
||
func _[AllString ~string, MaybeString ~string | ~int, NotString ~int | byte, NamedString String | Int]() { | ||
var ( | ||
i int | ||
r rune | ||
b byte | ||
I Int | ||
U uintptr | ||
M MaybeString | ||
N NotString | ||
) | ||
const p = 0 | ||
|
||
_ = MaybeString(i) // want `conversion from int to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = MaybeString(r) | ||
_ = MaybeString(b) | ||
_ = MaybeString(I) // want `conversion from Int .int. to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = MaybeString(U) // want `conversion from uintptr to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
// Type parameters are never constant types, so arguments are always | ||
// converted to their default type (int versus untyped int, in this case) | ||
_ = MaybeString(p) // want `conversion from int to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
// ...even if the type parameter is only strings. | ||
_ = AllString(p) // want `conversion from int to string .in AllString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
|
||
_ = NotString(i) | ||
_ = NotString(r) | ||
_ = NotString(b) | ||
_ = NotString(I) | ||
_ = NotString(U) | ||
_ = NotString(p) | ||
|
||
_ = NamedString(i) // want `conversion from int to String .string, in NamedString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = string(M) // want `conversion from int .in MaybeString. to string yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
|
||
// Note that M is not convertible to rune. | ||
_ = MaybeString(M) // want `conversion from int .in MaybeString. to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = NotString(N) // ok | ||
} |
49 changes: 49 additions & 0 deletions
49
go/analysis/passes/stringintconv/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,49 @@ | ||
// Copyright 2021 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. | ||
|
||
package typeparams | ||
|
||
type ( | ||
Int int | ||
Uintptr = uintptr | ||
String string | ||
) | ||
|
||
func _[AllString ~string, MaybeString ~string | ~int, NotString ~int | byte, NamedString String | Int]() { | ||
var ( | ||
i int | ||
r rune | ||
b byte | ||
I Int | ||
U uintptr | ||
M MaybeString | ||
N NotString | ||
) | ||
const p = 0 | ||
|
||
_ = MaybeString(rune(i)) // want `conversion from int to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = MaybeString(r) | ||
_ = MaybeString(b) | ||
_ = MaybeString(rune(I)) // want `conversion from Int .int. to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = MaybeString(rune(U)) // want `conversion from uintptr to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
// Type parameters are never constant types, so arguments are always | ||
// converted to their default type (int versus untyped int, in this case) | ||
_ = MaybeString(rune(p)) // want `conversion from int to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
// ...even if the type parameter is only strings. | ||
_ = AllString(rune(p)) // want `conversion from int to string .in AllString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
|
||
_ = NotString(i) | ||
_ = NotString(r) | ||
_ = NotString(b) | ||
_ = NotString(I) | ||
_ = NotString(U) | ||
_ = NotString(p) | ||
|
||
_ = NamedString(rune(i)) // want `conversion from int to String .string, in NamedString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = string(M) // want `conversion from int .in MaybeString. to string yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
|
||
// Note that M is not convertible to rune. | ||
_ = MaybeString(M) // want `conversion from int .in MaybeString. to string .in MaybeString. yields a string of one rune, not a string of digits .did you mean fmt\.Sprint.x.\?.` | ||
_ = NotString(N) // ok | ||
} |