-
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.
internal/analysisinternal: AddImport handles dot imports
If AddImport finds that an existing dot import suffices to refer to an name, it returns that information by means of a first return value of ".", and does not add a new import. For this to work, AddImport must know the name for which an import is needed, so it can determine whether it is shadowed. Change-Id: Ie4c9edf78fb89fc1b64f344517627173a253b999 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647437 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Jonathan Amsterdam <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
- Loading branch information
Showing
16 changed files
with
220 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package fix | ||
|
||
import . "fmt" | ||
|
||
func _(x uint64) { | ||
Println(string(x)) // want `conversion from uint64 to string yields...` | ||
} |
18 changes: 18 additions & 0 deletions
18
go/analysis/passes/stringintconv/testdata/src/fix/fixdot.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,18 @@ | ||
-- Format the number as a decimal -- | ||
package fix | ||
|
||
import . "fmt" | ||
|
||
func _(x uint64) { | ||
Println(Sprint(x)) // want `conversion from uint64 to string yields...` | ||
} | ||
|
||
-- Convert a single rune to a string -- | ||
package fix | ||
|
||
import . "fmt" | ||
|
||
func _(x uint64) { | ||
Println(string(rune(x))) // want `conversion from uint64 to string yields...` | ||
} | ||
|
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
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
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
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
23 changes: 23 additions & 0 deletions
23
gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.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,23 @@ | ||
//go:build go1.23 | ||
|
||
package mapsloop | ||
|
||
import . "maps" | ||
|
||
var _ = Clone[M] // force "maps" import so that each diagnostic doesn't add one | ||
|
||
func useCopyDot(dst, src map[int]string) { | ||
// Replace loop by maps.Copy. | ||
for key, value := range src { | ||
dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" | ||
} | ||
} | ||
|
||
func useCloneDot(src map[int]string) { | ||
// Replace make(...) by maps.Clone. | ||
dst := make(map[int]string, len(src)) | ||
for key, value := range src { | ||
dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" | ||
} | ||
println(dst) | ||
} |
19 changes: 19 additions & 0 deletions
19
gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.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,19 @@ | ||
//go:build go1.23 | ||
|
||
package mapsloop | ||
|
||
import . "maps" | ||
|
||
var _ = Clone[M] // force "maps" import so that each diagnostic doesn't add one | ||
|
||
func useCopyDot(dst, src map[int]string) { | ||
// Replace loop by maps.Copy. | ||
Copy(dst, src) | ||
} | ||
|
||
func useCloneDot(src map[int]string) { | ||
// Replace make(...) by maps.Clone. | ||
dst := Clone(src) | ||
println(dst) | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.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,26 @@ | ||
package sortslice | ||
|
||
import . "slices" | ||
import "sort" | ||
|
||
func _(s []myint) { | ||
sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.Slice can be modernized using slices.Sort" | ||
} | ||
|
||
func _(x *struct{ s []int }) { | ||
sort.Slice(x.s, func(first, second int) bool { return x.s[first] < x.s[second] }) // want "sort.Slice can be modernized using slices.Sort" | ||
} | ||
|
||
func _(s []int) { | ||
sort.Slice(s, func(i, j int) bool { return s[i] > s[j] }) // nope: wrong comparison operator | ||
} | ||
|
||
func _(s []int) { | ||
sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var | ||
} | ||
|
||
func _(s2 []struct{ x int }) { | ||
sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation | ||
} | ||
|
||
func _() { Clip([]int{}) } |
26 changes: 26 additions & 0 deletions
26
gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.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,26 @@ | ||
package sortslice | ||
|
||
import . "slices" | ||
import "sort" | ||
|
||
func _(s []myint) { | ||
Sort(s) // want "sort.Slice can be modernized using slices.Sort" | ||
} | ||
|
||
func _(x *struct{ s []int }) { | ||
Sort(x.s) // want "sort.Slice can be modernized using slices.Sort" | ||
} | ||
|
||
func _(s []int) { | ||
sort.Slice(s, func(i, j int) bool { return s[i] > s[j] }) // nope: wrong comparison operator | ||
} | ||
|
||
func _(s []int) { | ||
sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var | ||
} | ||
|
||
func _(s2 []struct{ x int }) { | ||
sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation | ||
} | ||
|
||
func _() { Clip([]int{}) } |
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
Oops, something went wrong.