Suggest mutability and fix type in toplevel-ref-arg

This commit is contained in:
Manish Goregaokar 2016-07-15 17:52:34 +05:30
parent b2637a6f78
commit 60f354880f
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
2 changed files with 15 additions and 4 deletions

View file

@ -59,14 +59,19 @@ impl LateLintPass for TopLevelRefPass {
if_let_chain! {[ if_let_chain! {[
let StmtDecl(ref d, _) = s.node, let StmtDecl(ref d, _) = s.node,
let DeclLocal(ref l) = d.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 Some(ref init) = l.init
], { ], {
let tyopt = if let Some(ref ty) = l.ty { let tyopt = if let Some(ref ty) = l.ty {
format!(": {}", snippet(cx, ty.span, "_")) format!(": &{}", snippet(cx, ty.span, "_"))
} else { } else {
"".to_owned() "".to_owned()
}; };
let mutopt = if mt == Mutability::MutMutable {
"mut "
} else {
""
};
span_lint_and_then(cx, span_lint_and_then(cx,
TOPLEVEL_REF_ARG, TOPLEVEL_REF_ARG,
l.pat.span, l.pat.span,
@ -75,7 +80,8 @@ impl LateLintPass for TopLevelRefPass {
let init = Sugg::hir(cx, init, ".."); let init = Sugg::hir(cx, init, "..");
db.span_suggestion(s.span, db.span_suggestion(s.span,
"try", "try",
format!("let {}{} = {};", format!("let {}{}{} = {};",
mutopt,
snippet(cx, i.span, "_"), snippet(cx, i.span, "_"),
tyopt, tyopt,
init.addr())); init.addr()));

View file

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