From b8c5e5a89c487a76339906f04820e21b9549dfcf Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 15 Jul 2016 19:02:41 +0530 Subject: [PATCH] Fix position of mut in toplevel-ref-arg (fixes #1100, again) --- clippy_lints/src/misc.rs | 23 +++++++++++------------ tests/compile-fail/toplevel_ref_arg.rs | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index 77d965fd799..02629eed852 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -62,29 +62,28 @@ fn check_stmt(&mut self, cx: &LateContext, s: &Stmt) { let PatKind::Binding(BindByRef(mt), i, None) = l.pat.node, let Some(ref init) = l.init ], { - let tyopt = if let Some(ref ty) = l.ty { - format!(": &{}", snippet(cx, ty.span, "_")) + let init = Sugg::hir(cx, init, ".."); + let (mutopt,initref) = if mt == Mutability::MutMutable { + ("mut ", init.mut_addr()) } else { - "".to_owned() + ("", init.addr()) }; - let mutopt = if mt == Mutability::MutMutable { - "mut " + let tyopt = if let Some(ref ty) = l.ty { + format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, "_")) } else { - "" + "".to_owned() }; span_lint_and_then(cx, TOPLEVEL_REF_ARG, l.pat.span, "`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead", |db| { - let init = Sugg::hir(cx, init, ".."); db.span_suggestion(s.span, "try", - format!("let {}{}{} = {};", - mutopt, - snippet(cx, i.span, "_"), - tyopt, - init.addr())); + format!("let {name}{tyopt} = {initref};", + name=snippet(cx, i.span, "_"), + tyopt=tyopt, + initref=initref)); } ); }} diff --git a/tests/compile-fail/toplevel_ref_arg.rs b/tests/compile-fail/toplevel_ref_arg.rs index 5aecc64ebca..86459ea9796 100644 --- a/tests/compile-fail/toplevel_ref_arg.rs +++ b/tests/compile-fail/toplevel_ref_arg.rs @@ -33,7 +33,7 @@ fn main() { let ref mut z = 1 + 2; //~^ ERROR `ref` on an entire `let` pattern is discouraged //~| HELP try - //~| SUGGESTION let mut z = &(1 + 2); + //~| SUGGESTION let z = &mut (1 + 2); let (ref x, _) = (1,2); // okay, not top level println!("The answer is {}.", x); -- 2.44.0