X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Funused_label.rs;h=60acbc1469f900e78459ffc70821b712c9a69983;hb=e5a5b0a0774625eebbe7b29c67b49dc6431544d1;hp=71d9520c05be47e78130eedb7649437fc2915681;hpb=846c3dba2cbb959b3ac2be87fb3cb8d9253acb9f;p=rust.git diff --git a/clippy_lints/src/unused_label.rs b/clippy_lints/src/unused_label.rs index 71d9520c05b..60acbc1469f 100644 --- a/clippy_lints/src/unused_label.rs +++ b/clippy_lints/src/unused_label.rs @@ -1,45 +1,39 @@ -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_tool_lint, lint_array}; +use crate::utils::span_lint; +use rustc::declare_lint_pass; use rustc::hir; use rustc::hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor}; -use std::collections::HashMap; -use syntax::ast; +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; +use rustc_data_structures::fx::FxHashMap; +use rustc_session::declare_tool_lint; use syntax::source_map::Span; -use syntax::symbol::LocalInternedString; -use crate::utils::{in_macro, span_lint}; +use syntax::symbol::Symbol; -/// **What it does:** Checks for unused labels. -/// -/// **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. -/// -/// **Known problems:** Hopefully none. -/// -/// **Example:** -/// ```rust,ignore -/// fn unused_label() { -/// 'label: for i in 1..2 { -/// if i > 4 { continue } -/// } -/// ``` declare_clippy_lint! { + /// **What it does:** Checks for unused labels. + /// + /// **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. + /// + /// **Known problems:** Hopefully none. + /// + /// **Example:** + /// ```rust,ignore + /// fn unused_label() { + /// 'label: for i in 1..2 { + /// if i > 4 { continue } + /// } + /// ``` pub UNUSED_LABEL, complexity, "unused labels" } -pub struct UnusedLabel; - -struct UnusedLabelVisitor<'a, 'tcx: 'a> { - labels: HashMap, +struct UnusedLabelVisitor<'a, 'tcx> { + labels: FxHashMap, cx: &'a LateContext<'a, 'tcx>, } -impl LintPass for UnusedLabel { - fn get_lints(&self) -> LintArray { - lint_array!(UNUSED_LABEL) - } -} +declare_lint_pass!(UnusedLabel => [UNUSED_LABEL]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel { fn check_fn( @@ -49,15 +43,15 @@ fn check_fn( decl: &'tcx hir::FnDecl, body: &'tcx hir::Body, span: Span, - fn_id: ast::NodeId, + fn_id: hir::HirId, ) { - if in_macro(span) { + if span.from_expansion() { return; } let mut v = UnusedLabelVisitor { cx, - labels: HashMap::new(), + labels: FxHashMap::default(), }; walk_fn(&mut v, kind, decl, body.id(), span, fn_id); @@ -67,14 +61,16 @@ fn check_fn( } } -impl<'a, 'tcx: 'a> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> { +impl<'a, 'tcx> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx hir::Expr) { - match expr.node { - hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => if let Some(label) = destination.label { - self.labels.remove(&label.ident.as_str()); + match expr.kind { + hir::ExprKind::Break(destination, _) | hir::ExprKind::Continue(destination) => { + if let Some(label) = destination.label { + self.labels.remove(&label.ident.name); + } }, - hir::ExprKind::Loop(_, Some(label), _) | hir::ExprKind::While(_, _, Some(label)) => { - self.labels.insert(label.ident.as_str(), expr.span); + hir::ExprKind::Loop(_, Some(label), _) => { + self.labels.insert(label.ident.name, expr.span); }, _ => (), } @@ -82,6 +78,6 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr) { walk_expr(self, expr); } fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { - NestedVisitorMap::All(&self.cx.tcx.hir) + NestedVisitorMap::All(&self.cx.tcx.hir()) } }