about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2024-02-19 23:30:17 +0100
committerSamuel Tardieu <sam@rfc1149.net>2024-02-26 08:51:28 +0100
commitdbfbd0e77fd12a46ac81d0ec6fca4012aad0ea2d (patch)
tree0327006424071a3033d6b352f7359e72ccec08a2 /tests
parentaa2c94e416d346341fe3a875160a7d064596f4d3 (diff)
downloadrust-dbfbd0e77fd12a46ac81d0ec6fca4012aad0ea2d.tar.gz
rust-dbfbd0e77fd12a46ac81d0ec6fca4012aad0ea2d.zip
feat: add `const_is_empty` lint
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/const_is_empty.rs52
-rw-r--r--tests/ui/const_is_empty.stderr124
2 files changed, 176 insertions, 0 deletions
diff --git a/tests/ui/const_is_empty.rs b/tests/ui/const_is_empty.rs
new file mode 100644
index 00000000000..d61cdbb0424
--- /dev/null
+++ b/tests/ui/const_is_empty.rs
@@ -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
+}
diff --git a/tests/ui/const_is_empty.stderr b/tests/ui/const_is_empty.stderr
new file mode 100644
index 00000000000..5a89e686242
--- /dev/null
+++ b/tests/ui/const_is_empty.stderr
@@ -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
+