Skip to content

Commit

Permalink
lint: _-prefixed variables don't get an unused-mut warning.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huonw committed Nov 16, 2013
1 parent 90754ae commit 6bd8bb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,20 +883,23 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {

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

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");
}
}
_ => ()
Expand Down
4 changes: 4 additions & 0 deletions src/test/compile-fail/lint-unused-mut-variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ fn main() {

let x = |mut y: int| y = 32;
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()) {}
Expand Down

5 comments on commit 6bd8bb5

@bors
Copy link
Contributor

@bors bors commented on 6bd8bb5 Nov 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at huonw@6bd8bb5

@bors
Copy link
Contributor

@bors bors commented on 6bd8bb5 Nov 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/6911 = 6bd8bb5 into auto

@bors
Copy link
Contributor

@bors bors commented on 6bd8bb5 Nov 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/6911 = 6bd8bb5 merged ok, testing candidate = 5208332

@bors
Copy link
Contributor

@bors bors commented on 6bd8bb5 Nov 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 6bd8bb5 Nov 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 5208332

Please sign in to comment.