mgechev / revive

🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
https://revive.run
MIT License
4.79k stars 278 forks source link

add-constant suggests invalid fix of replacing struct tags with string constants for anonymous structs #951

Closed flimzy closed 9 months ago

flimzy commented 9 months ago

Describe the bug

revive incorrectly suggests replacing duplicated struct tags with string constants, for tags found in anonymous struct literals.

Example file to reproduce:

package test

func x() {
    a := struct {
        X int `json:"x"`
    }{}

    b := struct {
        X int `json:"x"`
    }{}

    c := struct {
        X int `json:"x"`
    }{}

    _, _, _ = a, b, c
}

To Reproduce Steps to reproduce the behavior:

  1. I updated revive go install github.com/mgechev/revive@latest
  2. I run it with the following flags & configuration file:
# flags

revive -config config.toml .

Output:

test.go:13:9: string literal `json:"x"` appears, at least, 3 times, create a named constant for it
[rule.add-constant]

Expected behavior Struct tags in anonymous struct literals should be ignored by this rule, as they are for struct type definitions.

denisvmedia commented 9 months ago

Thanks for detailed reporting. Have you tried doing the same using var with a zero value instead of assignment? It should work the same way but shouldn't trigger a warning.

denisvmedia commented 9 months ago

954 should solve the issue

cc @flimzy @chavacava