mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 22:50:56 +00:00
Add not redundant examples for redundant_as_str
This commit is contained in:
parent
00ca47b97d
commit
367ba9cd00
3 changed files with 45 additions and 8 deletions
24
tests/ui/redundant_as_str.fixed
Normal file
24
tests/ui/redundant_as_str.fixed
Normal file
|
@ -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();
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue