2016-03-01 14:15:39 +00:00
|
|
|
use rustc::lint::*;
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir;
|
2016-12-06 10:32:21 +00:00
|
|
|
use rustc::hir::intravisit::{FnKind, Visitor, walk_expr, walk_fn, NestedVisitorMap};
|
2016-03-01 14:15:39 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
2016-11-23 20:19:03 +00:00
|
|
|
use syntax::symbol::InternedString;
|
2016-03-01 14:15:39 +00:00
|
|
|
use utils::{in_macro, span_lint};
|
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for unused labels.
|
2016-03-01 14:15:39 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** Maybe the label should be used in which case there is
|
|
|
|
/// an error in the code or it should be removed.
|
2016-03-01 14:15:39 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** Hopefully none.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// fn unused_label() {
|
|
|
|
/// 'label: for i in 1..2 {
|
|
|
|
/// if i > 4 { continue }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_LABEL,
|
|
|
|
Warn,
|
2016-08-06 08:18:36 +00:00
|
|
|
"unused labels"
|
2016-03-01 14:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnusedLabel;
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
struct UnusedLabelVisitor<'a, 'tcx: 'a> {
|
2016-03-01 14:15:39 +00:00
|
|
|
labels: HashMap<InternedString, Span>,
|
2016-12-06 10:32:21 +00:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2016-03-01 14:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnusedLabel {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_LABEL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel {
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_fn(
|
2016-12-21 11:14:54 +00:00
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
kind: FnKind<'tcx>,
|
|
|
|
decl: &'tcx hir::FnDecl,
|
|
|
|
body: &'tcx hir::Expr,
|
|
|
|
span: Span,
|
|
|
|
fn_id: ast::NodeId
|
2016-12-21 09:25:14 +00:00
|
|
|
) {
|
2016-03-01 14:15:39 +00:00
|
|
|
if in_macro(cx, span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
let mut v = UnusedLabelVisitor {
|
|
|
|
cx: cx,
|
|
|
|
labels: HashMap::new(),
|
|
|
|
};
|
|
|
|
walk_fn(&mut v, kind, decl, body.expr_id(), span, fn_id);
|
2016-03-01 14:15:39 +00:00
|
|
|
|
|
|
|
for (label, span) in v.labels {
|
|
|
|
span_lint(cx, UNUSED_LABEL, span, &format!("unused label `{}`", label));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
impl<'a, 'tcx: 'a> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2016-03-01 14:15:39 +00:00
|
|
|
match expr.node {
|
2016-11-25 18:24:55 +00:00
|
|
|
hir::ExprBreak(Some(label), _) |
|
2016-06-05 23:42:39 +00:00
|
|
|
hir::ExprAgain(Some(label)) => {
|
2016-12-01 21:31:56 +00:00
|
|
|
self.labels.remove(&label.name.as_str());
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-11-25 18:24:55 +00:00
|
|
|
hir::ExprLoop(_, Some(label), _) |
|
2016-06-05 23:42:39 +00:00
|
|
|
hir::ExprWhile(_, _, Some(label)) => {
|
2016-05-27 12:24:28 +00:00
|
|
|
self.labels.insert(label.node.as_str(), expr.span);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-03-01 14:15:39 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
2016-12-06 10:32:21 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::All(&self.cx.tcx.map)
|
|
|
|
}
|
2016-03-01 14:15:39 +00:00
|
|
|
}
|