diff --git a/src/misc.rs b/src/misc.rs index 53bf73de3..444062a59 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -43,13 +43,20 @@ impl LintPass for MiscPass { // an enum is extended. So we only consider cases where a `_` wildcard is used if arms[1].pats[0].node == PatWild(PatWildSingle) && arms[0].pats.len() == 1 { + let body_code = snippet(cx, arms[0].body.span, ".."); + let suggestion = if let ExprBlock(_) = arms[0].body.node { + body_code.into_owned() + } else { + format!("{{ {} }}", body_code) + }; span_help_and_lint(cx, SINGLE_MATCH, expr.span, "You seem to be trying to use match for \ - destructuring a single type. Did you mean to \ + destructuring a single pattern. Did you mean to \ use `if let`?", - &*format!("Try if let {} = {} {{ ... }}", - snippet(cx, arms[0].pats[0].span, ".."), - snippet(cx, ex.span, "..")) + &*format!("Try\nif let {} = {} {}", + snippet(cx, arms[0].pats[0].span, ".."), + snippet(cx, ex.span, ".."), + suggestion) ); } } diff --git a/src/utils.rs b/src/utils.rs index 4f5763b14..9b3b94e11 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -81,6 +81,6 @@ pub fn span_help_and_lint(cx: &Context, lint: &'static Lint, span: Span, msg: &str, help: &str) { span_lint(cx, lint, span, msg); if cx.current_level(lint) != Level::Allow { - cx.sess().span_help(span, help); + cx.sess().fileline_help(span, help); } } diff --git a/tests/compile-fail/match_if_let.rs b/tests/compile-fail/match_if_let.rs old mode 100644 new mode 100755 index f1864f9d7..47b8b18a5 --- a/tests/compile-fail/match_if_let.rs +++ b/tests/compile-fail/match_if_let.rs @@ -6,8 +6,10 @@ fn main(){ let x = Some(1u8); match x { //~ ERROR You seem to be trying to use match - //~^ HELP Try if let Some(y) = x { ... } - Some(y) => println!("{:?}", y), + //~^ HELP Try + Some(y) => { + println!("{:?}", y); + } _ => () } // Not linted @@ -17,7 +19,7 @@ fn main(){ } let z = (1u8,1u8); match z { //~ ERROR You seem to be trying to use match - //~^ HELP Try if let (2...3, 7...9) = z { ... } + //~^ HELP Try (2...3, 7...9) => println!("{:?}", z), _ => {} }