From 60f354880f4f9b7437e78da1c0ba6e72dc919b67 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 15 Jul 2016 17:52:34 +0530 Subject: [PATCH] Suggest mutability and fix type in toplevel-ref-arg --- clippy_lints/src/misc.rs | 12 +++++++++--- tests/compile-fail/toplevel_ref_arg.rs | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index b1627c31c8b..77d965fd799 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -59,14 +59,19 @@ fn check_stmt(&mut self, cx: &LateContext, s: &Stmt) { if_let_chain! {[ let StmtDecl(ref d, _) = s.node, let DeclLocal(ref l) = d.node, - let PatKind::Binding(BindByRef(_), i, None) = l.pat.node, + 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, "_")) + format!(": &{}", snippet(cx, ty.span, "_")) } else { "".to_owned() }; + let mutopt = if mt == Mutability::MutMutable { + "mut " + } else { + "" + }; span_lint_and_then(cx, TOPLEVEL_REF_ARG, l.pat.span, @@ -75,7 +80,8 @@ fn check_stmt(&mut self, cx: &LateContext, s: &Stmt) { let init = Sugg::hir(cx, init, ".."); db.span_suggestion(s.span, "try", - format!("let {}{} = {};", + format!("let {}{}{} = {};", + mutopt, snippet(cx, i.span, "_"), tyopt, init.addr())); diff --git a/tests/compile-fail/toplevel_ref_arg.rs b/tests/compile-fail/toplevel_ref_arg.rs index b2240cffb1a..5aecc64ebca 100644 --- a/tests/compile-fail/toplevel_ref_arg.rs +++ b/tests/compile-fail/toplevel_ref_arg.rs @@ -23,13 +23,18 @@ fn main() { let ref y: (&_, u8) = (&1, 2); //~^ ERROR `ref` on an entire `let` pattern is discouraged //~| HELP try - //~| SUGGESTION let y: (&_, u8) = &(&1, 2); + //~| SUGGESTION let y: &(&_, u8) = &(&1, 2); let ref z = 1 + 2; //~^ ERROR `ref` on an entire `let` pattern is discouraged //~| HELP try //~| SUGGESTION let z = &(1 + 2); + let ref mut z = 1 + 2; + //~^ ERROR `ref` on an entire `let` pattern is discouraged + //~| HELP try + //~| SUGGESTION let mut z = &(1 + 2); + let (ref x, _) = (1,2); // okay, not top level println!("The answer is {}.", x); } -- 2.44.0