From acd6ab8f0f00365b424ac9cd83d9d08852add0bc Mon Sep 17 00:00:00 2001 From: Kyle Stachowicz Date: Tue, 15 May 2018 17:36:43 -0700 Subject: [PATCH] Rename `unused_loop_label` to `unused_label` and fix/clean up lint logic --- src/librustc_lint/lib.rs | 6 +-- src/librustc_lint/unused.rs | 55 +++++++++++++--------------- src/test/ui/lint/unused_label.rs | 14 +++---- src/test/ui/lint/unused_label.stderr | 4 +- 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 19e0bbd8305..b0a766ec058 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 38c5a36067c..61c0485f886 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -25,8 +25,6 @@ use syntax_pos::Span; use rustc::hir; -use std::vec; - declare_lint! { pub UNUSED_MUST_USE, Warn, @@ -468,41 +466,38 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAllocation { } 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 @@ impl EarlyLintPass for UnusedLoopLabel { 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 43cf8c75ae3..ceb70fc542d 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 3e7be476e64..31f78b28ef5 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 { | ^^^^^^^^^^^^^^^^^^