2019-01-31 01:15:29 +00:00
|
|
|
//! Checks for uses of const which the type is not `Freeze` (`Cell`-free).
|
2018-06-06 15:20:22 +00:00
|
|
|
//!
|
|
|
|
//! This lint is **deny** by default.
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
use std::ptr;
|
|
|
|
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, Lint};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty::adjustment::Adjust;
|
|
|
|
use rustc_middle::ty::{Ty, TypeFlags};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-12-31 00:17:56 +00:00
|
|
|
use rustc_span::{InnerSpan, Span, DUMMY_SP};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
2018-06-06 15:20:22 +00:00
|
|
|
|
2019-09-18 17:29:04 +00:00
|
|
|
use crate::utils::{in_constant, is_copy, qpath_res, span_lint_and_then};
|
2019-01-31 01:15:29 +00:00
|
|
|
|
2018-06-06 15:20:22 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for declaration of `const` items which is interior
|
2019-01-31 01:15:29 +00:00
|
|
|
/// mutable (e.g., contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.).
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2019-01-31 01:15:29 +00:00
|
|
|
/// **Why is this bad?** Consts are copied everywhere they are referenced, i.e.,
|
2019-03-05 16:50:33 +00:00
|
|
|
/// every time you refer to the const a fresh instance of the `Cell` or `Mutex`
|
|
|
|
/// or `AtomicXxxx` will be created, which defeats the whole purpose of using
|
|
|
|
/// these types in the first place.
|
|
|
|
///
|
|
|
|
/// The `const` should better be replaced by a `static` item if a global
|
|
|
|
/// variable is wanted, or replaced by a `const fn` if a constructor is wanted.
|
|
|
|
///
|
|
|
|
/// **Known problems:** A "non-constant" const item is a legacy way to supply an
|
2019-01-31 01:15:29 +00:00
|
|
|
/// initialized value to downstream `static` items (e.g., the
|
2019-03-05 16:50:33 +00:00
|
|
|
/// `std::sync::ONCE_INIT` constant). In this case the use of `const` is legit,
|
|
|
|
/// and this lint should be suppressed.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
|
|
|
///
|
|
|
|
/// // Bad.
|
|
|
|
/// const CONST_ATOM: AtomicUsize = AtomicUsize::new(12);
|
|
|
|
/// CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged
|
|
|
|
/// assert_eq!(CONST_ATOM.load(SeqCst), 12); // because the CONST_ATOM in these lines are distinct
|
|
|
|
///
|
|
|
|
/// // Good.
|
|
|
|
/// static STATIC_ATOM: AtomicUsize = AtomicUsize::new(15);
|
|
|
|
/// STATIC_ATOM.store(9, SeqCst);
|
|
|
|
/// assert_eq!(STATIC_ATOM.load(SeqCst), 9); // use a `static` item to refer to the same instance
|
|
|
|
/// ```
|
2018-06-06 15:20:22 +00:00
|
|
|
pub DECLARE_INTERIOR_MUTABLE_CONST,
|
|
|
|
correctness,
|
2020-01-06 06:30:43 +00:00
|
|
|
"declaring `const` with interior mutability"
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-01-31 01:15:29 +00:00
|
|
|
/// **What it does:** Checks if `const` items which is interior mutable (e.g.,
|
|
|
|
/// contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.) has been borrowed directly.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2019-01-31 01:15:29 +00:00
|
|
|
/// **Why is this bad?** Consts are copied everywhere they are referenced, i.e.,
|
2019-03-05 16:50:33 +00:00
|
|
|
/// every time you refer to the const a fresh instance of the `Cell` or `Mutex`
|
|
|
|
/// or `AtomicXxxx` will be created, which defeats the whole purpose of using
|
|
|
|
/// these types in the first place.
|
|
|
|
///
|
|
|
|
/// The `const` value should be stored inside a `static` item.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
|
|
|
/// const CONST_ATOM: AtomicUsize = AtomicUsize::new(12);
|
|
|
|
///
|
|
|
|
/// // Bad.
|
|
|
|
/// CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged
|
|
|
|
/// assert_eq!(CONST_ATOM.load(SeqCst), 12); // because the CONST_ATOM in these lines are distinct
|
|
|
|
///
|
|
|
|
/// // Good.
|
|
|
|
/// static STATIC_ATOM: AtomicUsize = CONST_ATOM;
|
|
|
|
/// STATIC_ATOM.store(9, SeqCst);
|
|
|
|
/// assert_eq!(STATIC_ATOM.load(SeqCst), 9); // use a `static` item to refer to the same instance
|
|
|
|
/// ```
|
2018-06-06 15:20:22 +00:00
|
|
|
pub BORROW_INTERIOR_MUTABLE_CONST,
|
|
|
|
correctness,
|
2020-01-06 06:30:43 +00:00
|
|
|
"referencing `const` with interior mutability"
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 00:25:35 +00:00
|
|
|
#[allow(dead_code)]
|
2018-06-06 15:20:22 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Source {
|
2018-11-27 20:14:15 +00:00
|
|
|
Item { item: Span },
|
|
|
|
Assoc { item: Span, ty: Span },
|
|
|
|
Expr { expr: Span },
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Source {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-06-06 15:20:22 +00:00
|
|
|
fn lint(&self) -> (&'static Lint, &'static str, Span) {
|
|
|
|
match self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::Item { item } | Self::Assoc { item, .. } => (
|
2018-06-06 15:20:22 +00:00
|
|
|
DECLARE_INTERIOR_MUTABLE_CONST,
|
2020-01-06 06:30:43 +00:00
|
|
|
"a `const` item should never be interior mutable",
|
2018-06-06 15:20:22 +00:00
|
|
|
*item,
|
|
|
|
),
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::Expr { expr } => (
|
2018-06-06 15:20:22 +00:00
|
|
|
BORROW_INTERIOR_MUTABLE_CONST,
|
2020-01-06 06:30:43 +00:00
|
|
|
"a `const` item with interior mutability should not be borrowed",
|
2018-06-06 15:20:22 +00:00
|
|
|
*expr,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn verify_ty_bound<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, source: Source) {
|
2020-06-21 09:20:48 +00:00
|
|
|
if ty.is_freeze(cx.tcx.at(DUMMY_SP), cx.param_env) || is_copy(cx, ty) {
|
2019-01-31 01:15:29 +00:00
|
|
|
// 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`
|
2018-06-06 15:20:22 +00:00
|
|
|
// as well.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (lint, msg, span) = source.lint();
|
2020-04-17 06:08:00 +00:00
|
|
|
span_lint_and_then(cx, lint, span, msg, |diag| {
|
2019-08-19 16:30:32 +00:00
|
|
|
if span.from_expansion() {
|
2018-06-06 15:20:22 +00:00
|
|
|
return; // Don't give suggestions into macros.
|
|
|
|
}
|
|
|
|
match source {
|
|
|
|
Source::Item { .. } => {
|
2019-06-12 08:28:58 +00:00
|
|
|
let const_kw_span = span.from_inner(InnerSpan::new(0, 5));
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_label(const_kw_span, "make this a static item (maybe with lazy_static)");
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
Source::Assoc { ty: ty_span, .. } => {
|
2020-08-06 15:49:46 +00:00
|
|
|
if ty.flags().intersects(TypeFlags::HAS_FREE_LOCAL_NAMES) {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_label(ty_span, &format!("consider requiring `{}` to be `Copy`", ty));
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
Source::Expr { .. } => {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.help("assign this const to a local or static variable, and use the variable here");
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(NonCopyConst => [DECLARE_INTERIOR_MUTABLE_CONST, BORROW_INTERIOR_MUTABLE_CONST]);
|
2018-06-06 15:20:22 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ItemKind::Const(hir_ty, ..) = &it.kind {
|
2018-06-06 15:20:22 +00:00
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
|
|
|
verify_ty_bound(cx, ty, Source::Item { item: it.span });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx TraitItem<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let TraitItemKind::Const(hir_ty, ..) = &trait_item.kind {
|
2018-06-06 15:20:22 +00:00
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
2018-11-27 20:14:15 +00:00
|
|
|
verify_ty_bound(
|
|
|
|
cx,
|
|
|
|
ty,
|
|
|
|
Source::Assoc {
|
|
|
|
ty: hir_ty.span,
|
|
|
|
item: trait_item.span,
|
|
|
|
},
|
|
|
|
);
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ImplItemKind::Const(hir_ty, ..) = &impl_item.kind {
|
2019-06-25 21:33:51 +00:00
|
|
|
let item_hir_id = cx.tcx.hir().get_parent_node(impl_item.hir_id);
|
2019-06-18 09:15:47 +00:00
|
|
|
let item = cx.tcx.hir().expect_item(item_hir_id);
|
2019-01-31 01:15:29 +00:00
|
|
|
// Ensure the impl is an inherent impl.
|
2020-01-18 05:14:36 +00:00
|
|
|
if let ItemKind::Impl { of_trait: None, .. } = item.kind {
|
2018-06-06 15:20:22 +00:00
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
2018-11-27 20:14:15 +00:00
|
|
|
verify_ty_bound(
|
|
|
|
cx,
|
|
|
|
ty,
|
|
|
|
Source::Assoc {
|
|
|
|
ty: hir_ty.span,
|
|
|
|
item: impl_item.span,
|
|
|
|
},
|
|
|
|
);
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Path(qpath) = &expr.kind {
|
2018-06-06 15:20:22 +00:00
|
|
|
// Only lint if we use the const item inside a function.
|
2019-02-24 18:43:15 +00:00
|
|
|
if in_constant(cx, expr.hir_id) {
|
2018-06-06 15:20:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
// Make sure it is a const item.
|
2019-09-18 17:29:04 +00:00
|
|
|
match qpath_res(cx, qpath, expr.hir_id) {
|
2020-03-16 06:23:03 +00:00
|
|
|
Res::Def(DefKind::Const | DefKind::AssocConst, _) => {},
|
2018-06-06 15:20:22 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
// Climb up to resolve any field access and explicit referencing.
|
2018-06-06 15:20:22 +00:00
|
|
|
let mut cur_expr = expr;
|
|
|
|
let mut dereferenced_expr = expr;
|
|
|
|
let mut needs_check_adjustment = true;
|
|
|
|
loop {
|
2019-06-25 21:33:51 +00:00
|
|
|
let parent_id = cx.tcx.hir().get_parent_node(cur_expr.hir_id);
|
2019-02-24 18:43:15 +00:00
|
|
|
if parent_id == cur_expr.hir_id {
|
2018-06-06 15:20:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-25 21:34:07 +00:00
|
|
|
if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find(parent_id) {
|
2019-09-27 15:16:06 +00:00
|
|
|
match &parent_expr.kind {
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::AddrOf(..) => {
|
2019-01-31 01:15:29 +00:00
|
|
|
// `&e` => `e` must be referenced.
|
2018-06-06 15:20:22 +00:00
|
|
|
needs_check_adjustment = false;
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Field(..) => {
|
2018-06-06 15:20:22 +00:00
|
|
|
needs_check_adjustment = true;
|
2020-08-28 14:10:16 +00:00
|
|
|
|
|
|
|
// Check whether implicit dereferences happened;
|
|
|
|
// if so, no need to go further up
|
|
|
|
// because of the same reason as the `ExprKind::Unary` case.
|
|
|
|
if cx
|
|
|
|
.typeck_results()
|
|
|
|
.expr_adjustments(dereferenced_expr)
|
|
|
|
.iter()
|
|
|
|
.any(|adj| matches!(adj.kind, Adjust::Deref(_)))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dereferenced_expr = parent_expr;
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Index(e, _) if ptr::eq(&**e, cur_expr) => {
|
2018-06-06 15:20:22 +00:00
|
|
|
// `e[i]` => desugared to `*Index::index(&e, i)`,
|
|
|
|
// meaning `e` must be referenced.
|
|
|
|
// no need to go further up since a method call is involved now.
|
|
|
|
needs_check_adjustment = false;
|
|
|
|
break;
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2020-01-06 16:39:50 +00:00
|
|
|
ExprKind::Unary(UnOp::UnDeref, _) => {
|
2018-06-06 15:20:22 +00:00
|
|
|
// `*e` => desugared to `*Deref::deref(&e)`,
|
|
|
|
// meaning `e` must be referenced.
|
|
|
|
// no need to go further up since a method call is involved now.
|
|
|
|
needs_check_adjustment = false;
|
|
|
|
break;
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
cur_expr = parent_expr;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 21:07:04 +00:00
|
|
|
let ty = if needs_check_adjustment {
|
2020-07-17 08:47:04 +00:00
|
|
|
let adjustments = cx.typeck_results().expr_adjustments(dereferenced_expr);
|
2020-07-14 12:59:59 +00:00
|
|
|
if let Some(i) = adjustments
|
|
|
|
.iter()
|
|
|
|
.position(|adj| matches!(adj.kind, Adjust::Borrow(_) | Adjust::Deref(_)))
|
|
|
|
{
|
2018-06-06 15:20:22 +00:00
|
|
|
if i == 0 {
|
2020-07-17 08:47:04 +00:00
|
|
|
cx.typeck_results().expr_ty(dereferenced_expr)
|
2018-06-06 15:20:22 +00:00
|
|
|
} else {
|
|
|
|
adjustments[i - 1].target
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-31 01:15:29 +00:00
|
|
|
// No borrow adjustments means the entire const is moved.
|
2018-06-06 15:20:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-11 21:07:04 +00:00
|
|
|
} else {
|
2020-07-17 08:47:04 +00:00
|
|
|
cx.typeck_results().expr_ty(dereferenced_expr)
|
2018-06-06 15:20:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
verify_ty_bound(cx, ty, Source::Expr { expr: expr.span });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|