Skip to content

Commit 5b5d57c

Browse files
tttoadgopherbot
authored andcommitted
gopls/codeaction: fix panic when removing unused parameters with syntax errors.
Fixes golang/go#70268 Change-Id: I46643e79fc5cc30dac16bf18ef085bae173774a4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/626895 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Sam Thanawalla <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 025b812 commit 5b5d57c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

gopls/internal/golang/inline_all.go

+15
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
112112
if err != nil {
113113
return nil, bug.Errorf("finding %s in %s: %v", ref.URI, refpkg.Metadata().ID, err)
114114
}
115+
115116
start, end, err := pgf.RangePos(ref.Range)
116117
if err != nil {
117118
return nil, err // e.g. invalid range
@@ -137,6 +138,20 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
137138
// use(func(...) { f(...) })
138139
return nil, fmt.Errorf("cannot inline: found non-call function reference %v", ref)
139140
}
141+
142+
// Heuristic: ignore references that overlap with type checker errors, as they may
143+
// lead to invalid results (see golang/go#70268).
144+
hasTypeErrors := false
145+
for _, typeErr := range refpkg.TypeErrors() {
146+
if call.Lparen <= typeErr.Pos && typeErr.Pos <= call.Rparen {
147+
hasTypeErrors = true
148+
}
149+
}
150+
151+
if hasTypeErrors {
152+
continue
153+
}
154+
140155
// Sanity check.
141156
if obj := refpkg.TypesInfo().ObjectOf(name); obj == nil ||
142157
obj.Name() != origDecl.Name.Name ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
This test verifies the remove of unused parameters in case of syntax errors.
2+
Issue golang/go#70268.
3+
4+
-- go.mod --
5+
module unused.mod
6+
7+
go 1.21
8+
9+
-- a/a.go --
10+
package a
11+
12+
func A(x, unused int) int { //@codeaction("unused", "unused", "refactor.rewrite.removeUnusedParam", a)
13+
return x
14+
}
15+
16+
-- b/b.go --
17+
package b
18+
19+
import "unused.mod/a"
20+
21+
func main(){
22+
a.A/*dsdd*/(/*cccc*/ 1,
23+
24+
25+
) //@diag(")", re"not enough arguments")
26+
}
27+
-- @a/a/a.go --
28+
package a
29+
30+
func A(x int) int { //@codeaction("unused", "unused", "refactor.rewrite.removeUnusedParam", a)
31+
return x
32+
}
33+

0 commit comments

Comments
 (0)