Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useless-assert: support bool ops #91

Merged
merged 3 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ Currently the checker guards against assertion of the same variable:
❌ assert.Equal(t, tt.value, tt.value)
assert.ElementsMatch(t, users, users)
// And so on...
assert.True(t, num > num)
assert.False(t, num == num)
```

More complex cases are [open for contribution](CONTRIBUTING.md#useless-assert).
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions internal/checkers/useless_assert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package checkers

import (
"go/ast"

"golang.org/x/tools/go/analysis"

"github.com/Antonboom/testifylint/internal/analysisutil"
Expand All @@ -13,6 +15,8 @@ import (
// assert.Equal(t, tt.value, tt.value)
// assert.ElementsMatch(t, users, users)
// ...
// assert.True(t, num > num)
// assert.False(t, num == num)
//
// 2) Open for contribution...
type UselessAssert struct{}
Expand All @@ -22,6 +26,8 @@ func NewUselessAssert() UselessAssert { return UselessAssert{} }
func (UselessAssert) Name() string { return "useless-assert" }

func (checker UselessAssert) Check(pass *analysis.Pass, call *CallMeta) *analysis.Diagnostic {
var first, second ast.Node

switch call.Fn.NameFTrimmed {
case
"Contains",
Expand Down Expand Up @@ -55,14 +61,25 @@ func (checker UselessAssert) Check(pass *analysis.Pass, call *CallMeta) *analysi
"Subset",
"WithinDuration",
"YAMLEq":
default:
return nil
}
if len(call.Args) < 2 {
return nil
}
first, second = call.Args[0], call.Args[1]

case "True", "False":
if len(call.Args) < 1 {
return nil
}

if len(call.Args) < 2 {
be, ok := call.Args[0].(*ast.BinaryExpr)
if !ok {
return nil
}
first, second = be.X, be.Y

default:
return nil
}
first, second := call.Args[0], call.Args[1]

if analysisutil.NodeString(pass.Fset, first) == analysisutil.NodeString(pass.Fset, second) {
return newDiagnostic(checker.Name(), call, "asserting of the same variable", nil)
Expand Down
22 changes: 21 additions & 1 deletion internal/testgen/gen_useless_assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,27 @@ func (g UselessAssertTestsGenerator) TemplateData() any {
twoSideAssertions = append(twoSideAssertions,
Assertion{Fn: fn, Argsf: args, ReportMsgf: sameVarReport})
}

for _, args := range []string{
"num > num",
"num < num",
"num >= num",
"num <= num",
"num == num",
"num != num",
} {
for _, fn := range []string{"True", "False"} {
twoSideAssertions = append(twoSideAssertions,
Assertion{Fn: fn, Argsf: args, ReportMsgf: sameVarReport})
}
}

sort.Slice(twoSideAssertions, func(i, j int) bool {
return twoSideAssertions[i].Fn < twoSideAssertions[j].Fn
lhs, rhs := twoSideAssertions[i], twoSideAssertions[j]
if lhs.Fn == rhs.Fn {
return lhs.Argsf < rhs.Argsf
}
return lhs.Fn < rhs.Fn
})

return struct {
Expand Down Expand Up @@ -112,6 +131,7 @@ func {{ .CheckerName.AsTestName }}(t *testing.T) {
var err error
var elapsed time.Time
var str string
var num int
var tc testCase

// Invalid.
Expand Down
Loading