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
"referencing const with interior mutability"
}
+#[allow(dead_code)]
#[derive(Copy, Clone)]
enum Source {
Item { item: Span },
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,
}
}
-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`
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",
});
}
-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) {
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);
}
// 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,
};
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.