Skip to content

Commit 9784009

Browse files
authored
Merge pull request #2984 from flip1995/single_char_pattern
single_char_pattern: lint only on the argument span
2 parents bc98346 + 74fcf7d commit 9784009

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

clippy_lints/src/methods.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1889,18 +1889,17 @@ fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hi
18891889
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
18901890
if r.len() == 1 {
18911891
let c = r.chars().next().unwrap();
1892-
let snip = snippet(cx, expr.span, "..");
1892+
let snip = snippet(cx, arg.span, "..");
18931893
let hint = snip.replace(
18941894
&format!("\"{}\"", c.escape_default()),
18951895
&format!("'{}'", c.escape_default()));
1896-
span_lint_and_then(
1896+
span_lint_and_sugg(
18971897
cx,
18981898
SINGLE_CHAR_PATTERN,
18991899
arg.span,
19001900
"single-character string constant used as pattern",
1901-
|db| {
1902-
db.span_suggestion(expr.span, "try using a char instead", hint);
1903-
},
1901+
"try using a char instead",
1902+
hint,
19041903
);
19051904
}
19061905
}

tests/ui/single_char_pattern.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ fn main() {
4040

4141
let h = HashSet::<String>::new();
4242
h.contains("X"); // should not warn
43+
44+
x.replace(";", ",").split(","); // issue #2978
4345
}

tests/ui/single_char_pattern.stderr

+25-19
Original file line numberDiff line numberDiff line change
@@ -2,111 +2,117 @@ error: single-character string constant used as pattern
22
--> $DIR/single_char_pattern.rs:5:13
33
|
44
5 | x.split("x");
5-
| --------^^^- help: try using a char instead: `x.split('x')`
5+
| ^^^ help: try using a char instead: `'x'`
66
|
77
= note: `-D single-char-pattern` implied by `-D warnings`
88

99
error: single-character string constant used as pattern
1010
--> $DIR/single_char_pattern.rs:22:16
1111
|
1212
22 | x.contains("x");
13-
| -----------^^^- help: try using a char instead: `x.contains('x')`
13+
| ^^^ help: try using a char instead: `'x'`
1414

1515
error: single-character string constant used as pattern
1616
--> $DIR/single_char_pattern.rs:23:19
1717
|
1818
23 | x.starts_with("x");
19-
| --------------^^^- help: try using a char instead: `x.starts_with('x')`
19+
| ^^^ help: try using a char instead: `'x'`
2020

2121
error: single-character string constant used as pattern
2222
--> $DIR/single_char_pattern.rs:24:17
2323
|
2424
24 | x.ends_with("x");
25-
| ------------^^^- help: try using a char instead: `x.ends_with('x')`
25+
| ^^^ help: try using a char instead: `'x'`
2626

2727
error: single-character string constant used as pattern
2828
--> $DIR/single_char_pattern.rs:25:12
2929
|
3030
25 | x.find("x");
31-
| -------^^^- help: try using a char instead: `x.find('x')`
31+
| ^^^ help: try using a char instead: `'x'`
3232

3333
error: single-character string constant used as pattern
3434
--> $DIR/single_char_pattern.rs:26:13
3535
|
3636
26 | x.rfind("x");
37-
| --------^^^- help: try using a char instead: `x.rfind('x')`
37+
| ^^^ help: try using a char instead: `'x'`
3838

3939
error: single-character string constant used as pattern
4040
--> $DIR/single_char_pattern.rs:27:14
4141
|
4242
27 | x.rsplit("x");
43-
| ---------^^^- help: try using a char instead: `x.rsplit('x')`
43+
| ^^^ help: try using a char instead: `'x'`
4444

4545
error: single-character string constant used as pattern
4646
--> $DIR/single_char_pattern.rs:28:24
4747
|
4848
28 | x.split_terminator("x");
49-
| -------------------^^^- help: try using a char instead: `x.split_terminator('x')`
49+
| ^^^ help: try using a char instead: `'x'`
5050

5151
error: single-character string constant used as pattern
5252
--> $DIR/single_char_pattern.rs:29:25
5353
|
5454
29 | x.rsplit_terminator("x");
55-
| --------------------^^^- help: try using a char instead: `x.rsplit_terminator('x')`
55+
| ^^^ help: try using a char instead: `'x'`
5656

5757
error: single-character string constant used as pattern
5858
--> $DIR/single_char_pattern.rs:30:17
5959
|
6060
30 | x.splitn(0, "x");
61-
| ------------^^^- help: try using a char instead: `x.splitn(0, 'x')`
61+
| ^^^ help: try using a char instead: `'x'`
6262

6363
error: single-character string constant used as pattern
6464
--> $DIR/single_char_pattern.rs:31:18
6565
|
6666
31 | x.rsplitn(0, "x");
67-
| -------------^^^- help: try using a char instead: `x.rsplitn(0, 'x')`
67+
| ^^^ help: try using a char instead: `'x'`
6868

6969
error: single-character string constant used as pattern
7070
--> $DIR/single_char_pattern.rs:32:15
7171
|
7272
32 | x.matches("x");
73-
| ----------^^^- help: try using a char instead: `x.matches('x')`
73+
| ^^^ help: try using a char instead: `'x'`
7474

7575
error: single-character string constant used as pattern
7676
--> $DIR/single_char_pattern.rs:33:16
7777
|
7878
33 | x.rmatches("x");
79-
| -----------^^^- help: try using a char instead: `x.rmatches('x')`
79+
| ^^^ help: try using a char instead: `'x'`
8080

8181
error: single-character string constant used as pattern
8282
--> $DIR/single_char_pattern.rs:34:21
8383
|
8484
34 | x.match_indices("x");
85-
| ----------------^^^- help: try using a char instead: `x.match_indices('x')`
85+
| ^^^ help: try using a char instead: `'x'`
8686

8787
error: single-character string constant used as pattern
8888
--> $DIR/single_char_pattern.rs:35:22
8989
|
9090
35 | x.rmatch_indices("x");
91-
| -----------------^^^- help: try using a char instead: `x.rmatch_indices('x')`
91+
| ^^^ help: try using a char instead: `'x'`
9292

9393
error: single-character string constant used as pattern
9494
--> $DIR/single_char_pattern.rs:36:25
9595
|
9696
36 | x.trim_left_matches("x");
97-
| --------------------^^^- help: try using a char instead: `x.trim_left_matches('x')`
97+
| ^^^ help: try using a char instead: `'x'`
9898

9999
error: single-character string constant used as pattern
100100
--> $DIR/single_char_pattern.rs:37:26
101101
|
102102
37 | x.trim_right_matches("x");
103-
| ---------------------^^^- help: try using a char instead: `x.trim_right_matches('x')`
103+
| ^^^ help: try using a char instead: `'x'`
104104

105105
error: single-character string constant used as pattern
106106
--> $DIR/single_char_pattern.rs:39:13
107107
|
108108
39 | x.split("/n");
109-
| --------^^^^- help: try using a char instead: `x.split('/n')`
109+
| ^^^^ help: try using a char instead: `'/n'`
110110

111-
error: aborting due to 18 previous errors
111+
error: single-character string constant used as pattern
112+
--> $DIR/single_char_pattern.rs:44:31
113+
|
114+
44 | x.replace(";", ",").split(","); // issue #2978
115+
| ^^^ help: try using a char instead: `','`
116+
117+
error: aborting due to 19 previous errors
112118

0 commit comments

Comments
 (0)