Skip to content

Commit 8c03ee1

Browse files
committed
feat: add more tests for the const_is_empty lint
Suggested by @xFrednet.
1 parent c94ef81 commit 8c03ee1

File tree

2 files changed

+76
-26
lines changed

2 files changed

+76
-26
lines changed

tests/ui/const_is_empty.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::const_is_empty)]
2+
#![allow(clippy::needless_late_init, unused_must_use)]
23

34
fn test_literal() {
45
if "".is_empty() {
@@ -99,3 +100,40 @@ fn main() {
99100
let _ = b"".is_empty();
100101
//~^ ERROR: this expression always evaluates to true
101102
}
103+
104+
fn str_from_arg(var: &str) {
105+
var.is_empty();
106+
// Do not lint, we know nothiny about var
107+
}
108+
109+
fn update_str() {
110+
let mut value = "duck";
111+
value = "penguin";
112+
113+
let _ = value.is_empty();
114+
// Do not lint since value is mutable
115+
}
116+
117+
fn macros() {
118+
// Content from Macro
119+
let file = include_str!("const_is_empty.rs");
120+
let _ = file.is_empty();
121+
//~^ ERROR: this expression always evaluates to false
122+
123+
let var = env!("PATH");
124+
let _ = var.is_empty();
125+
//~^ ERROR: this expression always evaluates to false
126+
}
127+
128+
fn conditional_value() {
129+
let value;
130+
131+
if true {
132+
value = "hey";
133+
} else {
134+
value = "hej";
135+
}
136+
137+
let _ = value.is_empty();
138+
// Do not lint, current constant folding is too simple to detect this
139+
}

tests/ui/const_is_empty.stderr

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this expression always evaluates to true
2-
--> tests/ui/const_is_empty.rs:4:8
2+
--> tests/ui/const_is_empty.rs:5:8
33
|
44
LL | if "".is_empty() {
55
| ^^^^^^^^^^^^^
@@ -8,148 +8,160 @@ LL | if "".is_empty() {
88
= help: to override `-D warnings` add `#[allow(clippy::const_is_empty)]`
99

1010
error: this expression always evaluates to false
11-
--> tests/ui/const_is_empty.rs:7:8
11+
--> tests/ui/const_is_empty.rs:8:8
1212
|
1313
LL | if "foobar".is_empty() {
1414
| ^^^^^^^^^^^^^^^^^^^
1515

1616
error: this expression always evaluates to true
17-
--> tests/ui/const_is_empty.rs:13:8
17+
--> tests/ui/const_is_empty.rs:14:8
1818
|
1919
LL | if b"".is_empty() {
2020
| ^^^^^^^^^^^^^^
2121

2222
error: this expression always evaluates to false
23-
--> tests/ui/const_is_empty.rs:16:8
23+
--> tests/ui/const_is_empty.rs:17:8
2424
|
2525
LL | if b"foobar".is_empty() {
2626
| ^^^^^^^^^^^^^^^^^^^^
2727

2828
error: this expression always evaluates to true
29-
--> tests/ui/const_is_empty.rs:33:8
29+
--> tests/ui/const_is_empty.rs:34:8
3030
|
3131
LL | if empty2.is_empty() {
3232
| ^^^^^^^^^^^^^^^^^
3333

3434
error: this expression always evaluates to false
35-
--> tests/ui/const_is_empty.rs:36:8
35+
--> tests/ui/const_is_empty.rs:37:8
3636
|
3737
LL | if non_empty2.is_empty() {
3838
| ^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: this expression always evaluates to true
41-
--> tests/ui/const_is_empty.rs:58:13
41+
--> tests/ui/const_is_empty.rs:59:13
4242
|
4343
LL | let _ = EMPTY_STR.is_empty();
4444
| ^^^^^^^^^^^^^^^^^^^^
4545

4646
error: this expression always evaluates to false
47-
--> tests/ui/const_is_empty.rs:60:13
47+
--> tests/ui/const_is_empty.rs:61:13
4848
|
4949
LL | let _ = NON_EMPTY_STR.is_empty();
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^
5151

5252
error: this expression always evaluates to true
53-
--> tests/ui/const_is_empty.rs:62:13
53+
--> tests/ui/const_is_empty.rs:63:13
5454
|
5555
LL | let _ = EMPTY_BSTR.is_empty();
5656
| ^^^^^^^^^^^^^^^^^^^^^
5757

5858
error: this expression always evaluates to false
59-
--> tests/ui/const_is_empty.rs:64:13
59+
--> tests/ui/const_is_empty.rs:65:13
6060
|
6161
LL | let _ = NON_EMPTY_BSTR.is_empty();
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6363

6464
error: this expression always evaluates to true
65-
--> tests/ui/const_is_empty.rs:66:13
65+
--> tests/ui/const_is_empty.rs:67:13
6666
|
6767
LL | let _ = EMPTY_ARRAY.is_empty();
6868
| ^^^^^^^^^^^^^^^^^^^^^^
6969

7070
error: this expression always evaluates to true
71-
--> tests/ui/const_is_empty.rs:68:13
71+
--> tests/ui/const_is_empty.rs:69:13
7272
|
7373
LL | let _ = EMPTY_ARRAY_REPEAT.is_empty();
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7575

7676
error: this expression always evaluates to true
77-
--> tests/ui/const_is_empty.rs:70:13
77+
--> tests/ui/const_is_empty.rs:71:13
7878
|
7979
LL | let _ = EMPTY_U8_SLICE.is_empty();
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8181

8282
error: this expression always evaluates to false
83-
--> tests/ui/const_is_empty.rs:72:13
83+
--> tests/ui/const_is_empty.rs:73:13
8484
|
8585
LL | let _ = NON_EMPTY_U8_SLICE.is_empty();
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8787

8888
error: this expression always evaluates to false
89-
--> tests/ui/const_is_empty.rs:74:13
89+
--> tests/ui/const_is_empty.rs:75:13
9090
|
9191
LL | let _ = NON_EMPTY_ARRAY.is_empty();
9292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
9393

9494
error: this expression always evaluates to false
95-
--> tests/ui/const_is_empty.rs:76:13
95+
--> tests/ui/const_is_empty.rs:77:13
9696
|
9797
LL | let _ = NON_EMPTY_ARRAY_REPEAT.is_empty();
9898
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9999

100100
error: this expression always evaluates to true
101-
--> tests/ui/const_is_empty.rs:78:13
101+
--> tests/ui/const_is_empty.rs:79:13
102102
|
103103
LL | let _ = EMPTY_REF_ARRAY.is_empty();
104104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
105105

106106
error: this expression always evaluates to false
107-
--> tests/ui/const_is_empty.rs:80:13
107+
--> tests/ui/const_is_empty.rs:81:13
108108
|
109109
LL | let _ = NON_EMPTY_REF_ARRAY.is_empty();
110110
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111111

112112
error: this expression always evaluates to true
113-
--> tests/ui/const_is_empty.rs:82:13
113+
--> tests/ui/const_is_empty.rs:83:13
114114
|
115115
LL | let _ = EMPTY_SLICE.is_empty();
116116
| ^^^^^^^^^^^^^^^^^^^^^^
117117

118118
error: this expression always evaluates to false
119-
--> tests/ui/const_is_empty.rs:84:13
119+
--> tests/ui/const_is_empty.rs:85:13
120120
|
121121
LL | let _ = NON_EMPTY_SLICE.is_empty();
122122
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
123123

124124
error: this expression always evaluates to false
125-
--> tests/ui/const_is_empty.rs:86:13
125+
--> tests/ui/const_is_empty.rs:87:13
126126
|
127127
LL | let _ = NON_EMPTY_SLICE_REPEAT.is_empty();
128128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
129129

130130
error: this expression always evaluates to false
131-
--> tests/ui/const_is_empty.rs:92:13
131+
--> tests/ui/const_is_empty.rs:93:13
132132
|
133133
LL | let _ = value.is_empty();
134134
| ^^^^^^^^^^^^^^^^
135135

136136
error: this expression always evaluates to false
137-
--> tests/ui/const_is_empty.rs:95:13
137+
--> tests/ui/const_is_empty.rs:96:13
138138
|
139139
LL | let _ = x.is_empty();
140140
| ^^^^^^^^^^^^
141141

142142
error: this expression always evaluates to true
143-
--> tests/ui/const_is_empty.rs:97:13
143+
--> tests/ui/const_is_empty.rs:98:13
144144
|
145145
LL | let _ = "".is_empty();
146146
| ^^^^^^^^^^^^^
147147

148148
error: this expression always evaluates to true
149-
--> tests/ui/const_is_empty.rs:99:13
149+
--> tests/ui/const_is_empty.rs:100:13
150150
|
151151
LL | let _ = b"".is_empty();
152152
| ^^^^^^^^^^^^^^
153153

154-
error: aborting due to 25 previous errors
154+
error: this expression always evaluates to false
155+
--> tests/ui/const_is_empty.rs:120:13
156+
|
157+
LL | let _ = file.is_empty();
158+
| ^^^^^^^^^^^^^^^
159+
160+
error: this expression always evaluates to false
161+
--> tests/ui/const_is_empty.rs:124:13
162+
|
163+
LL | let _ = var.is_empty();
164+
| ^^^^^^^^^^^^^^
165+
166+
error: aborting due to 27 previous errors
155167

0 commit comments

Comments
 (0)