Skip to content

Commit acd6ab8

Browse files
committed
Rename unused_loop_label to unused_label and fix/clean up lint logic
1 parent bb867d3 commit acd6ab8

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

src/librustc_lint/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
110110

111111
add_early_builtin_with_new!(sess,
112112
DeprecatedAttr,
113-
UnusedLoopLabel,
113+
UnusedLabel,
114114
);
115115

116116
add_builtin!(sess,
@@ -178,8 +178,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
178178
UNUSED_DOC_COMMENT,
179179
UNUSED_EXTERN_CRATES,
180180
UNUSED_FEATURES,
181-
UNUSED_PARENS,
182-
UNUSED_LOOP_LABEL);
181+
UNUSED_LABEL,
182+
UNUSED_PARENS);
183183

184184
add_lint_group!(sess,
185185
"rust_2018_idioms",

src/librustc_lint/unused.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ use syntax_pos::Span;
2525

2626
use rustc::hir;
2727

28-
use std::vec;
29-
3028
declare_lint! {
3129
pub UNUSED_MUST_USE,
3230
Warn,
@@ -468,41 +466,38 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAllocation {
468466
}
469467

470468
declare_lint! {
471-
pub(super) UNUSED_LOOP_LABEL,
469+
pub(super) UNUSED_LABEL,
472470
Warn,
473-
"warns on unused labels for loops"
471+
"warns on unused labels"
474472
}
475473

476474
#[derive(Clone)]
477-
pub struct UnusedLoopLabel(pub vec::Vec<ast::Label>);
475+
pub struct UnusedLabel(pub Vec<ast::Label>);
478476

479-
impl UnusedLoopLabel {
477+
impl UnusedLabel {
480478
pub fn new() -> Self {
481-
UnusedLoopLabel(vec![])
479+
UnusedLabel(vec![])
482480
}
483481
}
484482

485-
impl LintPass for UnusedLoopLabel {
483+
impl LintPass for UnusedLabel {
486484
fn get_lints(&self) -> LintArray {
487-
lint_array!(UNUSED_LOOP_LABEL)
485+
lint_array!(UNUSED_LABEL)
488486
}
489487
}
490488

491-
impl EarlyLintPass for UnusedLoopLabel {
489+
impl EarlyLintPass for UnusedLabel {
492490
fn check_expr(&mut self, _: &EarlyContext, expr: &ast::Expr) {
493491
match expr.node {
494-
ast::ExprKind::While(_, _, Some(ref label))
495-
| ast::ExprKind::WhileLet(_, _, _, Some(ref label))
496-
| ast::ExprKind::ForLoop(_, _, _, Some(ref label))
497-
| ast::ExprKind::Loop(_, Some(ref label)) => {
498-
self.0.push(*label);
492+
ast::ExprKind::While(_, _, Some(label))
493+
| ast::ExprKind::WhileLet(_, _, _, Some(label))
494+
| ast::ExprKind::ForLoop(_, _, _, Some(label))
495+
| ast::ExprKind::Loop(_, Some(label)) => {
496+
self.0.push(label);
499497
}
500-
ast::ExprKind::Break(Some(ref label), _) | ast::ExprKind::Continue(Some(ref label)) => {
501-
'remove_used_label: for i in (0..self.0.len()).rev() {
502-
if self.0.get(i).unwrap().ident.name == label.ident.name {
503-
self.0.remove(i);
504-
break 'remove_used_label;
505-
}
498+
ast::ExprKind::Break(Some(label), _) | ast::ExprKind::Continue(Some(label)) => {
499+
if let Some(index) = self.0.iter().rposition(|&l| l.ident == label.ident) {
500+
self.0.remove(index);
506501
}
507502
}
508503
_ => {}
@@ -511,17 +506,17 @@ impl EarlyLintPass for UnusedLoopLabel {
511506

512507
fn check_expr_post(&mut self, ctxt: &EarlyContext, expr: &ast::Expr) {
513508
match expr.node {
514-
ast::ExprKind::While(_, _, Some(ref label))
515-
| ast::ExprKind::WhileLet(_, _, _, Some(ref label))
516-
| ast::ExprKind::ForLoop(_, _, _, Some(ref label))
517-
| ast::ExprKind::Loop(_, Some(ref label)) => if !self.0.is_empty() {
518-
{
519-
let unused_label = self.0.last().unwrap();
520-
if label.ident.name == unused_label.ident.name {
521-
ctxt.span_lint(UNUSED_LOOP_LABEL, label.ident.span, "unused loop label");
509+
ast::ExprKind::While(_, _, Some(label))
510+
| ast::ExprKind::WhileLet(_, _, _, Some(label))
511+
| ast::ExprKind::ForLoop(_, _, _, Some(label))
512+
| ast::ExprKind::Loop(_, Some(label)) => {
513+
if let Some(unused_label) = self.0.pop() {
514+
if label.ident == unused_label.ident {
515+
ctxt.span_lint(UNUSED_LABEL, label.ident.span, "unused label");
516+
} else {
517+
self.0.push(unused_label);
522518
}
523519
}
524-
self.0.pop();
525520
},
526521
_ => {}
527522
}

src/test/ui/lint/unused_label.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
// within nested loops
1414

1515
// compile-pass
16-
// compile-flags: -W unused_loop_label
16+
// compile-flags: -W unused-label
1717

1818
fn main() {
1919
'unused_while_label: while 0 == 0 {
20-
//~^ WARN unused loop label
20+
//~^ WARN unused label
2121
}
2222

2323
let opt = Some(0);
2424
'unused_while_let_label: while let Some(_) = opt {
25-
//~^ WARN unused loop label
25+
//~^ WARN unused label
2626
}
2727

2828
'unused_for_label: for _ in 0..10 {
29-
//~^ WARN unused loop label
29+
//~^ WARN unused label
3030
}
3131

3232
'used_loop_label: loop {
@@ -42,21 +42,21 @@ fn main() {
4242

4343
'used_loop_label_outer_2: loop {
4444
'unused_loop_label_inner_2: loop {
45-
//~^ WARN unused loop label
45+
//~^ WARN unused label
4646
break 'used_loop_label_outer_2;
4747
}
4848
}
4949

5050
'unused_loop_label_outer_3: loop {
51+
//~^ WARN unused label
5152
'used_loop_label_inner_3: loop {
52-
//~^ WARN unused loop label
5353
break 'used_loop_label_inner_3;
5454
}
5555
}
5656

5757
// This is diverging, so put it at the end so we don't get
5858
// unreachable_code errors everywhere else
5959
'unused_loop_label: loop {
60-
//~^ WARN unused loop label
60+
//~^ WARN unused label
6161
}
6262
}

src/test/ui/lint/unused_label.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ LL | 'unused_loop_label_inner_2: loop {
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
warning: unused label
28-
--> $DIR/unused_label.rs:50:9
28+
--> $DIR/unused_label.rs:50:5
2929
|
3030
LL | 'unused_loop_label_outer_3: loop {
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
warning: unused label
34-
--> $DIR/unused_label.rs:52:5
34+
--> $DIR/unused_label.rs:59:5
3535
|
3636
LL | 'unused_loop_label: loop {
3737
| ^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)