Skip to content

Commit 6aa6afc

Browse files
authored
Merge pull request #32 from Antonboom/fixes/non-standard-funcs
analysisutil.IsObj: don't use obj.Id() as uniq identifier
2 parents 208e9c7 + a806599 commit 6aa6afc

File tree

6 files changed

+105
-1
lines changed

6 files changed

+105
-1
lines changed

analyzer/analyzer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func TestTestifyLint(t *testing.T) {
3838
},
3939
},
4040
{dir: "ginkgo"},
41+
{
42+
dir: "not-std-funcs",
43+
flags: map[string]string{"enable-all": "true"},
44+
},
4145
{dir: "not-test-file"}, // By default, linter checks regular files too.
4246
{dir: "not-true-testify"}, // Linter ignores stretchr/testify's forks.
4347
{dir: "pkg-alias"},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package errors
2+
3+
func Is(err error, v int) bool {
4+
return false
5+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package notstdfuncs
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
myerrors "not-std-funcs/errors"
10+
)
11+
12+
func TestCustomStuff(t *testing.T) {
13+
var err error
14+
var errSentinel = errors.New("unexpected")
15+
var scores []float64
16+
17+
assert.Equal(t, nil, scores) // want "nil-compare: use assert\\.Nil"
18+
19+
assert.True(t, myerrors.Is(err, 2))
20+
assert.False(t, Is(err, errSentinel))
21+
assert.True(t, As(err, &err))
22+
assert.Equal(t, 3, len(scores))
23+
24+
require.True(t, myerrors.Is(err, 3))
25+
require.False(t, Is(err, errSentinel))
26+
require.True(t, As(err, &err))
27+
require.Equal(t, 4, len(scores))
28+
}
29+
30+
func Is(err, target error) bool {
31+
return true
32+
}
33+
34+
func As(err error, target any) bool {
35+
return false
36+
}
37+
38+
func len[T any](arr T) int {
39+
return 3
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package notstdfuncs
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
myerrors "not-std-funcs/errors"
10+
)
11+
12+
func TestCustomStuff(t *testing.T) {
13+
var err error
14+
var errSentinel = errors.New("unexpected")
15+
var scores []float64
16+
17+
assert.Nil(t, scores) // want "nil-compare: use assert\\.Nil"
18+
19+
assert.True(t, myerrors.Is(err, 2))
20+
assert.False(t, Is(err, errSentinel))
21+
assert.True(t, As(err, &err))
22+
assert.Equal(t, 3, len(scores))
23+
24+
require.True(t, myerrors.Is(err, 3))
25+
require.False(t, Is(err, errSentinel))
26+
require.True(t, As(err, &err))
27+
require.Equal(t, 4, len(scores))
28+
}
29+
30+
func Is(err, target error) bool {
31+
return true
32+
}
33+
34+
func As(err error, target any) bool {
35+
return false
36+
}
37+
38+
func len[T any](arr T) int {
39+
return 3
40+
}

internal/analysisutil/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ func IsObj(typesInfo *types.Info, expr ast.Expr, expected types.Object) bool {
3030
}
3131

3232
obj := typesInfo.ObjectOf(id)
33-
return obj.Id() == expected.Id()
33+
return obj == expected
3434
}

internal/analysisutil/object_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,18 @@ func TestIsObj(t *testing.T) {
114114
})
115115
}
116116
}
117+
118+
func TestIsObj_NamesakesFromDifferentPackages(t *testing.T) {
119+
lhs := types.NewFunc(token.NoPos, types.NewPackage("errors", "errors"), "Is", nil)
120+
rhs := types.NewFunc(token.NoPos, types.NewPackage("pkg/errors", "errors"), "Is", nil)
121+
122+
ident := new(ast.Ident)
123+
typesInfo := &types.Info{
124+
Defs: map[*ast.Ident]types.Object{
125+
ident: lhs,
126+
},
127+
}
128+
if analysisutil.IsObj(typesInfo, ident, rhs) {
129+
t.Fatalf("objects should not be equal")
130+
}
131+
}

0 commit comments

Comments
 (0)