diff --git a/tests/ui/redundant_as_str.fixed b/tests/ui/redundant_as_str.fixed new file mode 100644 index 000000000000..a95ab2d25a2c --- /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 35b6bdb3c5e8..d5ccfe2effef 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 e7da34650f2c..0ea42a94a81e 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: aborting due to previous error +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 2 previous errors