about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDev381 <49997896+Dev380@users.noreply.github.com>2023-09-18 16:36:10 -0400
committerDev381 <49997896+Dev380@users.noreply.github.com>2023-09-18 16:36:10 -0400
commit367ba9cd003e271dc4d7e354b1212fe3c60d3329 (patch)
treec4d7f479b978ec8a9f631b6f53e8bd1ccd83ed0b
parent00ca47b97d59ab3384c9786e5b38f0262f413384 (diff)
downloadrust-367ba9cd003e271dc4d7e354b1212fe3c60d3329.tar.gz
rust-367ba9cd003e271dc4d7e354b1212fe3c60d3329.zip
Add not redundant examples for redundant_as_str
-rw-r--r--tests/ui/redundant_as_str.fixed24
-rw-r--r--tests/ui/redundant_as_str.rs10
-rw-r--r--tests/ui/redundant_as_str.stderr19
3 files changed, 45 insertions, 8 deletions
diff --git a/tests/ui/redundant_as_str.fixed b/tests/ui/redundant_as_str.fixed
new file mode 100644
index 00000000000..a95ab2d25a2
--- /dev/null
+++ b/tests/ui/redundant_as_str.fixed
@@ -0,0 +1,24 @@
+#![warn(clippy::redundant_as_str)]
+
+fn main() {
+	let string = "Hello, world!".to_owned();
+
+	// These methods are redundant and the `as_str` can be removed.
+    let _redundant = string.as_bytes();
+    let _redundant = string.is_empty();
+
+    // These methods don't use `as_str` when they are redundant.
+    let _no_as_str = string.as_bytes();
+    let _no_as_str = string.is_empty();
+
+	// These methods are not redundant, and are equivelant to
+	// doing dereferencing the string and applying the method.
+    let _not_redundant = string.as_str().escape_unicode();
+    let _not_redundant = string.as_str().trim();
+    let _not_redundant = string.as_str().split_whitespace();
+
+	// These methods don't use `as_str` and are applied on a `str` directly.
+	let borrowed_str = "Hello, world!";
+    let _is_str = borrowed_str.as_bytes();
+    let _is_str = borrowed_str.is_empty();
+}
diff --git a/tests/ui/redundant_as_str.rs b/tests/ui/redundant_as_str.rs
index 35b6bdb3c5e..d5ccfe2effe 100644
--- a/tests/ui/redundant_as_str.rs
+++ b/tests/ui/redundant_as_str.rs
@@ -7,6 +7,10 @@ fn main() {
     let _redundant = string.as_str().as_bytes();
     let _redundant = string.as_str().is_empty();
 
+    // These methods don't use `as_str` when they are redundant.
+    let _no_as_str = string.as_bytes();
+    let _no_as_str = string.is_empty();
+
 	// These methods are not redundant, and are equivelant to
 	// doing dereferencing the string and applying the method.
     let _not_redundant = string.as_str().escape_unicode();
@@ -14,7 +18,7 @@ fn main() {
     let _not_redundant = string.as_str().split_whitespace();
 
 	// These methods don't use `as_str` and are applied on a `str` directly.
-	let borrowed_str = "Hello, world"!
-    let _no_as_str = borrowed_str.as_bytes();
-    let _no_as_str = borrowed_str.is_empty();
+	let borrowed_str = "Hello, world!";
+    let _is_str = borrowed_str.as_bytes();
+    let _is_str = borrowed_str.is_empty();
 }
diff --git a/tests/ui/redundant_as_str.stderr b/tests/ui/redundant_as_str.stderr
index e7da34650f2..0ea42a94a81 100644
--- a/tests/ui/redundant_as_str.stderr
+++ b/tests/ui/redundant_as_str.stderr
@@ -1,8 +1,17 @@
-error: expected one of `.`, `;`, `?`, `else`, or an operator, found `!`
-  --> $DIR/redundant_as_str.rs:17:35
+error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
+  --> $DIR/redundant_as_str.rs:7:29
    |
-LL |     let borrowed_str = "Hello, world"!
-   |                                      ^ expected one of `.`, `;`, `?`, `else`, or an operator
+LL |     let _redundant = string.as_str().as_bytes();
+   |                             ^^^^^^^^^^^^^^^^^ help: try: `as_bytes`
+   |
+   = note: `-D clippy::redundant-as-str` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::redundant_as_str)]`
+
+error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
+  --> $DIR/redundant_as_str.rs:8:29
+   |
+LL |     let _redundant = string.as_str().is_empty();
+   |                             ^^^^^^^^^^^^^^^^^ help: try: `is_empty`
 
-error: aborting due to previous error
+error: aborting due to 2 previous errors