Add more examples and comments to redundant_as_str test

This commit is contained in:
Dev381 2023-09-18 16:32:22 -04:00
parent f2ab16eac1
commit 00ca47b97d
3 changed files with 20 additions and 27 deletions

View File

@ -1,9 +0,0 @@
#![warn(clippy::redundant_as_str)]
fn main() {
let _redundant = "Hello, world!".to_owned().as_bytes();
let _redundant = "Hello, world!".to_owned().is_empty();
let _ok = "Hello, world!".to_owned().as_bytes();
let _ok = "Hello, world!".to_owned().is_empty();
}

View File

@ -1,9 +1,20 @@
#![warn(clippy::redundant_as_str)]
fn main() {
let _redundant = "Hello, world!".to_owned().as_str().as_bytes();
let _redundant = "Hello, world!".to_owned().as_str().is_empty();
let string = "Hello, world!".to_owned();
let _ok = "Hello, world!".to_owned().as_bytes();
let _ok = "Hello, world!".to_owned().is_empty();
// These methods are redundant and the `as_str` can be removed.
let _redundant = string.as_str().as_bytes();
let _redundant = string.as_str().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 _no_as_str = borrowed_str.as_bytes();
let _no_as_str = borrowed_str.is_empty();
}

View File

@ -1,17 +1,8 @@
error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
--> $DIR/redundant_as_str.rs:4:49
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `!`
--> $DIR/redundant_as_str.rs:17:35
|
LL | let _redundant = "Hello, world!".to_owned().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)]`
LL | let borrowed_str = "Hello, world"!
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
--> $DIR/redundant_as_str.rs:5:49
|
LL | let _redundant = "Hello, world!".to_owned().as_str().is_empty();
| ^^^^^^^^^^^^^^^^^ help: try: `is_empty`
error: aborting due to 2 previous errors
error: aborting due to previous error