Add lint checks for unused loop labels
This commit is contained in:
@@ -110,6 +110,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
|
|||||||
|
|
||||||
add_early_builtin_with_new!(sess,
|
add_early_builtin_with_new!(sess,
|
||||||
DeprecatedAttr,
|
DeprecatedAttr,
|
||||||
|
UnusedLoopLabel,
|
||||||
);
|
);
|
||||||
|
|
||||||
add_builtin!(sess,
|
add_builtin!(sess,
|
||||||
@@ -177,7 +178,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
|
|||||||
UNUSED_DOC_COMMENT,
|
UNUSED_DOC_COMMENT,
|
||||||
UNUSED_EXTERN_CRATES,
|
UNUSED_EXTERN_CRATES,
|
||||||
UNUSED_FEATURES,
|
UNUSED_FEATURES,
|
||||||
UNUSED_PARENS);
|
UNUSED_PARENS,
|
||||||
|
UNUSED_LOOP_LABEL);
|
||||||
|
|
||||||
add_lint_group!(sess,
|
add_lint_group!(sess,
|
||||||
"rust_2018_idioms",
|
"rust_2018_idioms",
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ use syntax_pos::Span;
|
|||||||
|
|
||||||
use rustc::hir;
|
use rustc::hir;
|
||||||
|
|
||||||
|
use std::vec;
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
pub UNUSED_MUST_USE,
|
pub UNUSED_MUST_USE,
|
||||||
Warn,
|
Warn,
|
||||||
@@ -464,3 +466,64 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAllocation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare_lint! {
|
||||||
|
pub(super) UNUSED_LOOP_LABEL,
|
||||||
|
Warn,
|
||||||
|
"warns on unused labels for loops"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct UnusedLoopLabel(pub vec::Vec<ast::Label>);
|
||||||
|
|
||||||
|
impl UnusedLoopLabel {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
UnusedLoopLabel(vec![])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LintPass for UnusedLoopLabel {
|
||||||
|
fn get_lints(&self) -> LintArray {
|
||||||
|
lint_array!(UNUSED_LOOP_LABEL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EarlyLintPass for UnusedLoopLabel {
|
||||||
|
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::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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.0.pop();
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1705,7 +1705,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
|
|||||||
let mut trace_text = "\n\nAn error occurred in miri:\n".to_string();
|
let mut trace_text = "\n\nAn error occurred in miri:\n".to_string();
|
||||||
backtrace.resolve();
|
backtrace.resolve();
|
||||||
write!(trace_text, "backtrace frames: {}\n", backtrace.frames().len()).unwrap();
|
write!(trace_text, "backtrace frames: {}\n", backtrace.frames().len()).unwrap();
|
||||||
'frames: for (i, frame) in backtrace.frames().iter().enumerate() {
|
for (i, frame) in backtrace.frames().iter().enumerate() {
|
||||||
if frame.symbols().is_empty() {
|
if frame.symbols().is_empty() {
|
||||||
write!(trace_text, "{}: no symbols\n", i).unwrap();
|
write!(trace_text, "{}: no symbols\n", i).unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
55
src/test/ui/lint/unused_loop_label.rs
Normal file
55
src/test/ui/lint/unused_loop_label.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
// The output should warn when a loop label is not used. However, it
|
||||||
|
// should also deal with the edge cases where a label is shadowed,
|
||||||
|
// within nested loops
|
||||||
|
|
||||||
|
// compile-pass
|
||||||
|
// compile-flags: -W unused_loop_label
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
'unused_while_label: while 0 == 0 {
|
||||||
|
//~^ WARN unused loop label
|
||||||
|
}
|
||||||
|
|
||||||
|
let opt = Some(0);
|
||||||
|
'unused_while_let_label: while let Some(_) = opt {
|
||||||
|
//~^ WARN unused loop label
|
||||||
|
}
|
||||||
|
|
||||||
|
'unused_for_label: for _ in 0..10 {
|
||||||
|
//~^ WARN unused loop label
|
||||||
|
}
|
||||||
|
|
||||||
|
'used_loop_label: loop {
|
||||||
|
break 'used_loop_label;
|
||||||
|
}
|
||||||
|
|
||||||
|
'used_loop_label_outer: loop {
|
||||||
|
'used_loop_label_inner: loop {
|
||||||
|
break 'used_loop_label_inner;
|
||||||
|
}
|
||||||
|
break 'used_loop_label_outer;
|
||||||
|
}
|
||||||
|
|
||||||
|
'unused_loop_label_outer: loop {
|
||||||
|
'unused_loop_label_inner: loop {
|
||||||
|
//~^ WARN unused loop label
|
||||||
|
break 'unused_loop_label_outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/test/ui/lint/unused_loop_label.stderr
Normal file
32
src/test/ui/lint/unused_loop_label.stderr
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
warning: unused loop label
|
||||||
|
--> $DIR/unused_loop_label.rs:19:5
|
||||||
|
|
|
||||||
|
LL | 'unused_while_label: while 0 == 0 {
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: requested on the command line with `-W unused-loop-label`
|
||||||
|
|
||||||
|
warning: unused loop label
|
||||||
|
--> $DIR/unused_loop_label.rs:24:5
|
||||||
|
|
|
||||||
|
LL | 'unused_while_let_label: while let Some(_) = opt {
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: unused loop label
|
||||||
|
--> $DIR/unused_loop_label.rs:28:5
|
||||||
|
|
|
||||||
|
LL | 'unused_for_label: for _ in 0..10 {
|
||||||
|
| ^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: unused loop label
|
||||||
|
--> $DIR/unused_loop_label.rs:44:9
|
||||||
|
|
|
||||||
|
LL | 'unused_loop_label_inner: loop {
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: unused loop label
|
||||||
|
--> $DIR/unused_loop_label.rs:52:5
|
||||||
|
|
|
||||||
|
LL | 'unused_loop_label: loop {
|
||||||
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
0
src/test/ui/lint/unused_loop_label.stdout
Normal file
0
src/test/ui/lint/unused_loop_label.stdout
Normal file
Reference in New Issue
Block a user