2018-10-06 16:18:06 +00:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-06-06 15:20:22 +00:00
|
|
|
//! Checks for uses of const which the type is not Freeze (Cell-free).
|
|
|
|
//!
|
|
|
|
//! This lint is **deny** by default.
|
|
|
|
|
|
|
|
use crate::utils::{in_constant, in_macro, is_copy, span_lint_and_then};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir::def::Def;
|
|
|
|
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_errors::Applicability;
|
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
2018-11-27 20:14:15 +00:00
|
|
|
use std::ptr;
|
2018-12-29 15:04:45 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2018-06-06 15:20:22 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for declaration of `const` items which is interior
|
|
|
|
/// mutable (e.g. contains a `Cell`, `Mutex`, `AtomicXxxx` etc).
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Consts are copied everywhere they are referenced, i.e.
|
|
|
|
/// 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
|
|
|
|
/// initialized value to downstream `static` items (e.g. the
|
|
|
|
/// `std::sync::ONCE_INIT` constant). In this case the use of `const` is legit,
|
|
|
|
/// and this lint should be suppressed.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2018-11-27 20:14:15 +00:00
|
|
|
/// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
2018-06-06 15:20:22 +00:00
|
|
|
///
|
|
|
|
/// // Bad.
|
|
|
|
/// const CONST_ATOM: AtomicUsize = AtomicUsize::new(12);
|
2018-11-27 20:14:15 +00:00
|
|
|
/// CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged
|
2018-06-06 15:20:22 +00:00
|
|
|
/// 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
|
|
|
|
/// ```
|
|
|
|
declare_clippy_lint! {
|
|
|
|
pub DECLARE_INTERIOR_MUTABLE_CONST,
|
|
|
|
correctness,
|
|
|
|
"declaring const with interior mutability"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// **What it does:** Checks if `const` items which is interior mutable (e.g.
|
|
|
|
/// contains a `Cell`, `Mutex`, `AtomicXxxx` etc) has been borrowed directly.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Consts are copied everywhere they are referenced, i.e.
|
|
|
|
/// 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
|
2018-11-27 20:14:15 +00:00
|
|
|
/// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
2018-06-06 15:20:22 +00:00
|
|
|
/// const CONST_ATOM: AtomicUsize = AtomicUsize::new(12);
|
|
|
|
///
|
|
|
|
/// // Bad.
|
2018-11-27 20:14:15 +00:00
|
|
|
/// CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged
|
2018-06-06 15:20:22 +00:00
|
|
|
/// 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
|
|
|
|
/// ```
|
|
|
|
declare_clippy_lint! {
|
|
|
|
pub BORROW_INTERIOR_MUTABLE_CONST,
|
|
|
|
correctness,
|
|
|
|
"referencing const with interior mutability"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
fn lint(&self) -> (&'static Lint, &'static str, Span) {
|
|
|
|
match self {
|
|
|
|
Source::Item { item } | Source::Assoc { item, .. } => (
|
|
|
|
DECLARE_INTERIOR_MUTABLE_CONST,
|
|
|
|
"a const item should never be interior mutable",
|
|
|
|
*item,
|
|
|
|
),
|
|
|
|
Source::Expr { expr } => (
|
|
|
|
BORROW_INTERIOR_MUTABLE_CONST,
|
|
|
|
"a const item with interior mutability should not be borrowed",
|
|
|
|
*expr,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 20:14:15 +00:00
|
|
|
fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>, source: Source) {
|
2018-06-06 15:20:22 +00:00
|
|
|
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
|
|
|
|
// as well.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (lint, msg, span) = source.lint();
|
|
|
|
span_lint_and_then(cx, lint, span, msg, |db| {
|
|
|
|
if in_macro(span) {
|
|
|
|
return; // Don't give suggestions into macros.
|
|
|
|
}
|
|
|
|
match source {
|
|
|
|
Source::Item { .. } => {
|
|
|
|
let const_kw_span = span.from_inner_byte_pos(0, 5);
|
|
|
|
db.span_suggestion_with_applicability(
|
|
|
|
const_kw_span,
|
|
|
|
"make this a static item",
|
|
|
|
"static".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
Source::Assoc { ty: ty_span, .. } => {
|
|
|
|
if ty.flags.contains(TypeFlags::HAS_FREE_LOCAL_NAMES) {
|
|
|
|
db.span_help(ty_span, &format!("consider requiring `{}` to be `Copy`", ty));
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
Source::Expr { .. } => {
|
2018-11-27 20:14:15 +00:00
|
|
|
db.help("assign this const to a local or static variable, and use the variable here");
|
|
|
|
},
|
2018-06-06 15:20:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NonCopyConst;
|
|
|
|
|
|
|
|
impl LintPass for NonCopyConst {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(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) {
|
2018-07-16 13:07:39 +00:00
|
|
|
if let ItemKind::Const(hir_ty, ..) = &it.node {
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx TraitItem) {
|
|
|
|
if let TraitItemKind::Const(hir_ty, ..) = &trait_item.node {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx ImplItem) {
|
|
|
|
if let ImplItemKind::Const(hir_ty, ..) = &impl_item.node {
|
2018-12-08 00:56:03 +00:00
|
|
|
let item_node_id = cx.tcx.hir().get_parent_node(impl_item.id);
|
|
|
|
let item = cx.tcx.hir().expect_item(item_node_id);
|
2018-06-06 15:20:22 +00:00
|
|
|
// ensure the impl is an inherent impl.
|
2018-07-16 13:07:39 +00:00
|
|
|
if let ItemKind::Impl(_, _, _, _, None, _, _) = item.node {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
2018-07-12 07:30:57 +00:00
|
|
|
if let ExprKind::Path(qpath) = &expr.node {
|
2018-06-06 15:20:22 +00:00
|
|
|
// Only lint if we use the const item inside a function.
|
|
|
|
if in_constant(cx, expr.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure it is a const item.
|
|
|
|
match cx.tables.qpath_def(qpath, expr.hir_id) {
|
|
|
|
Def::Const(_) | Def::AssociatedConst(_) => {},
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// climb up to resolve any field access and explicit referencing.
|
|
|
|
let mut cur_expr = expr;
|
|
|
|
let mut dereferenced_expr = expr;
|
|
|
|
let mut needs_check_adjustment = true;
|
|
|
|
loop {
|
2018-12-08 00:56:03 +00:00
|
|
|
let parent_id = cx.tcx.hir().get_parent_node(cur_expr.id);
|
2018-06-06 15:20:22 +00:00
|
|
|
if parent_id == cur_expr.id {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-08 00:56:03 +00:00
|
|
|
if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find(parent_id) {
|
2018-06-06 15:20:22 +00:00
|
|
|
match &parent_expr.node {
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::AddrOf(..) => {
|
2018-06-06 15:20:22 +00:00
|
|
|
// `&e` => `e` must be referenced
|
|
|
|
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
|
|
|
dereferenced_expr = parent_expr;
|
|
|
|
needs_check_adjustment = true;
|
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
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Unary(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 {
|
2018-06-06 15:20:22 +00:00
|
|
|
let adjustments = cx.tables.expr_adjustments(dereferenced_expr);
|
|
|
|
if let Some(i) = adjustments.iter().position(|adj| match adj.kind {
|
|
|
|
Adjust::Borrow(_) | Adjust::Deref(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}) {
|
|
|
|
if i == 0 {
|
|
|
|
cx.tables.expr_ty(dereferenced_expr)
|
|
|
|
} else {
|
|
|
|
adjustments[i - 1].target
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No borrow adjustments = the entire const is moved.
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 21:07:04 +00:00
|
|
|
} else {
|
|
|
|
cx.tables.expr_ty(dereferenced_expr)
|
2018-06-06 15:20:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
verify_ty_bound(cx, ty, Source::Expr { expr: expr.span });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|