Skip to content

Commit e08fcf7

Browse files
xzbdmwfindleyr
authored andcommitted
gopls/internal/analysis/undeclaredname: merge into CodeAction
This change removes the undeclaredname analyzer, and instead computes it directly from the protocol.QuickFix code action producer, follow the same structure in CL 617035. Also: - include return types for undeclared function - add basic return type test in missingfunction.txt, the major functionality should be the same as fromcall_returns.txt - add more cases in TypesFromContext, and corresponding tests. Fixes golang/go#47558 Change-Id: Ic79cf2f07a3baee60631bf0b8f5c2b9f4f4393d0 GitHub-Last-Rev: 33b78d7 GitHub-Pull-Request: #538 Reviewed-on: https://go-review.googlesource.com/c/tools/+/623156 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 0c01408 commit e08fcf7

File tree

27 files changed

+627
-415
lines changed

27 files changed

+627
-415
lines changed

gopls/doc/analyzers.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -842,26 +842,6 @@ Default: on.
842842

843843
Package documentation: [timeformat](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/timeformat)
844844

845-
<a id='undeclaredname'></a>
846-
## `undeclaredname`: suggested fixes for "undeclared name: <>"
847-
848-
849-
This checker provides suggested fixes for type errors of the
850-
type "undeclared name: <>". It will either insert a new statement,
851-
such as:
852-
853-
<> :=
854-
855-
or a new function declaration, such as:
856-
857-
func <>(inferred parameters) {
858-
panic("implement me!")
859-
}
860-
861-
Default: on.
862-
863-
Package documentation: [undeclaredname](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/undeclaredname)
864-
865845
<a id='unmarshal'></a>
866846
## `unmarshal`: report passing non-pointer or non-interface values to unmarshal
867847

gopls/doc/features/diagnostics.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,56 @@ func (f Foo) bar(s string, i int) string {
197197
}
198198
```
199199

200+
### `CreateUndeclared`: Create missing declaration for "undeclared name: X"
201+
202+
A Go compiler error "undeclared name: X" indicates that a variable or function is being used before
203+
it has been declared in the current scope. In this scenario, gopls offers a quick fix to create the declaration.
204+
205+
#### Declare a new variable
206+
207+
When you reference a variable that hasn't been declared:
208+
209+
```go
210+
func main() {
211+
x := 42
212+
min(x, y) // error: undefined: y
213+
}
214+
```
215+
216+
The quick fix would be:
217+
218+
```go
219+
func main() {
220+
x := 42
221+
y :=
222+
min(x, y)
223+
}
224+
```
225+
226+
#### Declare a new function
227+
228+
Similarly, if you call a function that hasn't been declared:
229+
230+
```go
231+
func main() {
232+
var s string
233+
s = doSomething(42) // error: undefined: doSomething
234+
}
235+
```
236+
237+
Gopls will insert a new function declaration below,
238+
inferring its type from the call:
239+
240+
```go
241+
func main() {
242+
var s string
243+
s = doSomething(42)
244+
}
245+
246+
func doSomething(i int) string {
247+
panic("unimplemented")
248+
}
249+
```
200250
<!--
201251
202252
dorky details and deletia:

gopls/internal/analysis/undeclaredname/doc.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/a.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/channels.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/consecutive_params.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/error_param.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/literals.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/operation.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

gopls/internal/analysis/undeclaredname/testdata/src/a/selector.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)