Skip to content

Commit

Permalink
gopls/internal/lsp: fix code action panic on params of external funcs
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Oct 26, 2023
1 parent ff1953b commit 080c202
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gopls/internal/lsp/code_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ func canRemoveParameter(pkg source.Package, pgf *source.ParsedGoFile, rng protoc
return false
}

if info.Decl.Body == nil {
return false // external function
}

if len(info.Field.Names) == 0 {
return true // no names => field is unused
}
Expand Down
33 changes: 33 additions & 0 deletions gopls/internal/regtest/misc/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,36 @@ func Foo() error {
env.AfterChange(NoDiagnostics(ForFile("main.go")))
})
}

func TestUnusedParameter_Issue63755(t *testing.T) {
// This test verifies the fix for #63755, where codeActions panicked on parameters
// of functions with no function body.

// We should not detect parameters as unused for external functions.

const files = `
-- go.mod --
module unused.mod
go 1.18
-- external.go --
package external
func External(z int) //@codeaction("refactor.rewrite", "z", "z", recursive)
func _() {
External(1)
}
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("external.go")
actions, err := env.Editor.CodeAction(env.Ctx, env.RegexpSearch("external.go", "z"), nil)
if err != nil {
t.Fatal(err)
}
if len(actions) > 0 {
t.Errorf("CodeAction(): got %d code actions, want 0", len(actions))
}
})
}

0 comments on commit 080c202

Please sign in to comment.