Skip to content

Commit

Permalink
feat: add more tests for the const_is_empty lint
Browse files Browse the repository at this point in the history
Suggested by @xFrednet.
  • Loading branch information
samueltardieu committed Feb 19, 2024
1 parent c94ef81 commit 8c03ee1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 26 deletions.
38 changes: 38 additions & 0 deletions tests/ui/const_is_empty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::const_is_empty)]
#![allow(clippy::needless_late_init, unused_must_use)]

fn test_literal() {
if "".is_empty() {
Expand Down Expand Up @@ -99,3 +100,40 @@ fn main() {
let _ = b"".is_empty();
//~^ ERROR: this expression always evaluates to true
}

fn str_from_arg(var: &str) {
var.is_empty();
// Do not lint, we know nothiny about var
}

fn update_str() {
let mut value = "duck";
value = "penguin";

let _ = value.is_empty();
// Do not lint since value is mutable
}

fn macros() {
// Content from Macro
let file = include_str!("const_is_empty.rs");
let _ = file.is_empty();
//~^ ERROR: this expression always evaluates to false

let var = env!("PATH");
let _ = var.is_empty();
//~^ ERROR: this expression always evaluates to false
}

fn conditional_value() {
let value;

if true {
value = "hey";
} else {
value = "hej";
}

let _ = value.is_empty();
// Do not lint, current constant folding is too simple to detect this
}
64 changes: 38 additions & 26 deletions tests/ui/const_is_empty.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this expression always evaluates to true
--> tests/ui/const_is_empty.rs:4:8
--> tests/ui/const_is_empty.rs:5:8
|
LL | if "".is_empty() {
| ^^^^^^^^^^^^^
Expand All @@ -8,148 +8,160 @@ LL | if "".is_empty() {
= 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
--> tests/ui/const_is_empty.rs:8:8
|
LL | if "foobar".is_empty() {
| ^^^^^^^^^^^^^^^^^^^

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

error: aborting due to 25 previous errors
error: this expression always evaluates to false
--> tests/ui/const_is_empty.rs:120:13
|
LL | let _ = file.is_empty();
| ^^^^^^^^^^^^^^^

error: this expression always evaluates to false
--> tests/ui/const_is_empty.rs:124:13
|
LL | let _ = var.is_empty();
| ^^^^^^^^^^^^^^

error: aborting due to 27 previous errors

0 comments on commit 8c03ee1

Please sign in to comment.