2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{in_macro, span_lint};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor};
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::source_map::Span;
|
|
|
|
use syntax::symbol::LocalInternedString;
|
2016-03-01 08:15:39 -06:00
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for unused labels.
|
2016-03-01 08:15:39 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05: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 08:15:39 -06:00
|
|
|
///
|
|
|
|
/// **Known problems:** Hopefully none.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// fn unused_label() {
|
|
|
|
/// 'label: for i in 1..2 {
|
|
|
|
/// if i > 4 { continue }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-03-01 08:15:39 -06:00
|
|
|
pub UNUSED_LABEL,
|
2018-03-28 08:24:26 -05:00
|
|
|
complexity,
|
2016-08-06 03:18:36 -05:00
|
|
|
"unused labels"
|
2016-03-01 08:15:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnusedLabel;
|
|
|
|
|
2016-12-06 04:32:21 -06:00
|
|
|
struct UnusedLabelVisitor<'a, 'tcx: 'a> {
|
2018-09-11 18:34:52 -05:00
|
|
|
labels: FxHashMap<LocalInternedString, Span>,
|
2016-12-06 04:32:21 -06:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2016-03-01 08:15:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnusedLabel {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_LABEL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel {
|
2016-12-21 03:25:14 -06:00
|
|
|
fn check_fn(
|
2016-12-21 05:14:54 -06:00
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
kind: FnKind<'tcx>,
|
|
|
|
decl: &'tcx hir::FnDecl,
|
2017-01-03 22:40:42 -06:00
|
|
|
body: &'tcx hir::Body,
|
2016-12-21 05:14:54 -06:00
|
|
|
span: Span,
|
2017-08-09 02:30:56 -05:00
|
|
|
fn_id: ast::NodeId,
|
2016-12-21 03:25:14 -06:00
|
|
|
) {
|
2017-03-31 17:14:04 -05:00
|
|
|
if in_macro(span) {
|
2016-03-01 08:15:39 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:32:21 -06:00
|
|
|
let mut v = UnusedLabelVisitor {
|
2018-03-15 10:07:15 -05:00
|
|
|
cx,
|
2018-09-11 18:34:52 -05:00
|
|
|
labels: FxHashMap::default(),
|
2016-12-06 04:32:21 -06:00
|
|
|
};
|
2017-01-03 22:40:42 -06:00
|
|
|
walk_fn(&mut v, kind, decl, body.id(), span, fn_id);
|
2016-03-01 08:15:39 -06:00
|
|
|
|
|
|
|
for (label, span) in v.labels {
|
|
|
|
span_lint(cx, UNUSED_LABEL, span, &format!("unused label `{}`", label));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:32:21 -06:00
|
|
|
impl<'a, 'tcx: 'a> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2016-03-01 08:15:39 -06:00
|
|
|
match expr.node {
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => {
|
|
|
|
if let Some(label) = destination.label {
|
|
|
|
self.labels.remove(&label.ident.as_str());
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
hir::ExprKind::Loop(_, Some(label), _) | hir::ExprKind::While(_, _, Some(label)) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.labels.insert(label.ident.as_str(), expr.span);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-01 08:15:39 -06:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
2016-12-06 04:32:21 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-07 18:56:03 -06:00
|
|
|
NestedVisitorMap::All(&self.cx.tcx.hir())
|
2016-12-06 04:32:21 -06:00
|
|
|
}
|
2016-03-01 08:15:39 -06:00
|
|
|
}
|