Use utils::sugg in TOPLEVEL_REF_ARG

This commit is contained in:
mcarton 2016-06-29 22:37:10 +02:00
parent a3c505551f
commit 7023988020
No known key found for this signature in database
GPG key ID: 5E427C794CBA45E8
3 changed files with 14 additions and 3 deletions

View file

@ -72,12 +72,13 @@ impl LateLintPass for TopLevelRefPass {
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 {}{} = &{};",
format!("let {}{} = {};",
snippet(cx, i.span, "_"),
tyopt,
snippet(cx, init.span, "_")));
init.addr()));
}
);
}}

View file

@ -114,6 +114,11 @@ impl<'a> Sugg<'a> {
pub fn and(&self, rhs: &Self) -> Sugg<'static> {
make_binop(ast::BinOpKind::And, self, rhs)
}
/// Convenience method to create the `&<expr>` suggestion.
pub fn addr(&self) -> Sugg<'static> {
make_unop("&", self)
}
}
impl<'a, 'b> std::ops::Sub<&'b Sugg<'b>> for &'a Sugg<'a> {

View file

@ -20,11 +20,16 @@ fn main() {
//~| HELP try
//~| SUGGESTION let x = &1;
let ref y : (&_, u8) = (&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 z = 1 + 2;
//~^ ERROR `ref` on an entire `let` pattern is discouraged
//~| HELP try
//~| SUGGESTION let z = &(1 + 2);
let (ref x, _) = (1,2); // okay, not top level
println!("The answer is {}.", x);
}