Skip to content

Commit 8b5abd4

Browse files
committed
[gopls-release-branch.0.14] gopls/internal/lsp: fix code action panic on params of external funcs
Fix a panic when inspecting a nil function body. Fixes golang/go#63755 Change-Id: I39342902b44192dd373dfdb24947079b40dbe115 Reviewed-on: https://go-review.googlesource.com/c/tools/+/537878 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> (cherry picked from commit 080c202) Reviewed-on: https://go-review.googlesource.com/c/tools/+/537879 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Peter Weinberger <[email protected]>
1 parent 3b17702 commit 8b5abd4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

gopls/internal/lsp/code_action.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,10 @@ func canRemoveParameter(pkg source.Package, pgf *source.ParsedGoFile, rng protoc
541541
return false
542542
}
543543

544+
if info.Decl.Body == nil {
545+
return false // external function
546+
}
547+
544548
if len(info.Field.Names) == 0 {
545549
return true // no names => field is unused
546550
}

gopls/internal/regtest/misc/fix_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,36 @@ func Foo() error {
101101
env.AfterChange(NoDiagnostics(ForFile("main.go")))
102102
})
103103
}
104+
105+
func TestUnusedParameter_Issue63755(t *testing.T) {
106+
// This test verifies the fix for #63755, where codeActions panicked on parameters
107+
// of functions with no function body.
108+
109+
// We should not detect parameters as unused for external functions.
110+
111+
const files = `
112+
-- go.mod --
113+
module unused.mod
114+
115+
go 1.18
116+
117+
-- external.go --
118+
package external
119+
120+
func External(z int) //@codeaction("refactor.rewrite", "z", "z", recursive)
121+
122+
func _() {
123+
External(1)
124+
}
125+
`
126+
Run(t, files, func(t *testing.T, env *Env) {
127+
env.OpenFile("external.go")
128+
actions, err := env.Editor.CodeAction(env.Ctx, env.RegexpSearch("external.go", "z"), nil)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
132+
if len(actions) > 0 {
133+
t.Errorf("CodeAction(): got %d code actions, want 0", len(actions))
134+
}
135+
})
136+
}

0 commit comments

Comments
 (0)