Skip to content

Commit 1a8b8cd

Browse files
committed
Don’t use {:?} and use span_suggestion in TOPLEVEL_REF_ARG
1 parent d27aa96 commit 1a8b8cd

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/misc.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc::middle::const_eval::eval_const_expr_partial;
1111
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
1212

1313
use utils::{get_item_name, match_path, snippet, get_parent_expr, span_lint};
14-
use utils::{span_help_and_lint, walk_ptrs_ty, is_integer_literal, implements_trait};
14+
use utils::{span_lint_and_then, walk_ptrs_ty, is_integer_literal, implements_trait};
1515

1616
/// **What it does:** This lint checks for function arguments and let bindings denoted as `ref`.
1717
///
@@ -62,16 +62,22 @@ impl LateLintPass for TopLevelRefPass {
6262
let Some(ref init) = l.init
6363
], {
6464
let tyopt = if let Some(ref ty) = l.ty {
65-
format!(": {:?} ", ty)
65+
format!(": {}", snippet(cx, ty.span, "_"))
6666
} else {
6767
"".to_owned()
6868
};
69-
span_help_and_lint(cx,
69+
span_lint_and_then(cx,
7070
TOPLEVEL_REF_ARG,
7171
l.pat.span,
7272
"`ref` on an entire `let` pattern is discouraged, take a reference with & instead",
73-
&format!("try `let {} {}= &{};`", snippet(cx, i.span, "_"),
74-
tyopt, snippet(cx, init.span, "_"))
73+
|db| {
74+
db.span_suggestion(s.span,
75+
"try",
76+
format!("let {}{} = &{};",
77+
snippet(cx, i.span, "_"),
78+
tyopt,
79+
snippet(cx, init.span, "_")));
80+
}
7581
);
7682
}
7783
};

tests/compile-fail/toplevel_ref_arg.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ fn main() {
1515
let y = |ref x| { println!("{:?}", x) };
1616
y(1u8);
1717

18-
let ref x = 1; //~ ERROR `ref` on an entire `let` pattern is discouraged
19-
//~^ HELP try `let x = &1;`
18+
let ref x = 1;
19+
//~^ ERROR `ref` on an entire `let` pattern is discouraged
20+
//~| HELP try
21+
//~| SUGGESTION let x = &1;
2022

21-
let ref y = (&1, 2); //~ ERROR `ref` on an entire `let` pattern is discouraged
22-
//~^ HELP try `let y = &(&1, 2);`
23+
let ref y : (&_, u8) = (&1, 2);
24+
//~^ ERROR `ref` on an entire `let` pattern is discouraged
25+
//~| HELP try
26+
//~| SUGGESTION let y: (&_, u8) = &(&1, 2);
2327

2428
let (ref x, _) = (1,2); // okay, not top level
2529
println!("The answer is {}.", x);

0 commit comments

Comments
 (0)