Fix lint_single_char_pattern on raw string literal

This commit is contained in:
Lzu Tao 2019-08-09 10:45:49 +07:00
parent d23e6b396a
commit 30cbdc7491
4 changed files with 31 additions and 4 deletions

View File

@ -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,

View File

@ -53,4 +53,8 @@ fn main() {
// Issue #3204
const S: &str = "#";
x.find(S);
// Raw string
x.split('a');
x.split('a');
}

View File

@ -53,4 +53,8 @@ fn main() {
// Issue #3204
const S: &str = "#";
x.find(S);
// Raw string
x.split(r"a");
x.split(r#"a"#);
}

View File

@ -132,5 +132,17 @@ 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: aborting due to 24 previous errors