]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/non_copy_const.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / non_copy_const.rs
index 9c184e8c54b28edb7e11334dfdef4535ae0abcfb..992baa05e78e7050c37d86960922748e070487f1 100644 (file)
@@ -4,17 +4,17 @@
 
 use std::ptr;
 
-use rustc::hir::def::Def;
+use rustc::hir::def::{DefKind, Res};
 use rustc::hir::*;
 use rustc::lint::{LateContext, LateLintPass, Lint, LintArray, LintPass};
 use rustc::ty::adjustment::Adjust;
-use rustc::ty::{self, TypeFlags};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::ty::{Ty, TypeFlags};
+use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
 use rustc_typeck::hir_ty_to_ty;
-use syntax_pos::{Span, DUMMY_SP};
+use syntax_pos::{InnerSpan, Span, DUMMY_SP};
 
-use crate::utils::{in_constant, in_macro, is_copy, span_lint_and_then};
+use crate::utils::{in_constant, is_copy, qpath_res, span_lint_and_then};
 
 declare_clippy_lint! {
     /// **What it does:** Checks for declaration of `const` items which is interior
@@ -84,6 +84,7 @@
     "referencing const with interior mutability"
 }
 
+#[allow(dead_code)]
 #[derive(Copy, Clone)]
 enum Source {
     Item { item: Span },
@@ -94,12 +95,12 @@ enum Source {
 impl Source {
     fn lint(&self) -> (&'static Lint, &'static str, Span) {
         match self {
-            Source::Item { item } | Source::Assoc { item, .. } => (
+            Self::Item { item } | Self::Assoc { item, .. } => (
                 DECLARE_INTERIOR_MUTABLE_CONST,
                 "a const item should never be interior mutable",
                 *item,
             ),
-            Source::Expr { expr } => (
+            Self::Expr { expr } => (
                 BORROW_INTERIOR_MUTABLE_CONST,
                 "a const item with interior mutability should not be borrowed",
                 *expr,
@@ -108,7 +109,7 @@ fn lint(&self) -> (&'static Lint, &'static str, Span) {
     }
 }
 
-fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>, source: Source) {
+fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>, source: Source) {
     if ty.is_freeze(cx.tcx, cx.param_env, DUMMY_SP) || is_copy(cx, ty) {
         // An `UnsafeCell` is `!Copy`, and an `UnsafeCell` is also the only type which
         // is `!Freeze`, thus if our type is `Copy` we can be sure it must be `Freeze`
@@ -118,12 +119,12 @@ fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>, sourc
 
     let (lint, msg, span) = source.lint();
     span_lint_and_then(cx, lint, span, msg, |db| {
-        if in_macro(span) {
+        if span.from_expansion() {
             return; // Don't give suggestions into macros.
         }
         match source {
             Source::Item { .. } => {
-                let const_kw_span = span.from_inner_byte_pos(0, 5);
+                let const_kw_span = span.from_inner(InnerSpan::new(0, 5));
                 db.span_suggestion(
                     const_kw_span,
                     "make this a static item",
@@ -143,17 +144,7 @@ fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>, sourc
     });
 }
 
-pub struct NonCopyConst;
-
-impl LintPass for NonCopyConst {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(DECLARE_INTERIOR_MUTABLE_CONST, BORROW_INTERIOR_MUTABLE_CONST)
-    }
-
-    fn name(&self) -> &'static str {
-        "NonCopyConst"
-    }
-}
+declare_lint_pass!(NonCopyConst => [DECLARE_INTERIOR_MUTABLE_CONST, BORROW_INTERIOR_MUTABLE_CONST]);
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst {
     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx Item) {
@@ -179,8 +170,8 @@ fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx Tra
 
     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx ImplItem) {
         if let ImplItemKind::Const(hir_ty, ..) = &impl_item.node {
-            let item_hir_id = cx.tcx.hir().get_parent_node_by_hir_id(impl_item.hir_id);
-            let item = cx.tcx.hir().expect_item_by_hir_id(item_hir_id);
+            let item_hir_id = cx.tcx.hir().get_parent_node(impl_item.hir_id);
+            let item = cx.tcx.hir().expect_item(item_hir_id);
             // Ensure the impl is an inherent impl.
             if let ItemKind::Impl(_, _, _, _, None, _, _) = item.node {
                 let ty = hir_ty_to_ty(cx.tcx, hir_ty);
@@ -204,8 +195,8 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
             }
 
             // Make sure it is a const item.
-            match cx.tables.qpath_def(qpath, expr.hir_id) {
-                Def::Const(_) | Def::AssociatedConst(_) => {},
+            match qpath_res(cx, qpath, expr.hir_id) {
+                Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => {},
                 _ => return,
             };
 
@@ -214,11 +205,11 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
             let mut dereferenced_expr = expr;
             let mut needs_check_adjustment = true;
             loop {
-                let parent_id = cx.tcx.hir().get_parent_node_by_hir_id(cur_expr.hir_id);
+                let parent_id = cx.tcx.hir().get_parent_node(cur_expr.hir_id);
                 if parent_id == cur_expr.hir_id {
                     break;
                 }
-                if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find_by_hir_id(parent_id) {
+                if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find(parent_id) {
                     match &parent_expr.node {
                         ExprKind::AddrOf(..) => {
                             // `&e` => `e` must be referenced.