Skip to content

Commit

Permalink
Suggest NotEmpty for Positive len (#124)
Browse files Browse the repository at this point in the history
* Suggest NotEmpty for Positive len

* fix doc & make doc, readme and tests consistent

---------

Co-authored-by: Anton Telyshev <[email protected]>
  • Loading branch information
mmorel-35 and Antonboom authored Jun 15, 2024
1 parent c1346cf commit 858de46
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ assert.NotEqual(t, 0, len(arr))
assert.NotEqualValues(t, 0, len(arr))
assert.Less(t, 0, len(arr))
assert.Greater(t, len(arr), 0)
assert.Positive(t, len(arr))
assert.NotZero(t, len(arr))
assert.NotEmpty(t, len(arr))

Expand Down
6 changes: 4 additions & 2 deletions analyzer/testdata/src/checkers-default/empty/empty_test.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func TestEmptyChecker(t *testing.T) {
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"

// Valid.
assert.NotEmpty(t, elems)
Expand Down
3 changes: 2 additions & 1 deletion internal/checkers/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
// assert.NotEqualValues(t, 0, len(arr))
// assert.Less(t, 0, len(arr))
// assert.Greater(t, len(arr), 0)
// assert.Positive(t, len(arr))
// assert.NotZero(t, len(arr))
// assert.NotEmpty(t, len(arr))
//
Expand Down Expand Up @@ -133,7 +134,7 @@ func (checker Empty) checkNotEmpty(pass *analysis.Pass, call *CallMeta) *analysi

a := call.Args[0]
switch call.Fn.NameFTrimmed {
case "NotZero", "NotEmpty":
case "NotZero", "NotEmpty", "Positive":
lenArg, ok := isBuiltinLenCall(pass, a)
if ok {
return newUseNotEmptyDiagnostic(a.Pos(), a.End(), lenArg)
Expand Down
3 changes: 2 additions & 1 deletion internal/testgen/gen_empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ func (g EmptyTestsGenerator) TemplateData() any {
{Fn: "NotEqual", Argsf: "0, len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "NotEqualValues", Argsf: "len(elems), 0", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "NotEqualValues", Argsf: "0, len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "Greater", Argsf: "len(elems), 0", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "Less", Argsf: "0, len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "Greater", Argsf: "len(elems), 0", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "Positive", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "NotZero", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
{Fn: "NotEmpty", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
},
Expand Down

0 comments on commit 858de46

Please sign in to comment.