Skip to content

Commit 8c42f8a

Browse files
xieyuschengopherbot
authored andcommitted
gopls/internal/analysis/modernize: use types.RelativeTo to respect current package
This CL fixes a bug introduced in CL659155. It leverages types.RelativeTo to determine the package name of a variable, ensuring that the package name is not added for types declared within the same package. This prevents invalid type errors. Additionally, this CL introduces additional test cases. Fixes golang/go#73037 Change-Id: I6ad0c80c02ad1a679a956b93a53a05a8eb1ba9c2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660815 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent fbb7047 commit 8c42f8a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

gopls/internal/analysis/modernize/rangeint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func rangeint(pass *analysis.Pass) {
184184
// (Unfortunately types.Qualifier doesn't provide the name of the package
185185
// member to be qualified, a qualifier cannot perform the necessary shadowing
186186
// check for dot-imported names.)
187-
beforeLimit, afterLimit = fmt.Sprintf("%s(", types.TypeString(tVar, (*types.Package).Name)), ")"
187+
beforeLimit, afterLimit = fmt.Sprintf("%s(", types.TypeString(tVar, types.RelativeTo(pass.Pkg))), ")"
188188
info2 := &types.Info{Types: make(map[ast.Expr]types.TypeAndValue)}
189189
if types.CheckExpr(pass.Fset, pass.Pkg, limit.Pos(), limit, info2) == nil {
190190
tLimit := types.Default(info2.TypeOf(limit))

gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,29 @@ func todo() {
205205
println(j)
206206
}
207207
}
208+
209+
type T uint
210+
type TAlias = uint
211+
212+
func Fn(a int) T {
213+
return T(a)
214+
}
215+
216+
func issue73037() {
217+
var q T
218+
for a := T(0); a < q; a++ { // want "for loop can be modernized using range over int"
219+
println(a)
220+
}
221+
for a := Fn(0); a < q; a++ {
222+
println(a)
223+
}
224+
var qa TAlias
225+
for a := TAlias(0); a < qa; a++ { // want "for loop can be modernized using range over int"
226+
println(a)
227+
}
228+
for a := T(0); a < 10; a++ { // want "for loop can be modernized using range over int"
229+
for b := T(0); b < 10; b++ { // want "for loop can be modernized using range over int"
230+
println(a, b)
231+
}
232+
}
233+
}

gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,29 @@ func todo() {
205205
println(j)
206206
}
207207
}
208+
209+
type T uint
210+
type TAlias = uint
211+
212+
func Fn(a int) T {
213+
return T(a)
214+
}
215+
216+
func issue73037() {
217+
var q T
218+
for a := range q { // want "for loop can be modernized using range over int"
219+
println(a)
220+
}
221+
for a := Fn(0); a < q; a++ {
222+
println(a)
223+
}
224+
var qa TAlias
225+
for a := range qa { // want "for loop can be modernized using range over int"
226+
println(a)
227+
}
228+
for a := range T(10) { // want "for loop can be modernized using range over int"
229+
for b := range T(10) { // want "for loop can be modernized using range over int"
230+
println(a, b)
231+
}
232+
}
233+
}

0 commit comments

Comments
 (0)