How to configure vscode to show unnecessary (overspecified) generics in go?

64 views Asked by At

In the following code

package main

import "fmt"

func test[A, B any](a A, b B) {
    fmt.Printf("a: %v, b: %v", a, b)
}

func main() {
    test[string, int]("test", 1)
}

the explicit type specification when invoking the test method is unnecessary and overspecified. It would be sufficient to call test("test", 1") since the types can be inferred from the arguments.

Is is possible to configure VSCode to indicate this? Or is there a linter that would report this? I somehow recall that I had seen VSCode displaying unnecessary types specifications as greyed out text, but either I have messed up my configuration or this feature is gone.

This would be extremely helpful for more advanced cases, in particular since type inferrence in go is improving steadily, and code that has been written for older go versions might be simplified.

According to documentation the setting

    "gopls": {
        "ui.diagnostic.analyses": {
            "infertypeargs": true
        }
    }

should have caused a visual indication of the unused types. But this is not showing up for me.

1

There are 1 answers

0
grg On BEST ANSWER

Currently, this analyser is only available from code actions inside the unnecessary type arguments:

x/tools/gopls: infertypeargs no longer produces diagnostics #63821 is tracking the lack of a diagnostic message. Once this issue is resolved, the diagnostic message should reappear in VS Code.

infertypeargs is enabled by default so your configuration should not be necessary.