Skip to content

Commit 367ba9c

Browse files
committed
Add not redundant examples for redundant_as_str
1 parent 00ca47b commit 367ba9c

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

tests/ui/redundant_as_str.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![warn(clippy::redundant_as_str)]
2+
3+
fn main() {
4+
let string = "Hello, world!".to_owned();
5+
6+
// These methods are redundant and the `as_str` can be removed.
7+
let _redundant = string.as_bytes();
8+
let _redundant = string.is_empty();
9+
10+
// These methods don't use `as_str` when they are redundant.
11+
let _no_as_str = string.as_bytes();
12+
let _no_as_str = string.is_empty();
13+
14+
// These methods are not redundant, and are equivelant to
15+
// doing dereferencing the string and applying the method.
16+
let _not_redundant = string.as_str().escape_unicode();
17+
let _not_redundant = string.as_str().trim();
18+
let _not_redundant = string.as_str().split_whitespace();
19+
20+
// These methods don't use `as_str` and are applied on a `str` directly.
21+
let borrowed_str = "Hello, world!";
22+
let _is_str = borrowed_str.as_bytes();
23+
let _is_str = borrowed_str.is_empty();
24+
}

tests/ui/redundant_as_str.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ fn main() {
77
let _redundant = string.as_str().as_bytes();
88
let _redundant = string.as_str().is_empty();
99

10+
// These methods don't use `as_str` when they are redundant.
11+
let _no_as_str = string.as_bytes();
12+
let _no_as_str = string.is_empty();
13+
1014
// These methods are not redundant, and are equivelant to
1115
// doing dereferencing the string and applying the method.
1216
let _not_redundant = string.as_str().escape_unicode();
1317
let _not_redundant = string.as_str().trim();
1418
let _not_redundant = string.as_str().split_whitespace();
1519

1620
// These methods don't use `as_str` and are applied on a `str` directly.
17-
let borrowed_str = "Hello, world"!
18-
let _no_as_str = borrowed_str.as_bytes();
19-
let _no_as_str = borrowed_str.is_empty();
21+
let borrowed_str = "Hello, world!";
22+
let _is_str = borrowed_str.as_bytes();
23+
let _is_str = borrowed_str.is_empty();
2024
}

tests/ui/redundant_as_str.stderr

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `!`
2-
--> $DIR/redundant_as_str.rs:17:35
1+
error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
2+
--> $DIR/redundant_as_str.rs:7:29
33
|
4-
LL | let borrowed_str = "Hello, world"!
5-
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
4+
LL | let _redundant = string.as_str().as_bytes();
5+
| ^^^^^^^^^^^^^^^^^ help: try: `as_bytes`
6+
|
7+
= note: `-D clippy::redundant-as-str` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::redundant_as_str)]`
9+
10+
error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
11+
--> $DIR/redundant_as_str.rs:8:29
12+
|
13+
LL | let _redundant = string.as_str().is_empty();
14+
| ^^^^^^^^^^^^^^^^^ help: try: `is_empty`
615

7-
error: aborting due to previous error
16+
error: aborting due to 2 previous errors
817

0 commit comments

Comments
 (0)