Skip to content

Commit c1eb8ce

Browse files
matthiaskrgrebroto
authored andcommitted
get_hint_if_single_char_arg: fix bug where multi-char letters are not detected properly
1 parent c6412ae commit c1eb8ce

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3204,7 +3204,7 @@ fn get_hint_if_single_char_arg(
32043204
if let hir::ExprKind::Lit(lit) = &arg.kind;
32053205
if let ast::LitKind::Str(r, style) = lit.node;
32063206
let string = r.as_str();
3207-
if string.len() == 1;
3207+
if string.chars().count() == 1;
32083208
then {
32093209
let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
32103210
let ch = if let ast::StrStyle::Raw(nhash) = style {

tests/ui/single_char_pattern.fixed

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ fn main() {
1818
//
1919
// We may not want to suggest changing these anyway
2020
// See: https://github.com/rust-lang/rust-clippy/issues/650#issuecomment-184328984
21-
x.split("ß");
22-
x.split("ℝ");
23-
x.split("💣");
21+
x.split('ß');
22+
x.split('ℝ');
23+
x.split('💣');
2424
// Can't use this lint for unicode code points which don't fit in a char
2525
x.split("❤️");
2626
x.contains('x');

tests/ui/single_char_pattern.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ LL | x.split("x");
66
|
77
= note: `-D clippy::single-char-pattern` implied by `-D warnings`
88

9+
error: single-character string constant used as pattern
10+
--> $DIR/single_char_pattern.rs:21:13
11+
|
12+
LL | x.split("ß");
13+
| ^^^ help: try using a `char` instead: `'ß'`
14+
15+
error: single-character string constant used as pattern
16+
--> $DIR/single_char_pattern.rs:22:13
17+
|
18+
LL | x.split("ℝ");
19+
| ^^^ help: try using a `char` instead: `'ℝ'`
20+
21+
error: single-character string constant used as pattern
22+
--> $DIR/single_char_pattern.rs:23:13
23+
|
24+
LL | x.split("💣");
25+
| ^^^^ help: try using a `char` instead: `'💣'`
26+
927
error: single-character string constant used as pattern
1028
--> $DIR/single_char_pattern.rs:26:16
1129
|
@@ -162,5 +180,5 @@ error: single-character string constant used as pattern
162180
LL | x.split(r###"#"###);
163181
| ^^^^^^^^^^ help: try using a `char` instead: `'#'`
164182

165-
error: aborting due to 27 previous errors
183+
error: aborting due to 30 previous errors
166184

tests/ui/single_char_push_str.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ fn main() {
1919
string.push('\u{0052}');
2020
string.push('a');
2121

22-
get_string!().push_str("ö");
22+
get_string!().push('ö');
2323
}

tests/ui/single_char_push_str.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ error: calling `push_str()` using a single-character string literal
3030
LL | string.push_str(r##"a"##);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('a')`
3232

33-
error: aborting due to 5 previous errors
33+
error: calling `push_str()` using a single-character string literal
34+
--> $DIR/single_char_push_str.rs:22:5
35+
|
36+
LL | get_string!().push_str("ö");
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `get_string!().push('ö')`
38+
39+
error: aborting due to 6 previous errors
3440

0 commit comments

Comments
 (0)