Skip to content

Commit 1159e2c

Browse files
committed
feat: add more tests for the const_is_empty lint
Suggested by @xFrednet and @matthiaskrgr.
1 parent 2884970 commit 1159e2c

File tree

2 files changed

+100
-26
lines changed

2 files changed

+100
-26
lines changed

tests/ui/const_is_empty.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#![feature(inline_const)]
12
#![warn(clippy::const_is_empty)]
3+
#![allow(clippy::needless_late_init, unused_must_use)]
24

35
fn test_literal() {
46
if "".is_empty() {
@@ -99,3 +101,69 @@ fn main() {
99101
let _ = b"".is_empty();
100102
//~^ ERROR: this expression always evaluates to true
101103
}
104+
105+
fn str_from_arg(var: &str) {
106+
var.is_empty();
107+
// Do not lint, we know nothiny about var
108+
}
109+
110+
fn update_str() {
111+
let mut value = "duck";
112+
value = "penguin";
113+
114+
let _ = value.is_empty();
115+
// Do not lint since value is mutable
116+
}
117+
118+
fn macros() {
119+
// Content from Macro
120+
let file = include_str!("const_is_empty.rs");
121+
let _ = file.is_empty();
122+
// No lint because initializer comes from a macro result
123+
124+
let var = env!("PATH");
125+
let _ = var.is_empty();
126+
// No lint because initializer comes from a macro result
127+
}
128+
129+
fn conditional_value() {
130+
let value;
131+
132+
if true {
133+
value = "hey";
134+
} else {
135+
value = "hej";
136+
}
137+
138+
let _ = value.is_empty();
139+
// Do not lint, current constant folding is too simple to detect this
140+
}
141+
142+
fn cfg_conditioned() {
143+
#[cfg(test)]
144+
let val = "";
145+
#[cfg(not(test))]
146+
let val = "foo";
147+
148+
let _ = val.is_empty();
149+
// Do not lint, value depend on a #[cfg(…)] directive
150+
}
151+
152+
fn not_cfg_conditioned() {
153+
let val = "";
154+
#[cfg(not(target_os = "inexistent"))]
155+
let _ = val.is_empty();
156+
//~^ ERROR: this expression always evaluates to true
157+
}
158+
159+
const fn const_rand() -> &'static str {
160+
"17"
161+
}
162+
163+
fn const_expressions() {
164+
let _ = const { if true { "1" } else { "2" } }.is_empty();
165+
// Do not lint, we do not recurse into boolean expressions
166+
167+
let _ = const_rand().is_empty();
168+
// Do not lint, we do not recurse into functions
169+
}

tests/ui/const_is_empty.stderr

Lines changed: 32 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:6:8
33
|
44
LL | if "".is_empty() {
55
| ^^^^^^^^^^^^^
@@ -8,148 +8,154 @@ 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:9: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:15: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:18: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:35: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:38: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:60: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:62: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:64: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:66: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:68: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:70: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:72: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:74: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:76: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:78: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:80: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:82: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:84: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:86: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:88: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:94: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:97: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:99: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:101: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 true
155+
--> tests/ui/const_is_empty.rs:155:13
156+
|
157+
LL | let _ = val.is_empty();
158+
| ^^^^^^^^^^^^^^
159+
160+
error: aborting due to 26 previous errors
155161

0 commit comments

Comments
 (0)