x/tools/gopls/internal/analysis/unusedparams: "no such param" error when applying fix
Opened this issue · 1 comments
gopls' unusedparams analyzer is spuriously reporting a diagnostic for an unused parameter in a func literal even when the func value is address taken, in this case by being assigned to a global variable of a particular func type:
var g func(x, y int)
func _() {
g = func(x, y int) { // unused parameter: x
print(y)
}
}The existing logic is supposed to recognize when a func lit is assigned to a func-typed var whose type it must conform to, but clearly it has a bug.
Ah: aside from the assignment, all uses of var g (of which there are none) are in call position, so the func lit is considered not address-taken (Adding a call g(1, 2) doesn't change this, but adding print(g) does.) I suppose this is consistent: the parameter could indeed be removed, but the change to do that may be more complex.
The problem is not the package-level var; the same problem occurs locally:
func _() {
var g func(x, y int)
g = func(x, y int) { // unused parameter: x
print(y)
}
g(1, 2)
}In both cases, applying the fix fails with an error ("no param found"). I wondered if the problem was that removing the parameter x would require the type of var g to change, which could be a tricky edit. But even in the case where g is declared from the func lit using := like so, the quick fix fails:
func _() {
g := func(x, y int) { // unused parameter: x
print(y)
}
g(1, 2)
}So I think the diagnostic is working as intended, but there are bugs in the fix.