Rename unused_loop_label to unused_label and fix/clean up lint logic

This commit is contained in:
Kyle Stachowicz 2018-05-15 17:36:43 -07:00
parent bb867d3512
commit acd6ab8f0f
4 changed files with 37 additions and 42 deletions

View file

@ -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",

View file

@ -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<ast::Label>);
pub struct UnusedLabel(pub Vec<ast::Label>);
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();
},
_ => {}
}

View file

@ -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
}
}

View file

@ -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 {
| ^^^^^^^^^^^^^^^^^^