]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/regex.rs
Auto merge of #4947 - rust-lang:doc-main-extern-crate, r=flip1995
[rust.git] / clippy_lints / src / regex.rs
index 4d1dbc75c94923d14f2d13cd5bb7846c740f3241..c60912ddb2cac57b7ec61ce42017b80b0a6d3309 100644 (file)
@@ -1,11 +1,11 @@
 use crate::consts::{constant, Constant};
-use crate::utils::{is_expn_of, match_type, paths, span_help_and_lint, span_lint};
+use crate::utils::{is_expn_of, match_def_path, match_type, paths, span_help_and_lint, span_lint};
 use if_chain::if_chain;
-use regex_syntax;
 use rustc::hir::*;
+use rustc::impl_lint_pass;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
 use rustc_data_structures::fx::FxHashSet;
+use rustc_session::declare_tool_lint;
 use std::convert::TryFrom;
 use syntax::ast::{LitKind, StrStyle};
 use syntax::source_map::{BytePos, Span};
 }
 
 #[derive(Clone, Default)]
-pub struct Pass {
+pub struct Regex {
     spans: FxHashSet<Span>,
     last: Option<HirId>,
 }
 
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(INVALID_REGEX, REGEX_MACRO, TRIVIAL_REGEX)
-    }
-
-    fn name(&self) -> &'static str {
-        "Regex"
-    }
-}
+impl_lint_pass!(Regex => [INVALID_REGEX, REGEX_MACRO, TRIVIAL_REGEX]);
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
-    fn check_crate(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Regex {
+    fn check_crate(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx Crate<'_>) {
         self.spans.clear();
     }
 
@@ -115,20 +107,20 @@ fn check_block_post(&mut self, _: &LateContext<'a, 'tcx>, block: &'tcx Block) {
 
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if_chain! {
-            if let ExprKind::Call(ref fun, ref args) = expr.node;
-            if let ExprKind::Path(ref qpath) = fun.node;
+            if let ExprKind::Call(ref fun, ref args) = expr.kind;
+            if let ExprKind::Path(ref qpath) = fun.kind;
             if args.len() == 1;
-            if let Some(def_id) = cx.tables.qpath_def(qpath, fun.hir_id).opt_def_id();
+            if let Some(def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id();
             then {
-                if cx.match_def_path(def_id, &paths::REGEX_NEW) ||
-                   cx.match_def_path(def_id, &paths::REGEX_BUILDER_NEW) {
+                if match_def_path(cx, def_id, &paths::REGEX_NEW) ||
+                   match_def_path(cx, def_id, &paths::REGEX_BUILDER_NEW) {
                     check_regex(cx, &args[0], true);
-                } else if cx.match_def_path(def_id, &paths::REGEX_BYTES_NEW) ||
-                   cx.match_def_path(def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
+                } else if match_def_path(cx, def_id, &paths::REGEX_BYTES_NEW) ||
+                   match_def_path(cx, def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
                     check_regex(cx, &args[0], false);
-                } else if cx.match_def_path(def_id, &paths::REGEX_SET_NEW) {
+                } else if match_def_path(cx, def_id, &paths::REGEX_SET_NEW) {
                     check_set(cx, &args[0], true);
-                } else if cx.match_def_path(def_id, &paths::REGEX_BYTES_SET_NEW) {
+                } else if match_def_path(cx, def_id, &paths::REGEX_BYTES_SET_NEW) {
                     check_set(cx, &args[0], false);
                 }
             }
@@ -137,6 +129,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
 }
 
 #[allow(clippy::cast_possible_truncation)] // truncation very unlikely here
+#[must_use]
 fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
     let offset = u32::from(offset);
     let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
@@ -193,8 +186,8 @@ fn is_trivial_regex(s: &regex_syntax::hir::Hir) -> Option<&'static str> {
 
 fn check_set<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, utf8: bool) {
     if_chain! {
-        if let ExprKind::AddrOf(_, ref expr) = expr.node;
-        if let ExprKind::Array(ref exprs) = expr.node;
+        if let ExprKind::AddrOf(BorrowKind::Ref, _, ref expr) = expr.kind;
+        if let ExprKind::Array(ref exprs) = expr.kind;
         then {
             for expr in exprs {
                 check_regex(cx, expr, utf8);
@@ -209,7 +202,7 @@ fn check_regex<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, utf8: boo
         .allow_invalid_utf8(!utf8)
         .build();
 
-    if let ExprKind::Lit(ref lit) = expr.node {
+    if let ExprKind::Lit(ref lit) = expr.kind {
         if let LitKind::Str(ref r, style) = lit.node {
             let r = &r.as_str();
             let offset = if let StrStyle::Raw(n) = style { 2 + n } else { 1 };