Skip to content

Commit 00ca47b

Browse files
committed
Add more examples and comments to redundant_as_str test
1 parent f2ab16e commit 00ca47b

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

tests/ui/redundant_as_str.fixed

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/ui/redundant_as_str.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
#![warn(clippy::redundant_as_str)]
22

33
fn main() {
4-
let _redundant = "Hello, world!".to_owned().as_str().as_bytes();
5-
let _redundant = "Hello, world!".to_owned().as_str().is_empty();
4+
let string = "Hello, world!".to_owned();
65

7-
let _ok = "Hello, world!".to_owned().as_bytes();
8-
let _ok = "Hello, world!".to_owned().is_empty();
6+
// These methods are redundant and the `as_str` can be removed.
7+
let _redundant = string.as_str().as_bytes();
8+
let _redundant = string.as_str().is_empty();
9+
10+
// These methods are not redundant, and are equivelant to
11+
// doing dereferencing the string and applying the method.
12+
let _not_redundant = string.as_str().escape_unicode();
13+
let _not_redundant = string.as_str().trim();
14+
let _not_redundant = string.as_str().split_whitespace();
15+
16+
// 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();
920
}

tests/ui/redundant_as_str.stderr

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
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:4:49
1+
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `!`
2+
--> $DIR/redundant_as_str.rs:17:35
33
|
4-
LL | let _redundant = "Hello, world!".to_owned().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:5:49
12-
|
13-
LL | let _redundant = "Hello, world!".to_owned().as_str().is_empty();
14-
| ^^^^^^^^^^^^^^^^^ help: try: `is_empty`
4+
LL | let borrowed_str = "Hello, world"!
5+
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
156

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

0 commit comments

Comments
 (0)