Merge pull request #2984 from flip1995/single_char_pattern
single_char_pattern: lint only on the argument span
This commit is contained in:
commit
97840090b7
@ -1889,18 +1889,17 @@ fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hi
|
|||||||
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
|
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
|
||||||
if r.len() == 1 {
|
if r.len() == 1 {
|
||||||
let c = r.chars().next().unwrap();
|
let c = r.chars().next().unwrap();
|
||||||
let snip = snippet(cx, expr.span, "..");
|
let snip = snippet(cx, arg.span, "..");
|
||||||
let hint = snip.replace(
|
let hint = snip.replace(
|
||||||
&format!("\"{}\"", c.escape_default()),
|
&format!("\"{}\"", c.escape_default()),
|
||||||
&format!("'{}'", c.escape_default()));
|
&format!("'{}'", c.escape_default()));
|
||||||
span_lint_and_then(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
SINGLE_CHAR_PATTERN,
|
SINGLE_CHAR_PATTERN,
|
||||||
arg.span,
|
arg.span,
|
||||||
"single-character string constant used as pattern",
|
"single-character string constant used as pattern",
|
||||||
|db| {
|
"try using a char instead",
|
||||||
db.span_suggestion(expr.span, "try using a char instead", hint);
|
hint,
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,4 +40,6 @@ fn main() {
|
|||||||
|
|
||||||
let h = HashSet::<String>::new();
|
let h = HashSet::<String>::new();
|
||||||
h.contains("X"); // should not warn
|
h.contains("X"); // should not warn
|
||||||
|
|
||||||
|
x.replace(";", ",").split(","); // issue #2978
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ error: single-character string constant used as pattern
|
|||||||
--> $DIR/single_char_pattern.rs:5:13
|
--> $DIR/single_char_pattern.rs:5:13
|
||||||
|
|
|
|
||||||
5 | x.split("x");
|
5 | x.split("x");
|
||||||
| --------^^^- help: try using a char instead: `x.split('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
|
|
||||||
= note: `-D single-char-pattern` implied by `-D warnings`
|
= note: `-D single-char-pattern` implied by `-D warnings`
|
||||||
|
|
||||||
@ -10,103 +10,109 @@ error: single-character string constant used as pattern
|
|||||||
--> $DIR/single_char_pattern.rs:22:16
|
--> $DIR/single_char_pattern.rs:22:16
|
||||||
|
|
|
|
||||||
22 | x.contains("x");
|
22 | x.contains("x");
|
||||||
| -----------^^^- help: try using a char instead: `x.contains('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:23:19
|
--> $DIR/single_char_pattern.rs:23:19
|
||||||
|
|
|
|
||||||
23 | x.starts_with("x");
|
23 | x.starts_with("x");
|
||||||
| --------------^^^- help: try using a char instead: `x.starts_with('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:24:17
|
--> $DIR/single_char_pattern.rs:24:17
|
||||||
|
|
|
|
||||||
24 | x.ends_with("x");
|
24 | x.ends_with("x");
|
||||||
| ------------^^^- help: try using a char instead: `x.ends_with('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:25:12
|
--> $DIR/single_char_pattern.rs:25:12
|
||||||
|
|
|
|
||||||
25 | x.find("x");
|
25 | x.find("x");
|
||||||
| -------^^^- help: try using a char instead: `x.find('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:26:13
|
--> $DIR/single_char_pattern.rs:26:13
|
||||||
|
|
|
|
||||||
26 | x.rfind("x");
|
26 | x.rfind("x");
|
||||||
| --------^^^- help: try using a char instead: `x.rfind('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:27:14
|
--> $DIR/single_char_pattern.rs:27:14
|
||||||
|
|
|
|
||||||
27 | x.rsplit("x");
|
27 | x.rsplit("x");
|
||||||
| ---------^^^- help: try using a char instead: `x.rsplit('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:28:24
|
--> $DIR/single_char_pattern.rs:28:24
|
||||||
|
|
|
|
||||||
28 | x.split_terminator("x");
|
28 | x.split_terminator("x");
|
||||||
| -------------------^^^- help: try using a char instead: `x.split_terminator('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:29:25
|
--> $DIR/single_char_pattern.rs:29:25
|
||||||
|
|
|
|
||||||
29 | x.rsplit_terminator("x");
|
29 | x.rsplit_terminator("x");
|
||||||
| --------------------^^^- help: try using a char instead: `x.rsplit_terminator('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:30:17
|
--> $DIR/single_char_pattern.rs:30:17
|
||||||
|
|
|
|
||||||
30 | x.splitn(0, "x");
|
30 | x.splitn(0, "x");
|
||||||
| ------------^^^- help: try using a char instead: `x.splitn(0, 'x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:31:18
|
--> $DIR/single_char_pattern.rs:31:18
|
||||||
|
|
|
|
||||||
31 | x.rsplitn(0, "x");
|
31 | x.rsplitn(0, "x");
|
||||||
| -------------^^^- help: try using a char instead: `x.rsplitn(0, 'x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:32:15
|
--> $DIR/single_char_pattern.rs:32:15
|
||||||
|
|
|
|
||||||
32 | x.matches("x");
|
32 | x.matches("x");
|
||||||
| ----------^^^- help: try using a char instead: `x.matches('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:33:16
|
--> $DIR/single_char_pattern.rs:33:16
|
||||||
|
|
|
|
||||||
33 | x.rmatches("x");
|
33 | x.rmatches("x");
|
||||||
| -----------^^^- help: try using a char instead: `x.rmatches('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:34:21
|
--> $DIR/single_char_pattern.rs:34:21
|
||||||
|
|
|
|
||||||
34 | x.match_indices("x");
|
34 | x.match_indices("x");
|
||||||
| ----------------^^^- help: try using a char instead: `x.match_indices('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:35:22
|
--> $DIR/single_char_pattern.rs:35:22
|
||||||
|
|
|
|
||||||
35 | x.rmatch_indices("x");
|
35 | x.rmatch_indices("x");
|
||||||
| -----------------^^^- help: try using a char instead: `x.rmatch_indices('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:36:25
|
--> $DIR/single_char_pattern.rs:36:25
|
||||||
|
|
|
|
||||||
36 | x.trim_left_matches("x");
|
36 | x.trim_left_matches("x");
|
||||||
| --------------------^^^- help: try using a char instead: `x.trim_left_matches('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:37:26
|
--> $DIR/single_char_pattern.rs:37:26
|
||||||
|
|
|
|
||||||
37 | x.trim_right_matches("x");
|
37 | x.trim_right_matches("x");
|
||||||
| ---------------------^^^- help: try using a char instead: `x.trim_right_matches('x')`
|
| ^^^ help: try using a char instead: `'x'`
|
||||||
|
|
||||||
error: single-character string constant used as pattern
|
error: single-character string constant used as pattern
|
||||||
--> $DIR/single_char_pattern.rs:39:13
|
--> $DIR/single_char_pattern.rs:39:13
|
||||||
|
|
|
|
||||||
39 | x.split("/n");
|
39 | x.split("/n");
|
||||||
| --------^^^^- help: try using a char instead: `x.split('/n')`
|
| ^^^^ help: try using a char instead: `'/n'`
|
||||||
|
|
||||||
error: aborting due to 18 previous errors
|
error: single-character string constant used as pattern
|
||||||
|
--> $DIR/single_char_pattern.rs:44:31
|
||||||
|
|
|
||||||
|
44 | x.replace(";", ",").split(","); // issue #2978
|
||||||
|
| ^^^ help: try using a char instead: `','`
|
||||||
|
|
||||||
|
error: aborting due to 19 previous errors
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user