vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

"foo declared but not used" use gray color, not red

Open guettli opened this issue 2 years ago • 12 comments

Is your feature request related to a problem? Please describe.

When I write code, then I often see red underlined variables of variables my code does not use yet.

image

But of course, this variable is not used, since I am writing line after line (I know 10x developers can write several lines at once, but I am just a 0.5x developer).

I like the fact that I see the unused variable highlighted, but I think the red color does not fit here.

Describe the solution you'd like

It would be more convenient and reduce cognitive load if "declared but not used" warning would be less highlighted.

Maybe use gray color?

Describe alternatives you've considered

There are many alternative to do less strong highlighting.

Additional context

I am new to go, and if there is something red, then I think there is something wrong with the code.

guettli avatar Jun 10 '22 06:06 guettli

cc @findleyr

gopls is really fast at detecting errors and putting diagnostics. There may be some work to do to improve the user experience for diagnostics on code that is actively being edited.

suzmue avatar Jun 16 '22 17:06 suzmue

gopls is great. While writing code you almost always have unused variables, since first write the line which creates the variable, and then in the next moment you will use the variable. Between both moments the variable is unused. That's obvious.

How long does it take on avarage for a developer to write the first line (create variable) until he/she uses the variable? May the nused var should be no error for the first N seconds?

guettli avatar Jun 24 '22 06:06 guettli

+1, red color confuses with a real error

gennad avatar Jun 26 '22 19:06 gennad

+1, red color confuses with a real error

Unused variables are real errors in Go though. From the spec (https://go.dev/ref/spec#Variable_declarations): "Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used."

The Go compiler enforces this implementation restriction: https://go.dev/play/p/VCM7-h1LFoU

So I don't think we can reasonably change the color away from red. If we were to do so, then we would break the invariant that a program without red squiggles compiles.

findleyr avatar Jun 26 '22 20:06 findleyr

+1, red color confuses with a real error

Unused variables are real errors in Go though. From the spec (https://go.dev/ref/spec#Variable_declarations): "Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used."

The Go compiler enforces this implementation restriction: https://go.dev/play/p/VCM7-h1LFoU

So I don't think we can reasonably change the color away from red. If we were to do so, then we would break the invariant that a program without red squiggles compiles.

Hmmm what about two kinds of red? A normal red and a light red?

guettli avatar Jun 30 '22 08:06 guettli

There is a mechanism to do this. go/types has a notion of a 'soft' error: https://pkg.go.dev/go/types#Error (which is approximately an error which is optional for the implementation). The errors we're talking about are soft, so we could use this.

But I don't think we have this level of control over the color in the LSP.

findleyr avatar Jun 30 '22 10:06 findleyr

Change https://go.dev/cl/415499 mentions this issue: internal/lsp: add unnecessary tags for unused vars and imports

gopherbot avatar Jul 22 '22 21:07 gopherbot

@suzmue is there anything which I can do to help? It would be great to see less red underlined code while typing Go code.

guettli avatar Mar 01 '23 10:03 guettli

FWIW, the diagnostic tags added in https://go.dev/cl/415499 do work in neovim, and I've had the opposite reaction: I keep getting confused because my code doesn't compile even though there are no errors.

So now we're both bothered :)

findleyr avatar Mar 02 '23 15:03 findleyr

FWIW, the diagnostic tags added in https://go.dev/cl/415499 do work in neovim, and I've had the opposite reaction: I keep getting confused because my code doesn't compile even though there are no errors.

So now we're both bothered :)

I can understand this. If there is no visual indicator, then it is confusing.

I think it would be great if unused variables get underlined in yellow color.

Is this possible (in vscode)?

guettli avatar Mar 02 '23 16:03 guettli

I agree red error indication is appropriate but it would be nice if the editor could perhaps hold off flagging it until the user either saved, or moved the cursor out of the function being edited, or something like that.

james-stocks avatar Apr 07 '23 19:04 james-stocks

New go learner here, I think a different color/shade would increase productivity since I don't know what the error is without hovering. And red line is jarring when I'm just typing out my code. I'm constantly in a state of why is my code erroring when this should be right.

octopushugger avatar Sep 18 '23 21:09 octopushugger