Skip to content

Commit 5208332

Browse files
committed
auto merge of #10518 : huonw/rust/6911, r=alexcrichton
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.
2 parents 6c8e337 + 6bd8bb5 commit 5208332

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/librustc/middle/lint.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -883,20 +883,23 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
883883

884884
fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) {
885885
match p.node {
886-
ast::PatIdent(ast::BindByValue(ast::MutMutable), _, _) => {
887-
let mut used = false;
888-
let mut bindings = 0;
889-
do pat_util::pat_bindings(cx.tcx.def_map, p) |_, id, _, _| {
890-
used = used || cx.tcx.used_mut_nodes.contains(&id);
891-
bindings += 1;
892-
}
893-
if !used {
894-
let msg = if bindings == 1 {
895-
"variable does not need to be mutable"
896-
} else {
897-
"variables do not need to be mutable"
898-
};
899-
cx.span_lint(unused_mut, p.span, msg);
886+
ast::PatIdent(ast::BindByValue(ast::MutMutable),
887+
ref path, _) if pat_util::pat_is_binding(cx.tcx.def_map, p)=> {
888+
// `let mut _a = 1;` doesn't need a warning.
889+
let initial_underscore = match path.segments {
890+
[ast::PathSegment { identifier: id, _ }] => {
891+
cx.tcx.sess.str_of(id).starts_with("_")
892+
}
893+
_ => {
894+
cx.tcx.sess.span_bug(p.span,
895+
"mutable binding that doesn't \
896+
consist of exactly one segment");
897+
}
898+
};
899+
900+
if !initial_underscore && !cx.tcx.used_mut_nodes.contains(&p.id) {
901+
cx.span_lint(unused_mut, p.span,
902+
"variable does not need to be mutable");
900903
}
901904
}
902905
_ => ()

src/test/compile-fail/lint-unused-mut-variables.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ fn main() {
4949

5050
let x = |mut y: int| y = 32;
5151
fn nothing(mut foo: int) { foo = 37; }
52+
53+
// leading underscore should avoid the warning, just like the
54+
// unused variable lint.
55+
let mut _allowed = 1;
5256
}
5357

5458
fn callback(f: &fn()) {}

0 commit comments

Comments
 (0)