From 1a8b8cd28f3e452a1c4bfc2208ada1a2f4a0ebda Mon Sep 17 00:00:00 2001 From: mcarton Date: Sun, 7 Feb 2016 18:28:37 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20use=20`{:=3F}`=20and=20use=20sp?= =?UTF-8?q?an=5Fsuggestion=20in=20TOPLEVEL=5FREF=5FARG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/misc.rs | 16 +++++++++++----- tests/compile-fail/toplevel_ref_arg.rs | 12 ++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/misc.rs b/src/misc.rs index c0aed7822..f570c18b7 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -11,7 +11,7 @@ use rustc::middle::const_eval::eval_const_expr_partial; use rustc::middle::const_eval::EvalHint::ExprTypeChecked; use utils::{get_item_name, match_path, snippet, get_parent_expr, span_lint}; -use utils::{span_help_and_lint, walk_ptrs_ty, is_integer_literal, implements_trait}; +use utils::{span_lint_and_then, walk_ptrs_ty, is_integer_literal, implements_trait}; /// **What it does:** This lint checks for function arguments and let bindings denoted as `ref`. /// @@ -62,16 +62,22 @@ impl LateLintPass for TopLevelRefPass { let Some(ref init) = l.init ], { let tyopt = if let Some(ref ty) = l.ty { - format!(": {:?} ", ty) + format!(": {}", snippet(cx, ty.span, "_")) } else { "".to_owned() }; - span_help_and_lint(cx, + span_lint_and_then(cx, TOPLEVEL_REF_ARG, l.pat.span, "`ref` on an entire `let` pattern is discouraged, take a reference with & instead", - &format!("try `let {} {}= &{};`", snippet(cx, i.span, "_"), - tyopt, snippet(cx, init.span, "_")) + |db| { + db.span_suggestion(s.span, + "try", + format!("let {}{} = &{};", + snippet(cx, i.span, "_"), + tyopt, + snippet(cx, init.span, "_"))); + } ); } }; diff --git a/tests/compile-fail/toplevel_ref_arg.rs b/tests/compile-fail/toplevel_ref_arg.rs index 05ad1af00..de1556ed0 100644 --- a/tests/compile-fail/toplevel_ref_arg.rs +++ b/tests/compile-fail/toplevel_ref_arg.rs @@ -15,11 +15,15 @@ fn main() { let y = |ref x| { println!("{:?}", x) }; y(1u8); - let ref x = 1; //~ ERROR `ref` on an entire `let` pattern is discouraged - //~^ HELP try `let x = &1;` + let ref x = 1; + //~^ ERROR `ref` on an entire `let` pattern is discouraged + //~| HELP try + //~| SUGGESTION let x = &1; - let ref y = (&1, 2); //~ ERROR `ref` on an entire `let` pattern is discouraged - //~^ HELP try `let y = &(&1, 2);` + let ref y : (&_, u8) = (&1, 2); + //~^ ERROR `ref` on an entire `let` pattern is discouraged + //~| HELP try + //~| SUGGESTION let y: (&_, u8) = &(&1, 2); let (ref x, _) = (1,2); // okay, not top level println!("The answer is {}.", x);