]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_label.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / unused_label.rs
1 use crate::utils::{in_macro, span_lint};
2 use rustc::hir;
3 use rustc::hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use rustc_data_structures::fx::FxHashMap;
7 use syntax::source_map::Span;
8 use syntax::symbol::LocalInternedString;
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for unused labels.
12     ///
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.
15     ///
16     /// **Known problems:** Hopefully none.
17     ///
18     /// **Example:**
19     /// ```rust,ignore
20     /// fn unused_label() {
21     ///     'label: for i in 1..2 {
22     ///         if i > 4 { continue }
23     ///     }
24     /// ```
25     pub UNUSED_LABEL,
26     complexity,
27     "unused labels"
28 }
29
30 pub struct UnusedLabel;
31
32 struct UnusedLabelVisitor<'a, 'tcx: 'a> {
33     labels: FxHashMap<LocalInternedString, Span>,
34     cx: &'a LateContext<'a, 'tcx>,
35 }
36
37 impl LintPass for UnusedLabel {
38     fn get_lints(&self) -> LintArray {
39         lint_array!(UNUSED_LABEL)
40     }
41
42     fn name(&self) -> &'static str {
43         "UnusedLable"
44     }
45 }
46
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel {
48     fn check_fn(
49         &mut self,
50         cx: &LateContext<'a, 'tcx>,
51         kind: FnKind<'tcx>,
52         decl: &'tcx hir::FnDecl,
53         body: &'tcx hir::Body,
54         span: Span,
55         fn_id: hir::HirId,
56     ) {
57         if in_macro(span) {
58             return;
59         }
60
61         let mut v = UnusedLabelVisitor {
62             cx,
63             labels: FxHashMap::default(),
64         };
65         walk_fn(&mut v, kind, decl, body.id(), span, fn_id);
66
67         for (label, span) in v.labels {
68             span_lint(cx, UNUSED_LABEL, span, &format!("unused label `{}`", label));
69         }
70     }
71 }
72
73 impl<'a, 'tcx: 'a> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
74     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
75         match expr.node {
76             hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => {
77                 if let Some(label) = destination.label {
78                     self.labels.remove(&label.ident.as_str());
79                 }
80             },
81             hir::ExprKind::Loop(_, Some(label), _) | hir::ExprKind::While(_, _, Some(label)) => {
82                 self.labels.insert(label.ident.as_str(), expr.span);
83             },
84             _ => (),
85         }
86
87         walk_expr(self, expr);
88     }
89     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
90         NestedVisitorMap::All(&self.cx.tcx.hir())
91     }
92 }