1 use crate::utils::{is_direct_expn_of, span_lint};
2 use if_chain::if_chain;
4 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc::hir::{BorrowKind, Expr, ExprKind, Mutability, StmtKind, UnOp};
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_lint_pass, ty};
8 use rustc_session::declare_tool_lint;
11 declare_clippy_lint! {
12 /// **What it does:** Checks for function/method calls with a mutable
13 /// parameter in `debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!` macros.
15 /// **Why is this bad?** In release builds `debug_assert!` macros are optimized out by the
17 /// Therefore mutating something in a `debug_assert!` macro results in different behaviour
18 /// between a release and debug build.
20 /// **Known problems:** None
24 /// debug_assert_eq!(vec![3].pop(), Some(3));
26 /// fn take_a_mut_parameter(_: &mut u32) -> bool { unimplemented!() }
27 /// debug_assert!(take_a_mut_parameter(&mut 5));
29 pub DEBUG_ASSERT_WITH_MUT_CALL,
31 "mutable arguments in `debug_assert{,_ne,_eq}!`"
34 declare_lint_pass!(DebugAssertWithMutCall => [DEBUG_ASSERT_WITH_MUT_CALL]);
36 const DEBUG_MACRO_NAMES: [&str; 3] = ["debug_assert", "debug_assert_eq", "debug_assert_ne"];
38 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DebugAssertWithMutCall {
39 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
40 for dmn in &DEBUG_MACRO_NAMES {
41 if is_direct_expn_of(e.span, dmn).is_some() {
42 if let Some(span) = extract_call(cx, e) {
45 DEBUG_ASSERT_WITH_MUT_CALL,
47 &format!("do not call a function with mutable arguments inside of `{}!`", dmn),
55 //HACK(hellow554): remove this when #4694 is implemented
56 #[allow(clippy::cognitive_complexity)]
57 fn extract_call<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, e: &'tcx Expr) -> Option<Span> {
59 if let ExprKind::Block(ref block, _) = e.kind;
60 if block.stmts.len() == 1;
61 if let StmtKind::Semi(ref matchexpr) = block.stmts[0].kind;
64 if let ExprKind::Match(ref ifclause, _, _) = matchexpr.kind;
65 if let ExprKind::DropTemps(ref droptmp) = ifclause.kind;
66 if let ExprKind::Unary(UnOp::UnNot, ref condition) = droptmp.kind;
69 let mut visitor = MutArgVisitor::new(cx);
70 visitor.visit_expr(condition);
71 return visitor.expr_span();
73 // debug_assert_{eq,ne}
75 if let ExprKind::Block(ref matchblock, _) = matchexpr.kind;
76 if let Some(ref matchheader) = matchblock.expr;
77 if let ExprKind::Match(ref headerexpr, _, _) = matchheader.kind;
78 if let ExprKind::Tup(ref conditions) = headerexpr.kind;
79 if conditions.len() == 2;
81 if let ExprKind::AddrOf(BorrowKind::Ref, _, ref lhs) = conditions[0].kind {
82 let mut visitor = MutArgVisitor::new(cx);
83 visitor.visit_expr(lhs);
84 if let Some(span) = visitor.expr_span() {
88 if let ExprKind::AddrOf(BorrowKind::Ref, _, ref rhs) = conditions[1].kind {
89 let mut visitor = MutArgVisitor::new(cx);
90 visitor.visit_expr(rhs);
91 if let Some(span) = visitor.expr_span() {
105 struct MutArgVisitor<'a, 'tcx> {
106 cx: &'a LateContext<'a, 'tcx>,
107 expr_span: Option<Span>,
111 impl<'a, 'tcx> MutArgVisitor<'a, 'tcx> {
112 fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
120 fn expr_span(&self) -> Option<Span> {
129 impl<'a, 'tcx> Visitor<'tcx> for MutArgVisitor<'a, 'tcx> {
130 fn visit_expr(&mut self, expr: &'tcx Expr) {
132 ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mutable, _) => {
136 ExprKind::Path(_) => {
137 if let Some(adj) = self.cx.tables.adjustments().get(expr.hir_id) {
140 .any(|a| matches!(a.target.kind, ty::Ref(_, _, Mutability::Mutable)))
147 _ if !self.found => self.expr_span = Some(expr.span),
150 walk_expr(self, expr)
153 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
154 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())