|
1 | | -use crate::utils::{is_direct_expn_of, span_lint}; |
2 | | -use if_chain::if_chain; |
| 1 | +use crate::utils::{higher, is_direct_expn_of, span_lint}; |
3 | 2 | use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; |
4 | | -use rustc_hir::{BorrowKind, Expr, ExprKind, MatchSource, Mutability, StmtKind, UnOp}; |
| 3 | +use rustc_hir::{BorrowKind, Expr, ExprKind, MatchSource, Mutability}; |
5 | 4 | use rustc_lint::{LateContext, LateLintPass}; |
6 | 5 | use rustc_middle::hir::map::Map; |
7 | 6 | use rustc_middle::ty; |
@@ -39,66 +38,23 @@ impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall { |
39 | 38 | fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { |
40 | 39 | for dmn in &DEBUG_MACRO_NAMES { |
41 | 40 | if is_direct_expn_of(e.span, dmn).is_some() { |
42 | | - if let Some(span) = extract_call(cx, e) { |
43 | | - span_lint( |
44 | | - cx, |
45 | | - DEBUG_ASSERT_WITH_MUT_CALL, |
46 | | - span, |
47 | | - &format!("do not call a function with mutable arguments inside of `{}!`", dmn), |
48 | | - ); |
49 | | - } |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -//HACK(hellow554): remove this when #4694 is implemented |
56 | | -fn extract_call<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<Span> { |
57 | | - if_chain! { |
58 | | - if let ExprKind::Block(ref block, _) = e.kind; |
59 | | - if block.stmts.len() == 1; |
60 | | - if let StmtKind::Semi(ref matchexpr) = block.stmts[0].kind; |
61 | | - then { |
62 | | - // debug_assert |
63 | | - if_chain! { |
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; |
67 | | - then { |
68 | | - let mut visitor = MutArgVisitor::new(cx); |
69 | | - visitor.visit_expr(condition); |
70 | | - return visitor.expr_span(); |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - // debug_assert_{eq,ne} |
75 | | - if_chain! { |
76 | | - if let ExprKind::Block(ref matchblock, _) = matchexpr.kind; |
77 | | - if let Some(ref matchheader) = matchblock.expr; |
78 | | - if let ExprKind::Match(ref headerexpr, _, _) = matchheader.kind; |
79 | | - if let ExprKind::Tup(ref conditions) = headerexpr.kind; |
80 | | - if conditions.len() == 2; |
81 | | - then { |
82 | | - if let ExprKind::AddrOf(BorrowKind::Ref, _, ref lhs) = conditions[0].kind { |
| 41 | + if let Some(macro_args) = higher::extract_assert_macro_args(e) { |
| 42 | + for arg in macro_args { |
83 | 43 | let mut visitor = MutArgVisitor::new(cx); |
84 | | - visitor.visit_expr(lhs); |
| 44 | + visitor.visit_expr(arg); |
85 | 45 | if let Some(span) = visitor.expr_span() { |
86 | | - return Some(span); |
87 | | - } |
88 | | - } |
89 | | - if let ExprKind::AddrOf(BorrowKind::Ref, _, ref rhs) = conditions[1].kind { |
90 | | - let mut visitor = MutArgVisitor::new(cx); |
91 | | - visitor.visit_expr(rhs); |
92 | | - if let Some(span) = visitor.expr_span() { |
93 | | - return Some(span); |
| 46 | + span_lint( |
| 47 | + cx, |
| 48 | + DEBUG_ASSERT_WITH_MUT_CALL, |
| 49 | + span, |
| 50 | + &format!("do not call a function with mutable arguments inside of `{}!`", dmn), |
| 51 | + ); |
94 | 52 | } |
95 | 53 | } |
96 | 54 | } |
97 | 55 | } |
98 | 56 | } |
99 | 57 | } |
100 | | - |
101 | | - None |
102 | 58 | } |
103 | 59 |
|
104 | 60 | struct MutArgVisitor<'a, 'tcx> { |
|
0 commit comments