golang/go

x/tools/gopls: CodeAction(refactor.rewrite.removeUnusedParam) preserves unnecessary expressions

Opened this issue · 2 comments

At L131 of PatchSet 1 of https://go.dev/cl/718500, the "Remove unused parameter" code action applied in sequence at the startLine parameters of markdownEscape, font, and printStyled, causes this input:

			for i, line := range text.Lines {
				fmt.Fprintf(&md, " ")
				printStyled(&md, line, i == 0)
			}

to be transformed to this suboptimal output:

			for i, line := range text.Lines {
				fmt.Fprintf(&md, " ")
				var _ bool = i == 0               // yuck
				printStyled(&md, line)
			}

We should investigate why.

Oh, I think it's conservatively ensuring that no local variable (and/or import perhaps?) becomes unused. Deleting the line would trigger an "unused variable i" error. Perhaps we should simplify the expression to something like _ = i. Or we could remove the decl of i--though perhaps that would make sense as a separate analyzer that synergistically cleans up after the first one, assuming that users eventually run the analyzers till fixed point.