]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_label.rs
Auto merge of #3700 - phansch:would_you_like_some_help_with_this_const_fn, r=oli-obk
[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::ast;
8 use syntax::source_map::Span;
9 use syntax::symbol::LocalInternedString;
10
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 declare_clippy_lint! {
26     pub UNUSED_LABEL,
27     complexity,
28     "unused labels"
29 }
30
31 pub struct UnusedLabel;
32
33 struct UnusedLabelVisitor<'a, 'tcx: 'a> {
34     labels: FxHashMap<LocalInternedString, Span>,
35     cx: &'a LateContext<'a, 'tcx>,
36 }
37
38 impl LintPass for UnusedLabel {
39     fn get_lints(&self) -> LintArray {
40         lint_array!(UNUSED_LABEL)
41     }
42
43     fn name(&self) -> &'static str {
44         "UnusedLable"
45     }
46 }
47
48 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel {
49     fn check_fn(
50         &mut self,
51         cx: &LateContext<'a, 'tcx>,
52         kind: FnKind<'tcx>,
53         decl: &'tcx hir::FnDecl,
54         body: &'tcx hir::Body,
55         span: Span,
56         fn_id: ast::NodeId,
57     ) {
58         if in_macro(span) {
59             return;
60         }
61
62         let mut v = UnusedLabelVisitor {
63             cx,
64             labels: FxHashMap::default(),
65         };
66         walk_fn(&mut v, kind, decl, body.id(), span, fn_id);
67
68         for (label, span) in v.labels {
69             span_lint(cx, UNUSED_LABEL, span, &format!("unused label `{}`", label));
70         }
71     }
72 }
73
74 impl<'a, 'tcx: 'a> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
75     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
76         match expr.node {
77             hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => {
78                 if let Some(label) = destination.label {
79                     self.labels.remove(&label.ident.as_str());
80                 }
81             },
82             hir::ExprKind::Loop(_, Some(label), _) | hir::ExprKind::While(_, _, Some(label)) => {
83                 self.labels.insert(label.ident.as_str(), expr.span);
84             },
85             _ => (),
86         }
87
88         walk_expr(self, expr);
89     }
90     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
91         NestedVisitorMap::All(&self.cx.tcx.hir())
92     }
93 }