Skip to content

Commit c7873a3

Browse files
committed
gopls/internal/golang: eliminate dot import: skip keyed fields
This CL fixes a bug in "eliminate dot import" that would cause it to change field F in T{F: ...} to T{pkg.F: ...}. + test Fixes golang/go#73960 Change-Id: I090dacf855ec04137b5995d22a0b6fafc3f32d61 Reviewed-on: https://go-review.googlesource.com/c/tools/+/678595 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 6e8a193 commit c7873a3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

gopls/internal/golang/codeaction.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818

1919
"golang.org/x/tools/go/ast/astutil"
20+
"golang.org/x/tools/go/ast/edge"
2021
"golang.org/x/tools/go/ast/inspector"
2122
"golang.org/x/tools/gopls/internal/analysis/fillstruct"
2223
"golang.org/x/tools/gopls/internal/analysis/fillswitch"
@@ -733,11 +734,15 @@ func refactorRewriteEliminateDotImport(ctx context.Context, req *codeActionsRequ
733734
continue
734735
}
735736

736-
// Only qualify unqualified identifiers (due to dot imports).
737+
// Only qualify unqualified identifiers (due to dot imports)
738+
// that reference package-level symbols.
737739
// All other references to a symbol imported from another package
738740
// are nested within a select expression (pkg.Foo, v.Method, v.Field).
739-
if is[*ast.SelectorExpr](curId.Parent().Node()) {
740-
continue
741+
if ek, _ := curId.ParentEdge(); ek == edge.SelectorExpr_Sel {
742+
continue // qualified identifier (pkg.X) or selector (T.X or e.X)
743+
}
744+
if !typesinternal.IsPackageLevel(use) {
745+
continue // unqualified field reference T{X: ...}
741746
}
742747

743748
// Make sure that the package name will not be shadowed by something else in scope.

gopls/internal/test/marker/testdata/codeaction/eliminate_dot_import.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
This test checks the behavior of the 'remove dot import' code action.
22

33
-- go.mod --
4-
module golang.org/lsptests/removedotimport
4+
module example.com
55

66
go 1.18
77

@@ -13,6 +13,7 @@ package dotimport
1313
import (
1414
. "fmt" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a1)
1515
. "bytes" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a2)
16+
. "time" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a3)
1617
)
1718

1819
var _ = a
@@ -22,19 +23,28 @@ func a() {
2223

2324
buf := NewBuffer(nil)
2425
buf.Grow(10)
26+
27+
_ = Ticker{C: nil}
2528
}
2629

2730
-- @a1/a.go --
2831
@@ -6 +6 @@
2932
- . "fmt" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a1)
3033
+ "fmt" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a1)
31-
@@ -13 +13 @@
34+
@@ -14 +14 @@
3235
- Println("hello")
3336
+ fmt.Println("hello")
3437
-- @a2/a.go --
3538
@@ -7 +7 @@
3639
- . "bytes" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a2)
3740
+ "bytes" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a2)
38-
@@ -15 +15 @@
41+
@@ -16 +16 @@
3942
- buf := NewBuffer(nil)
4043
+ buf := bytes.NewBuffer(nil)
44+
-- @a3/a.go --
45+
@@ -8 +8 @@
46+
- . "time" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a3)
47+
+ "time" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a3)
48+
@@ -19 +19 @@
49+
- _ = Ticker{C: nil}
50+
+ _ = time.Ticker{C: nil}

0 commit comments

Comments
 (0)