Skip to content

Commit 0bf7d2d

Browse files
committed
equal-values: fix false positive with any
When variables are both any, we should not report.
1 parent b789d10 commit 0bf7d2d

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

analyzer/testdata/src/checkers-default/equal-values/equal_values_test.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analyzer/testdata/src/checkers-default/equal-values/equal_values_test.go.golden

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func TestEqualValuesChecker(t *testing.T) {
4343
b []byte
4444
f func() bool
4545
)
46+
47+
var (
48+
anyInt any = 42
49+
anyFloat any = 42.0
50+
)
51+
4652
tlsConf := new(tls.Config)
4753

4854
// Invalid.
@@ -127,6 +133,8 @@ func TestEqualValuesChecker(t *testing.T) {
127133
assert.EqualValuesf(t, 2048, mm["Etype"], "msg with args %d %s", 42, "42")
128134
assert.EqualValues(t, req, dto)
129135
assert.EqualValuesf(t, req, dto, "msg with args %d %s", 42, "42")
136+
assert.EqualValues(t, anyInt, anyFloat)
137+
assert.EqualValuesf(t, anyInt, anyFloat, "msg with args %d %s", 42, "42")
130138
assert.EqualValues(t, req, reqWithTags)
131139
assert.EqualValuesf(t, req, reqWithTags, "msg with args %d %s", 42, "42")
132140
assert.EqualValues(t, S{"1"}, []string{"1"})

internal/checkers/equal_values.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@ func (checker EqualValues) Check(pass *analysis.Pass, call *CallMeta) *analysis.
4343
}
4444

4545
ft, st := pass.TypesInfo.TypeOf(first), pass.TypesInfo.TypeOf(second)
46-
if types.Identical(ft, st) {
47-
proposed := strings.TrimSuffix(assrn, "Values")
48-
return newUseFunctionDiagnostic(checker.Name(), call, proposed)
46+
if !types.Identical(ft, st) {
47+
return nil
4948
}
50-
return nil
49+
50+
switch ft.String() {
51+
case "any", "interface {}":
52+
// EqualValues is ok here.
53+
// Equal would check their types and would fail
54+
return nil
55+
}
56+
57+
proposed := strings.TrimSuffix(assrn, "Values")
58+
return newUseFunctionDiagnostic(checker.Name(), call, proposed)
5159
}

internal/testgen/gen_equal_values.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func (g EqualValuesTestsGenerator) TemplateData() any {
6767

6868
{Fn: "EqualValues", Argsf: `2048, mm["Etype"]`},
6969
{Fn: "EqualValues", Argsf: `req, dto`},
70+
{Fn: "EqualValues", Argsf: `anyInt, anyFloat`},
7071
{Fn: "EqualValues", Argsf: `req, reqWithTags`},
7172
{Fn: "EqualValues", Argsf: `S{"1"}, []string{"1"}`},
7273
{Fn: "EqualValues", Argsf: `f, (func())(nil)`},
@@ -131,6 +132,12 @@ func {{ .CheckerName.AsTestName }}(t *testing.T) {
131132
b []byte
132133
f func() bool
133134
)
135+
136+
var (
137+
anyInt any = 42
138+
anyFloat any = 42.0
139+
)
140+
134141
tlsConf := new(tls.Config)
135142
136143
// Invalid.

0 commit comments

Comments
 (0)