Skip to content

Commit 8ffbad9

Browse files
committed
empty: added empty and zero length detection
Fixes #108
1 parent 654e0ae commit 8ffbad9

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,15 @@ assert.Less(t, len(arr), 0)
225225
assert.Greater(t, 0, len(arr))
226226
assert.Less(t, len(arr), 1)
227227
assert.Greater(t, 1, len(arr))
228+
assert.Zero(t, len(arr))
229+
assert.Empty(t, len(arr))
228230

229231
assert.NotEqual(t, 0, len(arr))
230232
assert.NotEqualValues(t, 0, len(arr))
231233
assert.Less(t, 0, len(arr))
232234
assert.Greater(t, len(arr), 0)
235+
assert.NotZero(t, len(arr))
236+
assert.NotEmpty(t, len(arr))
233237

234238
235239
assert.Empty(t, arr)

analyzer/testdata/src/checkers-default/empty/empty_test.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analyzer/testdata/src/checkers-default/empty/empty_test.go.golden

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func TestEmptyChecker(t *testing.T) {
4444
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
4545
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
4646
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
47+
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
48+
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
49+
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
50+
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
4751

4852
// Valid.
4953
assert.Empty(t, elems)
@@ -65,6 +69,10 @@ func TestEmptyChecker(t *testing.T) {
6569
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
6670
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
6771
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
72+
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
73+
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
74+
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
75+
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
6876

6977
// Valid.
7078
assert.NotEmpty(t, elems)

internal/checkers/empty.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ import (
2121
// assert.Greater(t, 0, len(arr))
2222
// assert.Less(t, len(arr), 1)
2323
// assert.Greater(t, 1, len(arr))
24+
// assert.Zero(t, len(arr))
25+
// assert.Empty(t, len(arr))
2426
//
2527
// assert.NotEqual(t, 0, len(arr))
2628
// assert.NotEqualValues(t, 0, len(arr))
2729
// assert.Less(t, 0, len(arr))
2830
// assert.Greater(t, len(arr), 0)
31+
// assert.NotZero(t, len(arr))
32+
// assert.NotEmpty(t, len(arr))
2933
//
3034
// and requires
3135
//
@@ -56,10 +60,23 @@ func (checker Empty) checkEmpty(pass *analysis.Pass, call *CallMeta) *analysis.D
5660
)
5761
}
5862

63+
if len(call.Args) == 0 {
64+
return nil
65+
}
66+
67+
a := call.Args[0]
68+
switch call.Fn.NameFTrimmed {
69+
case "Zero", "Empty":
70+
lenArg, ok := isBuiltinLenCall(pass, a)
71+
if ok {
72+
return newUseEmptyDiagnostic(a.Pos(), a.End(), lenArg)
73+
}
74+
}
75+
5976
if len(call.Args) < 2 {
6077
return nil
6178
}
62-
a, b := call.Args[0], call.Args[1]
79+
b := call.Args[1]
6380

6481
switch call.Fn.NameFTrimmed {
6582
case "Len":
@@ -110,10 +127,23 @@ func (checker Empty) checkNotEmpty(pass *analysis.Pass, call *CallMeta) *analysi
110127
)
111128
}
112129

130+
if len(call.Args) == 0 {
131+
return nil
132+
}
133+
134+
a := call.Args[0]
135+
switch call.Fn.NameFTrimmed {
136+
case "NotZero", "NotEmpty":
137+
lenArg, ok := isBuiltinLenCall(pass, a)
138+
if ok {
139+
return newUseNotEmptyDiagnostic(a.Pos(), a.End(), lenArg)
140+
}
141+
}
142+
113143
if len(call.Args) < 2 {
114144
return nil
115145
}
116-
a, b := call.Args[0], call.Args[1]
146+
b := call.Args[1]
117147

118148
switch call.Fn.NameFTrimmed {
119149
case "NotEqual", "NotEqualValues":

internal/testgen/gen_empty.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func (g EmptyTestsGenerator) TemplateData() any {
5959
{Fn: "GreaterOrEqual", Argsf: "0, len(elems)", ReportMsgf: report, ProposedFn: "Empty", ProposedArgsf: "elems"},
6060
{Fn: "Less", Argsf: "len(elems), 1", ReportMsgf: report, ProposedFn: "Empty", ProposedArgsf: "elems"},
6161
{Fn: "Greater", Argsf: "1, len(elems)", ReportMsgf: report, ProposedFn: "Empty", ProposedArgsf: "elems"},
62+
{Fn: "Zero", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "Empty", ProposedArgsf: "elems"},
63+
{Fn: "Empty", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "Empty", ProposedArgsf: "elems"},
6264

6365
// Bullshit, but supported by the checker:
6466
// n < 0, n <= 0
@@ -84,6 +86,8 @@ func (g EmptyTestsGenerator) TemplateData() any {
8486
{Fn: "NotEqualValues", Argsf: "0, len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
8587
{Fn: "Greater", Argsf: "len(elems), 0", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
8688
{Fn: "Less", Argsf: "0, len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
89+
{Fn: "NotZero", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
90+
{Fn: "NotEmpty", Argsf: "len(elems)", ReportMsgf: report, ProposedFn: "NotEmpty", ProposedArgsf: "elems"},
8791
},
8892
ValidAssertions: []Assertion{
8993
{Fn: "NotEmpty", Argsf: "elems"},
@@ -161,7 +165,7 @@ func {{ .CheckerName.AsTestName }}(t *testing.T) {
161165
{{- range $ai, $assrn := $test.InvalidAssertions }}
162166
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }}
163167
{{- end }}
164-
168+
165169
// Valid.
166170
{{- range $ai, $assrn := $test.ValidAssertions }}
167171
{{ NewAssertionExpander.Expand $assrn "assert" "t" nil }}

0 commit comments

Comments
 (0)