Skip to content

Commit 699ee5e

Browse files
committed
Auto merge of rust-lang#8376 - dswij:8373, r=camsteffen
[`chars_next_cmp`] Fix unescaped suggestion closes rust-lang#8373 changelog: [`chars_next_cmp`] Fix unescaped suggestion
2 parents bef92b8 + 5faa7eb commit 699ee5e

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

Diff for: clippy_lints/src/methods/chars_cmp_with_unwrap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(super) fn check<'tcx>(
3232
if info.eq { "" } else { "!" },
3333
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
3434
suggest,
35-
c),
35+
c.escape_default()),
3636
applicability,
3737
);
3838

Diff for: tests/ui/starts_ends_with.fixed

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ fn main() {}
77
fn starts_with() {
88
"".starts_with(' ');
99
!"".starts_with(' ');
10+
11+
// Ensure that suggestion is escaped correctly
12+
"".starts_with('\n');
13+
!"".starts_with('\n');
1014
}
1115

1216
fn chars_cmp_with_unwrap() {
@@ -31,7 +35,7 @@ fn chars_cmp_with_unwrap() {
3135
// !s.ends_with('o')
3236
// Nothing here
3337
}
34-
if !s.ends_with('o') {
38+
if !s.ends_with('\n') {
3539
// !s.ends_with('o')
3640
// Nothing here
3741
}
@@ -43,4 +47,8 @@ fn ends_with() {
4347
!"".ends_with(' ');
4448
"".ends_with(' ');
4549
!"".ends_with(' ');
50+
51+
// Ensure that suggestion is escaped correctly
52+
"".ends_with('\n');
53+
!"".ends_with('\n');
4654
}

Diff for: tests/ui/starts_ends_with.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ fn main() {}
77
fn starts_with() {
88
"".chars().next() == Some(' ');
99
Some(' ') != "".chars().next();
10+
11+
// Ensure that suggestion is escaped correctly
12+
"".chars().next() == Some('\n');
13+
Some('\n') != "".chars().next();
1014
}
1115

1216
fn chars_cmp_with_unwrap() {
@@ -31,7 +35,7 @@ fn chars_cmp_with_unwrap() {
3135
// !s.ends_with('o')
3236
// Nothing here
3337
}
34-
if s.chars().last().unwrap() != 'o' {
38+
if s.chars().last().unwrap() != '\n' {
3539
// !s.ends_with('o')
3640
// Nothing here
3741
}
@@ -43,4 +47,8 @@ fn ends_with() {
4347
Some(' ') != "".chars().last();
4448
"".chars().next_back() == Some(' ');
4549
Some(' ') != "".chars().next_back();
50+
51+
// Ensure that suggestion is escaped correctly
52+
"".chars().last() == Some('\n');
53+
Some('\n') != "".chars().last();
4654
}

Diff for: tests/ui/starts_ends_with.stderr

+37-13
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,90 @@ LL | Some(' ') != "".chars().next();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".starts_with(' ')`
1414

1515
error: you should use the `starts_with` method
16-
--> $DIR/starts_ends_with.rs:14:8
16+
--> $DIR/starts_ends_with.rs:12:5
17+
|
18+
LL | "".chars().next() == Some('/n');
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `"".starts_with('/n')`
20+
21+
error: you should use the `starts_with` method
22+
--> $DIR/starts_ends_with.rs:13:5
23+
|
24+
LL | Some('/n') != "".chars().next();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".starts_with('/n')`
26+
27+
error: you should use the `starts_with` method
28+
--> $DIR/starts_ends_with.rs:18:8
1729
|
1830
LL | if s.chars().next().unwrap() == 'f' {
1931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `s.starts_with('f')`
2032

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

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

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

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

4759
error: you should use the `ends_with` method
48-
--> $DIR/starts_ends_with.rs:34:8
60+
--> $DIR/starts_ends_with.rs:38:8
4961
|
50-
LL | if s.chars().last().unwrap() != 'o' {
51-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!s.ends_with('o')`
62+
LL | if s.chars().last().unwrap() != '/n' {
63+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!s.ends_with('/n')`
5264

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

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

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

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

77-
error: aborting due to 12 previous errors
89+
error: you should use the `ends_with` method
90+
--> $DIR/starts_ends_with.rs:52:5
91+
|
92+
LL | "".chars().last() == Some('/n');
93+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `"".ends_with('/n')`
94+
95+
error: you should use the `ends_with` method
96+
--> $DIR/starts_ends_with.rs:53:5
97+
|
98+
LL | Some('/n') != "".chars().last();
99+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: like this: `!"".ends_with('/n')`
100+
101+
error: aborting due to 16 previous errors
78102

0 commit comments

Comments
 (0)