diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 05453535347..d753b664045 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -3,9 +3,9 @@ use crate::utils::paths; use crate::utils::sugg::Sugg; use crate::utils::usage::is_unused; use crate::utils::{ - expr_block, get_arg_name, in_macro, is_allowed, is_expn_of, is_refutable, is_wild, match_qpath, match_type, - match_var, multispan_sugg, remove_blocks, snippet, snippet_block, snippet_with_applicability, span_lint_and_help, - span_lint_and_note, span_lint_and_sugg, span_lint_and_then, walk_ptrs_ty, + expr_block, get_arg_name, in_macro, indent_of, is_allowed, is_expn_of, is_refutable, is_wild, match_qpath, + match_type, match_var, multispan_sugg, remove_blocks, snippet, snippet_block, snippet_with_applicability, + span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then, walk_ptrs_ty, }; use if_chain::if_chain; use rustc::lint::in_external_macro; @@ -836,7 +836,7 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A let mut snippet_body = if match_body.span.from_expansion() { Sugg::hir_with_macro_callsite(cx, match_body, "..").to_string() } else { - snippet_block(cx, match_body.span, "..").to_owned().to_string() + snippet_block(cx, match_body.span, "..", Some(expr.span)).to_string() }; // Do we need to add ';' to suggestion ? @@ -865,10 +865,11 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A "this match could be written as a `let` statement", "consider using `let` statement", format!( - "let {} = {};\n{}", + "let {} = {};\n{}{}", snippet_with_applicability(cx, bind_names, "..", &mut applicability), snippet_with_applicability(cx, matched_vars, "..", &mut applicability), - snippet_body + " ".repeat(indent_of(cx, expr.span).unwrap_or(0)), + snippet_body, ), applicability, ); diff --git a/tests/ui/match_single_binding.fixed b/tests/ui/match_single_binding.fixed index 8fb8dc323e4..932bd6783a1 100644 --- a/tests/ui/match_single_binding.fixed +++ b/tests/ui/match_single_binding.fixed @@ -14,12 +14,12 @@ fn main() { let c = 3; // Lint let (x, y, z) = (a, b, c); -{ - println!("{} {} {}", x, y, z); -} + { + println!("{} {} {}", x, y, z); + } // Lint let (x, y, z) = (a, b, c); -println!("{} {} {}", x, y, z); + println!("{} {} {}", x, y, z); // Ok match a { 2 => println!("2"), @@ -35,29 +35,29 @@ println!("{} {} {}", x, y, z); println!("whatever"); // Lint { - let x = 29; - println!("x has a value of {}", x); -} + let x = 29; + println!("x has a value of {}", x); + } // Lint { - let e = 5 * a; - if e >= 5 { - println!("e is superior to 5"); + let e = 5 * a; + if e >= 5 { + println!("e is superior to 5"); + } } -} // Lint let p = Point { x: 0, y: 7 }; let Point { x, y } = p; -println!("Coords: ({}, {})", x, y); + println!("Coords: ({}, {})", x, y); // Lint let Point { x: x1, y: y1 } = p; -println!("Coords: ({}, {})", x1, y1); + println!("Coords: ({}, {})", x1, y1); // Lint let x = 5; let ref r = x; -println!("Got a reference to {}", r); + println!("Got a reference to {}", r); // Lint let mut x = 5; let ref mut mr = x; -println!("Got a mutable reference to {}", mr); + println!("Got a mutable reference to {}", mr); } diff --git a/tests/ui/match_single_binding.stderr b/tests/ui/match_single_binding.stderr index d76e229adff..471aec780d2 100644 --- a/tests/ui/match_single_binding.stderr +++ b/tests/ui/match_single_binding.stderr @@ -12,9 +12,9 @@ LL | | } help: consider using `let` statement | LL | let (x, y, z) = (a, b, c); -LL | { -LL | println!("{} {} {}", x, y, z); -LL | } +LL | { +LL | println!("{} {} {}", x, y, z); +LL | } | error: this match could be written as a `let` statement @@ -28,7 +28,7 @@ LL | | } help: consider using `let` statement | LL | let (x, y, z) = (a, b, c); -LL | println!("{} {} {}", x, y, z); +LL | println!("{} {} {}", x, y, z); | error: this match could be replaced by its body itself @@ -53,9 +53,9 @@ LL | | } help: consider using the match body instead | LL | { -LL | let x = 29; -LL | println!("x has a value of {}", x); -LL | } +LL | let x = 29; +LL | println!("x has a value of {}", x); +LL | } | error: this match could be replaced by its body itself @@ -73,11 +73,11 @@ LL | | } help: consider using the match body instead | LL | { -LL | let e = 5 * a; -LL | if e >= 5 { -LL | println!("e is superior to 5"); +LL | let e = 5 * a; +LL | if e >= 5 { +LL | println!("e is superior to 5"); +LL | } LL | } -LL | } | error: this match could be written as a `let` statement @@ -91,7 +91,7 @@ LL | | } help: consider using `let` statement | LL | let Point { x, y } = p; -LL | println!("Coords: ({}, {})", x, y); +LL | println!("Coords: ({}, {})", x, y); | error: this match could be written as a `let` statement @@ -105,7 +105,7 @@ LL | | } help: consider using `let` statement | LL | let Point { x: x1, y: y1 } = p; -LL | println!("Coords: ({}, {})", x1, y1); +LL | println!("Coords: ({}, {})", x1, y1); | error: this match could be written as a `let` statement @@ -119,7 +119,7 @@ LL | | } help: consider using `let` statement | LL | let ref r = x; -LL | println!("Got a reference to {}", r); +LL | println!("Got a reference to {}", r); | error: this match could be written as a `let` statement @@ -133,7 +133,7 @@ LL | | } help: consider using `let` statement | LL | let ref mut mr = x; -LL | println!("Got a mutable reference to {}", mr); +LL | println!("Got a mutable reference to {}", mr); | error: aborting due to 9 previous errors