Skip to content

Commit f29bdf1

Browse files
committed
internal/lsp: add suggested fix for unusedparams analysis
Suggest replacing the unused function parameter with _. Updates golang/go#36602 Change-Id: I53738e65907b8a4b4857dd8954f1477a043cf442 Reviewed-on: https://go-review.googlesource.com/c/tools/+/374254 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 7424a4d commit f29bdf1

File tree

6 files changed

+122
-5
lines changed

6 files changed

+122
-5
lines changed

internal/lsp/analysis/unusedparams/testdata/src/a/a.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 The Go Authors. All rights reserved.
1+
// Copyright 2022 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"net/http"
11+
)
12+
13+
type parent interface {
14+
n(f bool)
15+
}
16+
17+
type yuh struct {
18+
a int
19+
}
20+
21+
func (y *yuh) n(f bool) {
22+
for i := 0; i < 10; i++ {
23+
fmt.Println(i)
24+
}
25+
}
26+
27+
func a(i1 int, _ int, i3 int) int { // want "potentially unused parameter: 'i2'"
28+
i3 += i1
29+
_ = func(_ int) int { // want "potentially unused parameter: 'z'"
30+
_ = 1
31+
return 1
32+
}
33+
return i3
34+
}
35+
36+
func b(_ bytes.Buffer) { // want "potentially unused parameter: 'c'"
37+
_ = 1
38+
}
39+
40+
func z(_ http.ResponseWriter, _ *http.Request) { // want "potentially unused parameter: 'h'"
41+
fmt.Println("Before")
42+
}
43+
44+
func l(h http.Handler) http.Handler {
45+
return http.HandlerFunc(z)
46+
}
47+
48+
func mult(a, _ int) int { // want "potentially unused parameter: 'b'"
49+
a += 1
50+
return a
51+
}
52+
53+
func y(a int) {
54+
panic("yo")
55+
}

internal/lsp/analysis/unusedparams/testdata/src/typeparams/typeparams.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Go Authors. All rights reserved.
1+
// Copyright 2022 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package typeparams
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"net/http"
11+
)
12+
13+
type parent[T any] interface {
14+
n(f T)
15+
}
16+
17+
type yuh[T any] struct {
18+
a T
19+
}
20+
21+
func (y *yuh[int]) n(f bool) {
22+
for i := 0; i < 10; i++ {
23+
fmt.Println(i)
24+
}
25+
}
26+
27+
func a[T comparable](i1 int, _ T, i3 int) int { // want "potentially unused parameter: 'i2'"
28+
i3 += i1
29+
_ = func(_ int) int { // want "potentially unused parameter: 'z'"
30+
_ = 1
31+
return 1
32+
}
33+
return i3
34+
}
35+
36+
func b[T any](_ bytes.Buffer) { // want "potentially unused parameter: 'c'"
37+
_ = 1
38+
}
39+
40+
func z[T http.ResponseWriter](_ T, _ *http.Request) { // want "potentially unused parameter: 'h'"
41+
fmt.Println("Before")
42+
}
43+
44+
func l(h http.Handler) http.Handler {
45+
return http.HandlerFunc(z[http.ResponseWriter])
46+
}
47+
48+
func mult(a, _ int) int { // want "potentially unused parameter: 'b'"
49+
a += 1
50+
return a
51+
}
52+
53+
func y[T any](a T) {
54+
panic("yo")
55+
}

internal/lsp/analysis/unusedparams/unusedparams.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,20 @@ func run(pass *analysis.Pass) (interface{}, error) {
131131
start, end = u.ident.Pos(), u.ident.End()
132132
}
133133
// TODO(golang/go#36602): Add suggested fixes to automatically
134-
// remove the unused parameter. To start, just remove it from the
135-
// function declaration. Later, remove it from every use of this
134+
// remove the unused parameter from every use of this
136135
// function.
137136
pass.Report(analysis.Diagnostic{
138137
Pos: start,
139138
End: end,
140139
Message: fmt.Sprintf("potentially unused parameter: '%s'", u.ident.Name),
140+
SuggestedFixes: []analysis.SuggestedFix{{
141+
Message: `Replace with "_"`,
142+
TextEdits: []analysis.TextEdit{{
143+
Pos: u.ident.Pos(),
144+
End: u.ident.End(),
145+
NewText: []byte("_"),
146+
}},
147+
}},
141148
})
142149
}
143150
})

internal/lsp/analysis/unusedparams/unusedparams_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ func Test(t *testing.T) {
1818
if typeparams.Enabled {
1919
tests = append(tests, "typeparams")
2020
}
21-
analysistest.Run(t, testdata, unusedparams.Analyzer, tests...)
21+
analysistest.RunWithSuggestedFixes(t, testdata, unusedparams.Analyzer, tests...)
2222
}

0 commit comments

Comments
 (0)