]> git.lizzy.rs Git - rust.git/commitdiff
Suggest mutability and fix type in toplevel-ref-arg
authorManish Goregaokar <manishsmail@gmail.com>
Fri, 15 Jul 2016 12:22:34 +0000 (17:52 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Fri, 15 Jul 2016 12:22:34 +0000 (17:52 +0530)
clippy_lints/src/misc.rs
tests/compile-fail/toplevel_ref_arg.rs

index b1627c31c8b80033517ac667d372ad5db3304b40..77d965fd799b793a2a489d25ca9ef92533580a78 100644 (file)
@@ -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()));
index b2240cffb1a6638058fec7b45ab7682d4cb21aed..5aecc64ebcaa3bcd26e0f8c225d96cfbaa2935ea 100644 (file)
@@ -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);
 }