diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 19e0bbd8305309c9b31766ce0021d9148554c086..b0a766ec058749a6f101162a11c74f1e0038531a 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -110,7 +110,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { add_early_builtin_with_new!(sess, DeprecatedAttr, - UnusedLoopLabel, + UnusedLabel, ); add_builtin!(sess, @@ -178,8 +178,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { UNUSED_DOC_COMMENT, UNUSED_EXTERN_CRATES, UNUSED_FEATURES, - UNUSED_PARENS, - UNUSED_LOOP_LABEL); + UNUSED_LABEL, + UNUSED_PARENS); add_lint_group!(sess, "rust_2018_idioms", diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 38c5a36067c46ba5175cf7ca05e20fa05b9ec54d..61c0485f88615ef5f8c37266f35920d9110218d6 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -25,8 +25,6 @@ use rustc::hir; -use std::vec; - declare_lint! { pub UNUSED_MUST_USE, Warn, @@ -468,41 +466,38 @@ fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) { } declare_lint! { - pub(super) UNUSED_LOOP_LABEL, + pub(super) UNUSED_LABEL, Warn, - "warns on unused labels for loops" + "warns on unused labels" } #[derive(Clone)] -pub struct UnusedLoopLabel(pub vec::Vec); +pub struct UnusedLabel(pub Vec); -impl UnusedLoopLabel { +impl UnusedLabel { pub fn new() -> Self { - UnusedLoopLabel(vec![]) + UnusedLabel(vec![]) } } -impl LintPass for UnusedLoopLabel { +impl LintPass for UnusedLabel { fn get_lints(&self) -> LintArray { - lint_array!(UNUSED_LOOP_LABEL) + lint_array!(UNUSED_LABEL) } } -impl EarlyLintPass for UnusedLoopLabel { +impl EarlyLintPass for UnusedLabel { fn check_expr(&mut self, _: &EarlyContext, expr: &ast::Expr) { match expr.node { - ast::ExprKind::While(_, _, Some(ref label)) - | ast::ExprKind::WhileLet(_, _, _, Some(ref label)) - | ast::ExprKind::ForLoop(_, _, _, Some(ref label)) - | ast::ExprKind::Loop(_, Some(ref label)) => { - self.0.push(*label); + ast::ExprKind::While(_, _, Some(label)) + | ast::ExprKind::WhileLet(_, _, _, Some(label)) + | ast::ExprKind::ForLoop(_, _, _, Some(label)) + | ast::ExprKind::Loop(_, Some(label)) => { + self.0.push(label); } - ast::ExprKind::Break(Some(ref label), _) | ast::ExprKind::Continue(Some(ref label)) => { - 'remove_used_label: for i in (0..self.0.len()).rev() { - if self.0.get(i).unwrap().ident.name == label.ident.name { - self.0.remove(i); - break 'remove_used_label; - } + ast::ExprKind::Break(Some(label), _) | ast::ExprKind::Continue(Some(label)) => { + if let Some(index) = self.0.iter().rposition(|&l| l.ident == label.ident) { + self.0.remove(index); } } _ => {} @@ -511,17 +506,17 @@ fn check_expr(&mut self, _: &EarlyContext, expr: &ast::Expr) { fn check_expr_post(&mut self, ctxt: &EarlyContext, expr: &ast::Expr) { match expr.node { - ast::ExprKind::While(_, _, Some(ref label)) - | ast::ExprKind::WhileLet(_, _, _, Some(ref label)) - | ast::ExprKind::ForLoop(_, _, _, Some(ref label)) - | ast::ExprKind::Loop(_, Some(ref label)) => if !self.0.is_empty() { - { - let unused_label = self.0.last().unwrap(); - if label.ident.name == unused_label.ident.name { - ctxt.span_lint(UNUSED_LOOP_LABEL, label.ident.span, "unused loop label"); + ast::ExprKind::While(_, _, Some(label)) + | ast::ExprKind::WhileLet(_, _, _, Some(label)) + | ast::ExprKind::ForLoop(_, _, _, Some(label)) + | ast::ExprKind::Loop(_, Some(label)) => { + if let Some(unused_label) = self.0.pop() { + if label.ident == unused_label.ident { + ctxt.span_lint(UNUSED_LABEL, label.ident.span, "unused label"); + } else { + self.0.push(unused_label); } } - self.0.pop(); }, _ => {} } diff --git a/src/test/ui/lint/unused_label.rs b/src/test/ui/lint/unused_label.rs index 43cf8c75ae3f0bca2ef24930372d9c504eba9a35..ceb70fc542dc0dad2be87eaff25fe64ae4f095c7 100644 --- a/src/test/ui/lint/unused_label.rs +++ b/src/test/ui/lint/unused_label.rs @@ -13,20 +13,20 @@ // within nested loops // compile-pass -// compile-flags: -W unused_loop_label +// compile-flags: -W unused-label fn main() { 'unused_while_label: while 0 == 0 { - //~^ WARN unused loop label + //~^ WARN unused label } let opt = Some(0); 'unused_while_let_label: while let Some(_) = opt { - //~^ WARN unused loop label + //~^ WARN unused label } 'unused_for_label: for _ in 0..10 { - //~^ WARN unused loop label + //~^ WARN unused label } 'used_loop_label: loop { @@ -42,14 +42,14 @@ fn main() { 'used_loop_label_outer_2: loop { 'unused_loop_label_inner_2: loop { - //~^ WARN unused loop label + //~^ WARN unused label break 'used_loop_label_outer_2; } } 'unused_loop_label_outer_3: loop { + //~^ WARN unused label 'used_loop_label_inner_3: loop { - //~^ WARN unused loop label break 'used_loop_label_inner_3; } } @@ -57,6 +57,6 @@ fn main() { // This is diverging, so put it at the end so we don't get // unreachable_code errors everywhere else 'unused_loop_label: loop { - //~^ WARN unused loop label + //~^ WARN unused label } } diff --git a/src/test/ui/lint/unused_label.stderr b/src/test/ui/lint/unused_label.stderr index 3e7be476e6439958b5b0151d1cda51b9c6309f25..31f78b28ef5e96bf00b9b45cebe22482ccab0e81 100644 --- a/src/test/ui/lint/unused_label.stderr +++ b/src/test/ui/lint/unused_label.stderr @@ -25,13 +25,13 @@ LL | 'unused_loop_label_inner_2: loop { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: unused label - --> $DIR/unused_label.rs:50:9 + --> $DIR/unused_label.rs:50:5 | LL | 'unused_loop_label_outer_3: loop { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: unused label - --> $DIR/unused_label.rs:52:5 + --> $DIR/unused_label.rs:59:5 | LL | 'unused_loop_label: loop { | ^^^^^^^^^^^^^^^^^^