2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2022-01-13 12:18:19 +00:00
|
|
|
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2020-10-23 20:16:59 +00:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind, MatchSource, Mutability};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::ty;
|
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::Span;
|
2019-10-16 11:25:42 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for function/method calls with a mutable
|
2019-10-16 11:25:42 +00:00
|
|
|
/// parameter in `debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!` macros.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// In release builds `debug_assert!` macros are optimized out by the
|
2019-10-16 11:25:42 +00:00
|
|
|
/// compiler.
|
|
|
|
/// Therefore mutating something in a `debug_assert!` macro results in different behaviour
|
|
|
|
/// between a release and debug build.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-10-16 11:25:42 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// debug_assert_eq!(vec![3].pop(), Some(3));
|
|
|
|
/// // or
|
|
|
|
/// fn take_a_mut_parameter(_: &mut u32) -> bool { unimplemented!() }
|
|
|
|
/// debug_assert!(take_a_mut_parameter(&mut 5));
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.40.0"]
|
2019-10-16 11:25:42 +00:00
|
|
|
pub DEBUG_ASSERT_WITH_MUT_CALL,
|
2020-01-31 09:42:31 +00:00
|
|
|
nursery,
|
2019-10-16 11:25:42 +00:00
|
|
|
"mutable arguments in `debug_assert{,_ne,_eq}!`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(DebugAssertWithMutCall => [DEBUG_ASSERT_WITH_MUT_CALL]);
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2022-01-13 12:18:19 +00:00
|
|
|
let Some(macro_call) = root_macro_call_first_node(cx, e) else { return };
|
|
|
|
let macro_name = cx.tcx.item_name(macro_call.def_id);
|
|
|
|
if !matches!(
|
|
|
|
macro_name.as_str(),
|
|
|
|
"debug_assert" | "debug_assert_eq" | "debug_assert_ne"
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let Some((lhs, rhs, _)) = find_assert_eq_args(cx, e, macro_call.expn) else { return };
|
|
|
|
for arg in [lhs, rhs] {
|
|
|
|
let mut visitor = MutArgVisitor::new(cx);
|
|
|
|
visitor.visit_expr(arg);
|
|
|
|
if let Some(span) = visitor.expr_span() {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DEBUG_ASSERT_WITH_MUT_CALL,
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"do not call a function with mutable arguments inside of `{}!`",
|
|
|
|
macro_name
|
|
|
|
),
|
|
|
|
);
|
2019-10-16 11:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MutArgVisitor<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2019-10-16 11:25:42 +00:00
|
|
|
expr_span: Option<Span>,
|
|
|
|
found: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MutArgVisitor<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
fn new(cx: &'a LateContext<'tcx>) -> Self {
|
2019-10-16 11:25:42 +00:00
|
|
|
Self {
|
|
|
|
cx,
|
|
|
|
expr_span: None,
|
|
|
|
found: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_span(&self) -> Option<Span> {
|
2021-03-12 14:30:50 +00:00
|
|
|
if self.found { self.expr_span } else { None }
|
2019-10-16 11:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for MutArgVisitor<'a, 'tcx> {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
2019-10-16 11:25:42 +00:00
|
|
|
match expr.kind {
|
2019-12-21 18:38:45 +00:00
|
|
|
ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) => {
|
2019-10-16 11:25:42 +00:00
|
|
|
self.found = true;
|
|
|
|
return;
|
|
|
|
},
|
2021-01-01 18:38:11 +00:00
|
|
|
ExprKind::If(..) => {
|
|
|
|
self.found = true;
|
|
|
|
return;
|
|
|
|
},
|
2019-10-16 11:25:42 +00:00
|
|
|
ExprKind::Path(_) => {
|
2020-07-17 08:47:04 +00:00
|
|
|
if let Some(adj) = self.cx.typeck_results().adjustments().get(expr.hir_id) {
|
2019-10-16 11:25:42 +00:00
|
|
|
if adj
|
|
|
|
.iter()
|
2020-08-03 22:18:29 +00:00
|
|
|
.any(|a| matches!(a.target.kind(), ty::Ref(_, _, Mutability::Mut)))
|
2019-10-16 11:25:42 +00:00
|
|
|
{
|
|
|
|
self.found = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-01-29 14:46:38 +00:00
|
|
|
// Don't check await desugars
|
|
|
|
ExprKind::Match(_, _, MatchSource::AwaitDesugar) => return,
|
2019-10-16 11:25:42 +00:00
|
|
|
_ if !self.found => self.expr_span = Some(expr.span),
|
|
|
|
_ => return,
|
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
walk_expr(self, expr);
|
2019-10-16 11:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
|
2019-10-16 11:25:42 +00:00
|
|
|
}
|
|
|
|
}
|