Skip to content

Commit

Permalink
Auto merge of #8376 - dswij:8373, r=camsteffen
Browse files Browse the repository at this point in the history
[`chars_next_cmp`] Fix unescaped suggestion

closes #8373

changelog: [`chars_next_cmp`] Fix unescaped suggestion
  • Loading branch information
bors committed Feb 5, 2022
2 parents bef92b8 + 5faa7eb commit 699ee5e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/chars_cmp_with_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(super) fn check<'tcx>(
if info.eq { "" } else { "!" },
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
suggest,
c),
c.escape_default()),
applicability,
);

Expand Down
10 changes: 9 additions & 1 deletion tests/ui/starts_ends_with.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ fn main() {}
fn starts_with() {
"".starts_with(' ');
!"".starts_with(' ');

// Ensure that suggestion is escaped correctly
"".starts_with('\n');
!"".starts_with('\n');
}

fn chars_cmp_with_unwrap() {
Expand All @@ -31,7 +35,7 @@ fn chars_cmp_with_unwrap() {
// !s.ends_with('o')
// Nothing here
}
if !s.ends_with('o') {
if !s.ends_with('\n') {
// !s.ends_with('o')
// Nothing here
}
Expand All @@ -43,4 +47,8 @@ fn ends_with() {
!"".ends_with(' ');
"".ends_with(' ');
!"".ends_with(' ');

// Ensure that suggestion is escaped correctly
"".ends_with('\n');
!"".ends_with('\n');
}
10 changes: 9 additions & 1 deletion tests/ui/starts_ends_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ fn main() {}
fn starts_with() {
"".chars().next() == Some(' ');
Some(' ') != "".chars().next();

// Ensure that suggestion is escaped correctly
"".chars().next() == Some('\n');
Some('\n') != "".chars().next();
}

fn chars_cmp_with_unwrap() {
Expand All @@ -31,7 +35,7 @@ fn chars_cmp_with_unwrap() {
// !s.ends_with('o')
// Nothing here
}
if s.chars().last().unwrap() != 'o' {
if s.chars().last().unwrap() != '\n' {
// !s.ends_with('o')
// Nothing here
}
Expand All @@ -43,4 +47,8 @@ fn ends_with() {
Some(' ') != "".chars().last();
"".chars().next_back() == Some(' ');
Some(' ') != "".chars().next_back();

// Ensure that suggestion is escaped correctly
"".chars().last() == Some('\n');
Some('\n') != "".chars().last();
}
50 changes: 37 additions & 13 deletions tests/ui/starts_ends_with.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,90 @@ LL | Some(' ') != "".chars().next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".starts_with(' ')`

error: you should use the `starts_with` method
--> $DIR/starts_ends_with.rs:14:8
--> $DIR/starts_ends_with.rs:12:5
|
LL | "".chars().next() == Some('/n');
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `"".starts_with('/n')`

error: you should use the `starts_with` method
--> $DIR/starts_ends_with.rs:13:5
|
LL | Some('/n') != "".chars().next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".starts_with('/n')`

error: you should use the `starts_with` method
--> $DIR/starts_ends_with.rs:18:8
|
LL | if s.chars().next().unwrap() == 'f' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `s.starts_with('f')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:18:8
--> $DIR/starts_ends_with.rs:22:8
|
LL | if s.chars().next_back().unwrap() == 'o' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `s.ends_with('o')`
|
= note: `-D clippy::chars-last-cmp` implied by `-D warnings`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:22:8
--> $DIR/starts_ends_with.rs:26:8
|
LL | if s.chars().last().unwrap() == 'o' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `s.ends_with('o')`

error: you should use the `starts_with` method
--> $DIR/starts_ends_with.rs:26:8
--> $DIR/starts_ends_with.rs:30:8
|
LL | if s.chars().next().unwrap() != 'f' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!s.starts_with('f')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:30:8
--> $DIR/starts_ends_with.rs:34:8
|
LL | if s.chars().next_back().unwrap() != 'o' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!s.ends_with('o')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:34:8
--> $DIR/starts_ends_with.rs:38:8
|
LL | if s.chars().last().unwrap() != 'o' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!s.ends_with('o')`
LL | if s.chars().last().unwrap() != '/n' {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!s.ends_with('/n')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:42:5
--> $DIR/starts_ends_with.rs:46:5
|
LL | "".chars().last() == Some(' ');
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `"".ends_with(' ')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:43:5
--> $DIR/starts_ends_with.rs:47:5
|
LL | Some(' ') != "".chars().last();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".ends_with(' ')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:44:5
--> $DIR/starts_ends_with.rs:48:5
|
LL | "".chars().next_back() == Some(' ');
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `"".ends_with(' ')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:45:5
--> $DIR/starts_ends_with.rs:49:5
|
LL | Some(' ') != "".chars().next_back();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".ends_with(' ')`

error: aborting due to 12 previous errors
error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:52:5
|
LL | "".chars().last() == Some('/n');
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `"".ends_with('/n')`

error: you should use the `ends_with` method
--> $DIR/starts_ends_with.rs:53:5
|
LL | Some('/n') != "".chars().last();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".ends_with('/n')`

error: aborting due to 16 previous errors

0 comments on commit 699ee5e

Please sign in to comment.