about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/needless_as_bytes.fixed50
-rw-r--r--tests/ui/needless_as_bytes.rs50
-rw-r--r--tests/ui/needless_as_bytes.stderr29
3 files changed, 129 insertions, 0 deletions
diff --git a/tests/ui/needless_as_bytes.fixed b/tests/ui/needless_as_bytes.fixed
new file mode 100644
index 00000000000..042342311fd
--- /dev/null
+++ b/tests/ui/needless_as_bytes.fixed
@@ -0,0 +1,50 @@
+#![warn(clippy::needless_as_bytes)]
+#![allow(clippy::const_is_empty)]
+
+struct S;
+
+impl S {
+    fn as_bytes(&self) -> &[u8] {
+        &[]
+    }
+}
+
+fn main() {
+    if "some string".is_empty() {
+        //~^ needless_as_bytes
+        println!("len = {}", "some string".len());
+        //~^ needless_as_bytes
+    }
+
+    let s = String::from("yet another string");
+    if s.is_empty() {
+        //~^ needless_as_bytes
+        println!("len = {}", s.len());
+        //~^ needless_as_bytes
+    }
+
+    // Do not lint
+    let _ = S.as_bytes().is_empty();
+    let _ = S.as_bytes().len();
+    let _ = (&String::new() as &dyn AsBytes).as_bytes().len();
+    macro_rules! m {
+        (1) => {
+            ""
+        };
+        (2) => {
+            "".as_bytes()
+        };
+    }
+    m!(1).as_bytes().len();
+    m!(2).len();
+}
+
+pub trait AsBytes {
+    fn as_bytes(&self) -> &[u8];
+}
+
+impl AsBytes for String {
+    fn as_bytes(&self) -> &[u8] {
+        &[]
+    }
+}
diff --git a/tests/ui/needless_as_bytes.rs b/tests/ui/needless_as_bytes.rs
new file mode 100644
index 00000000000..c481e041e0a
--- /dev/null
+++ b/tests/ui/needless_as_bytes.rs
@@ -0,0 +1,50 @@
+#![warn(clippy::needless_as_bytes)]
+#![allow(clippy::const_is_empty)]
+
+struct S;
+
+impl S {
+    fn as_bytes(&self) -> &[u8] {
+        &[]
+    }
+}
+
+fn main() {
+    if "some string".as_bytes().is_empty() {
+        //~^ needless_as_bytes
+        println!("len = {}", "some string".as_bytes().len());
+        //~^ needless_as_bytes
+    }
+
+    let s = String::from("yet another string");
+    if s.as_bytes().is_empty() {
+        //~^ needless_as_bytes
+        println!("len = {}", s.as_bytes().len());
+        //~^ needless_as_bytes
+    }
+
+    // Do not lint
+    let _ = S.as_bytes().is_empty();
+    let _ = S.as_bytes().len();
+    let _ = (&String::new() as &dyn AsBytes).as_bytes().len();
+    macro_rules! m {
+        (1) => {
+            ""
+        };
+        (2) => {
+            "".as_bytes()
+        };
+    }
+    m!(1).as_bytes().len();
+    m!(2).len();
+}
+
+pub trait AsBytes {
+    fn as_bytes(&self) -> &[u8];
+}
+
+impl AsBytes for String {
+    fn as_bytes(&self) -> &[u8] {
+        &[]
+    }
+}
diff --git a/tests/ui/needless_as_bytes.stderr b/tests/ui/needless_as_bytes.stderr
new file mode 100644
index 00000000000..3391238a142
--- /dev/null
+++ b/tests/ui/needless_as_bytes.stderr
@@ -0,0 +1,29 @@
+error: needless call to `as_bytes()`
+  --> tests/ui/needless_as_bytes.rs:13:8
+   |
+LL |     if "some string".as_bytes().is_empty() {
+   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `is_empty()` can be called directly on strings: `"some string".is_empty()`
+   |
+   = note: `-D clippy::needless-as-bytes` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::needless_as_bytes)]`
+
+error: needless call to `as_bytes()`
+  --> tests/ui/needless_as_bytes.rs:15:30
+   |
+LL |         println!("len = {}", "some string".as_bytes().len());
+   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `len()` can be called directly on strings: `"some string".len()`
+
+error: needless call to `as_bytes()`
+  --> tests/ui/needless_as_bytes.rs:20:8
+   |
+LL |     if s.as_bytes().is_empty() {
+   |        ^^^^^^^^^^^^^^^^^^^^^^^ help: `is_empty()` can be called directly on strings: `s.is_empty()`
+
+error: needless call to `as_bytes()`
+  --> tests/ui/needless_as_bytes.rs:22:30
+   |
+LL |         println!("len = {}", s.as_bytes().len());
+   |                              ^^^^^^^^^^^^^^^^^^ help: `len()` can be called directly on strings: `s.len()`
+
+error: aborting due to 4 previous errors
+