lint: _-prefixed variables don't get an unused-mut warning.

Bringing it into line with the unused-variable one,

    fn main() {
        let mut _a = 1;
    }

will not warn that `_a` is never used mutably.

Fixes #6911.
This commit is contained in:
Huon Wilson 2013-11-16 00:39:48 +11:00
parent 90754ae9c9
commit 6bd8bb51a0
2 changed files with 21 additions and 14 deletions

View file

@ -883,20 +883,23 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) { fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) {
match p.node { match p.node {
ast::PatIdent(ast::BindByValue(ast::MutMutable), _, _) => { ast::PatIdent(ast::BindByValue(ast::MutMutable),
let mut used = false; ref path, _) if pat_util::pat_is_binding(cx.tcx.def_map, p)=> {
let mut bindings = 0; // `let mut _a = 1;` doesn't need a warning.
do pat_util::pat_bindings(cx.tcx.def_map, p) |_, id, _, _| { let initial_underscore = match path.segments {
used = used || cx.tcx.used_mut_nodes.contains(&id); [ast::PathSegment { identifier: id, _ }] => {
bindings += 1; cx.tcx.sess.str_of(id).starts_with("_")
} }
if !used { _ => {
let msg = if bindings == 1 { cx.tcx.sess.span_bug(p.span,
"variable does not need to be mutable" "mutable binding that doesn't \
} else { consist of exactly one segment");
"variables do not need to be mutable" }
}; };
cx.span_lint(unused_mut, p.span, msg);
if !initial_underscore && !cx.tcx.used_mut_nodes.contains(&p.id) {
cx.span_lint(unused_mut, p.span,
"variable does not need to be mutable");
} }
} }
_ => () _ => ()

View file

@ -49,6 +49,10 @@ fn main() {
let x = |mut y: int| y = 32; let x = |mut y: int| y = 32;
fn nothing(mut foo: int) { foo = 37; } fn nothing(mut foo: int) { foo = 37; }
// leading underscore should avoid the warning, just like the
// unused variable lint.
let mut _allowed = 1;
} }
fn callback(f: &fn()) {} fn callback(f: &fn()) {}