1 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use crate::rustc::{declare_tool_lint, lint_array};
4 use crate::rustc::hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor};
5 use crate::rustc_data_structures::fx::FxHashMap;
6 use crate::syntax::ast;
7 use crate::syntax::source_map::Span;
8 use crate::syntax::symbol::LocalInternedString;
9 use crate::utils::{in_macro, span_lint};
11 /// **What it does:** Checks for unused labels.
13 /// **Why is this bad?** Maybe the label should be used in which case there is
14 /// an error in the code or it should be removed.
16 /// **Known problems:** Hopefully none.
20 /// fn unused_label() {
21 /// 'label: for i in 1..2 {
22 /// if i > 4 { continue }
25 declare_clippy_lint! {
31 pub struct UnusedLabel;
33 struct UnusedLabelVisitor<'a, 'tcx: 'a> {
34 labels: FxHashMap<LocalInternedString, Span>,
35 cx: &'a LateContext<'a, 'tcx>,
38 impl LintPass for UnusedLabel {
39 fn get_lints(&self) -> LintArray {
40 lint_array!(UNUSED_LABEL)
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel {
47 cx: &LateContext<'a, 'tcx>,
49 decl: &'tcx hir::FnDecl,
50 body: &'tcx hir::Body,
58 let mut v = UnusedLabelVisitor {
60 labels: FxHashMap::default(),
62 walk_fn(&mut v, kind, decl, body.id(), span, fn_id);
64 for (label, span) in v.labels {
65 span_lint(cx, UNUSED_LABEL, span, &format!("unused label `{}`", label));
70 impl<'a, 'tcx: 'a> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
71 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
73 hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => if let Some(label) = destination.label {
74 self.labels.remove(&label.ident.as_str());
76 hir::ExprKind::Loop(_, Some(label), _) | hir::ExprKind::While(_, _, Some(label)) => {
77 self.labels.insert(label.ident.as_str(), expr.span);
82 walk_expr(self, expr);
84 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
85 NestedVisitorMap::All(&self.cx.tcx.hir)