feat: add const_is_empty lint
This commit is contained in:
parent
aa2c94e416
commit
dbfbd0e77f
6 changed files with 252 additions and 1 deletions
52
tests/ui/const_is_empty.rs
Normal file
52
tests/ui/const_is_empty.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#![warn(clippy::const_is_empty)]
|
||||
|
||||
fn test_literal() {
|
||||
if "".is_empty() {
|
||||
//~^ERROR: this expression always evaluates to true
|
||||
}
|
||||
if "foobar".is_empty() {
|
||||
//~^ERROR: this expression always evaluates to false
|
||||
}
|
||||
}
|
||||
|
||||
fn test_byte_literal() {
|
||||
if b"".is_empty() {
|
||||
//~^ERROR: this expression always evaluates to true
|
||||
}
|
||||
if b"foobar".is_empty() {
|
||||
//~^ERROR: this expression always evaluates to false
|
||||
}
|
||||
}
|
||||
|
||||
fn test_no_mut() {
|
||||
let mut empty = "";
|
||||
if empty.is_empty() {
|
||||
// No lint because it is mutable
|
||||
}
|
||||
}
|
||||
|
||||
fn test_propagated() {
|
||||
let empty = "";
|
||||
let non_empty = "foobar";
|
||||
let empty2 = empty;
|
||||
let non_empty2 = non_empty;
|
||||
if empty2.is_empty() {
|
||||
//~^ERROR: this expression always evaluates to true
|
||||
}
|
||||
if non_empty2.is_empty() {
|
||||
//~^ERROR: this expression always evaluates to false
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let value = "foobar";
|
||||
let _ = value.is_empty();
|
||||
//~^ ERROR: this expression always evaluates to false
|
||||
let x = value;
|
||||
let _ = x.is_empty();
|
||||
//~^ ERROR: this expression always evaluates to false
|
||||
let _ = "".is_empty();
|
||||
//~^ ERROR: this expression always evaluates to true
|
||||
let _ = b"".is_empty();
|
||||
//~^ ERROR: this expression always evaluates to true
|
||||
}
|
||||
124
tests/ui/const_is_empty.stderr
Normal file
124
tests/ui/const_is_empty.stderr
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
error: this expression always evaluates to true
|
||||
--> tests/ui/const_is_empty.rs:4:8
|
||||
|
|
||||
LL | if "".is_empty() {
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:4:8
|
||||
|
|
||||
LL | if "".is_empty() {
|
||||
| ^^
|
||||
= note: `-D clippy::const-is-empty` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::const_is_empty)]`
|
||||
|
||||
error: this expression always evaluates to false
|
||||
--> tests/ui/const_is_empty.rs:7:8
|
||||
|
|
||||
LL | if "foobar".is_empty() {
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:7:8
|
||||
|
|
||||
LL | if "foobar".is_empty() {
|
||||
| ^^^^^^^^
|
||||
|
||||
error: this expression always evaluates to true
|
||||
--> tests/ui/const_is_empty.rs:13:8
|
||||
|
|
||||
LL | if b"".is_empty() {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:13:8
|
||||
|
|
||||
LL | if b"".is_empty() {
|
||||
| ^^^
|
||||
|
||||
error: this expression always evaluates to false
|
||||
--> tests/ui/const_is_empty.rs:16:8
|
||||
|
|
||||
LL | if b"foobar".is_empty() {
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:16:8
|
||||
|
|
||||
LL | if b"foobar".is_empty() {
|
||||
| ^^^^^^^^^
|
||||
|
||||
error: this expression always evaluates to true
|
||||
--> tests/ui/const_is_empty.rs:33:8
|
||||
|
|
||||
LL | if empty2.is_empty() {
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:29:17
|
||||
|
|
||||
LL | let empty = "";
|
||||
| ^^
|
||||
|
||||
error: this expression always evaluates to false
|
||||
--> tests/ui/const_is_empty.rs:36:8
|
||||
|
|
||||
LL | if non_empty2.is_empty() {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:30:21
|
||||
|
|
||||
LL | let non_empty = "foobar";
|
||||
| ^^^^^^^^
|
||||
|
||||
error: this expression always evaluates to false
|
||||
--> tests/ui/const_is_empty.rs:43:13
|
||||
|
|
||||
LL | let _ = value.is_empty();
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:42:17
|
||||
|
|
||||
LL | let value = "foobar";
|
||||
| ^^^^^^^^
|
||||
|
||||
error: this expression always evaluates to false
|
||||
--> tests/ui/const_is_empty.rs:46:13
|
||||
|
|
||||
LL | let _ = x.is_empty();
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:42:17
|
||||
|
|
||||
LL | let value = "foobar";
|
||||
| ^^^^^^^^
|
||||
|
||||
error: this expression always evaluates to true
|
||||
--> tests/ui/const_is_empty.rs:48:13
|
||||
|
|
||||
LL | let _ = "".is_empty();
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:48:13
|
||||
|
|
||||
LL | let _ = "".is_empty();
|
||||
| ^^
|
||||
|
||||
error: this expression always evaluates to true
|
||||
--> tests/ui/const_is_empty.rs:50:13
|
||||
|
|
||||
LL | let _ = b"".is_empty();
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
note: because its initialization value is constant
|
||||
--> tests/ui/const_is_empty.rs:50:13
|
||||
|
|
||||
LL | let _ = b"".is_empty();
|
||||
| ^^^
|
||||
|
||||
error: aborting due to 10 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue