Skip to content

Fix lint_single_char_pattern on raw string literal #4361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2329,13 +2329,20 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
if_chain! {
if let hir::ExprKind::Lit(lit) = &arg.node;
if let ast::LitKind::Str(r, _) = lit.node;
if let ast::LitKind::Str(r, style) = lit.node;
if r.as_str().len() == 1;
then {
let mut applicability = Applicability::MachineApplicable;
let snip = snippet_with_applicability(cx, arg.span, "..", &mut applicability);
let c = &snip[1..snip.len() - 1];
let hint = format!("'{}'", if c == "'" { "\\'" } else { c });
let ch = if let ast::StrStyle::Raw(nhash) = style {
let nhash = nhash as usize;
// for raw string: r##"a"##
&snip[(nhash + 2)..(snip.len() - 1 - nhash)]
} else {
// for regular string: "a"
&snip[1..(snip.len() - 1)]
};
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
span_lint_and_sugg(
cx,
SINGLE_CHAR_PATTERN,
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/single_char_pattern.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ fn main() {
// Issue #3204
const S: &str = "#";
x.find(S);

// Raw string
x.split('a');
x.split('a');
x.split('a');
x.split('\'');
x.split('#');
}
7 changes: 7 additions & 0 deletions tests/ui/single_char_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ fn main() {
// Issue #3204
const S: &str = "#";
x.find(S);

// Raw string
x.split(r"a");
x.split(r#"a"#);
x.split(r###"a"###);
x.split(r###"'"###);
x.split(r###"#"###);
}
32 changes: 31 additions & 1 deletion tests/ui/single_char_pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,35 @@ error: single-character string constant used as pattern
LL | x.starts_with("/x03"); // issue #2996
| ^^^^^^ help: try using a char instead: `'/x03'`

error: aborting due to 22 previous errors
error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:58:13
|
LL | x.split(r"a");
| ^^^^ help: try using a char instead: `'a'`

error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:59:13
|
LL | x.split(r#"a"#);
| ^^^^^^ help: try using a char instead: `'a'`

error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:60:13
|
LL | x.split(r###"a"###);
| ^^^^^^^^^^ help: try using a char instead: `'a'`

error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:61:13
|
LL | x.split(r###"'"###);
| ^^^^^^^^^^ help: try using a char instead: `'/''`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the suggestion is not `'\''`. It seems really awkward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that might be caused by some compiletest-rs normalization of the output. The same happens with other suggestions


error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:62:13
|
LL | x.split(r###"#"###);
| ^^^^^^^^^^ help: try using a char instead: `'#'`

error: aborting due to 27 previous errors