|
1 | 1 | use clippy_config::Conf; |
2 | | -use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
3 | 3 | use clippy_utils::is_in_test; |
4 | 4 | use clippy_utils::macros::{macro_backtrace, MacroCall}; |
5 | 5 | use clippy_utils::source::snippet_with_applicability; |
@@ -65,61 +65,67 @@ impl LateLintPass<'_> for DbgMacro { |
65 | 65 | // allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml |
66 | 66 | !(self.allow_dbg_in_tests && is_in_test(cx.tcx, expr.hir_id)) |
67 | 67 | { |
68 | | - let mut applicability = Applicability::MachineApplicable; |
69 | | - |
70 | | - let (sugg_span, suggestion) = match expr.peel_drop_temps().kind { |
71 | | - // dbg!() |
72 | | - ExprKind::Block(..) => { |
73 | | - // If the `dbg!` macro is a "free" statement and not contained within other expressions, |
74 | | - // remove the whole statement. |
75 | | - if let Node::Stmt(_) = cx.tcx.parent_hir_node(expr.hir_id) |
76 | | - && let Some(semi_span) = cx.sess().source_map().mac_call_stmt_semi_span(macro_call.span) |
77 | | - { |
78 | | - (macro_call.span.to(semi_span), String::new()) |
79 | | - } else { |
80 | | - (macro_call.span, String::from("()")) |
81 | | - } |
82 | | - }, |
83 | | - // dbg!(1) |
84 | | - ExprKind::Match(val, ..) => ( |
85 | | - macro_call.span, |
86 | | - snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability).to_string(), |
87 | | - ), |
88 | | - // dbg!(2, 3) |
89 | | - ExprKind::Tup( |
90 | | - [ |
91 | | - Expr { |
92 | | - kind: ExprKind::Match(first, ..), |
93 | | - .. |
94 | | - }, |
95 | | - .., |
96 | | - Expr { |
97 | | - kind: ExprKind::Match(last, ..), |
98 | | - .. |
99 | | - }, |
100 | | - ], |
101 | | - ) => { |
102 | | - let snippet = snippet_with_applicability( |
103 | | - cx, |
104 | | - first.span.source_callsite().to(last.span.source_callsite()), |
105 | | - "..", |
106 | | - &mut applicability, |
107 | | - ); |
108 | | - (macro_call.span, format!("({snippet})")) |
109 | | - }, |
110 | | - _ => return, |
111 | | - }; |
112 | | - |
113 | 68 | self.prev_ctxt = cur_syntax_ctxt; |
114 | 69 |
|
115 | | - span_lint_and_sugg( |
| 70 | + span_lint_and_then( |
116 | 71 | cx, |
117 | 72 | DBG_MACRO, |
118 | | - sugg_span, |
| 73 | + macro_call.span, |
119 | 74 | "the `dbg!` macro is intended as a debugging tool", |
120 | | - "remove the invocation before committing it to a version control system", |
121 | | - suggestion, |
122 | | - applicability, |
| 75 | + |diag| { |
| 76 | + let mut applicability = Applicability::MachineApplicable; |
| 77 | + |
| 78 | + let (sugg_span, suggestion) = match expr.peel_drop_temps().kind { |
| 79 | + // dbg!() |
| 80 | + ExprKind::Block(..) => { |
| 81 | + // If the `dbg!` macro is a "free" statement and not contained within other expressions, |
| 82 | + // remove the whole statement. |
| 83 | + if let Node::Stmt(_) = cx.tcx.parent_hir_node(expr.hir_id) |
| 84 | + && let Some(semi_span) = cx.sess().source_map().mac_call_stmt_semi_span(macro_call.span) |
| 85 | + { |
| 86 | + (macro_call.span.to(semi_span), String::new()) |
| 87 | + } else { |
| 88 | + (macro_call.span, String::from("()")) |
| 89 | + } |
| 90 | + }, |
| 91 | + // dbg!(1) |
| 92 | + ExprKind::Match(val, ..) => ( |
| 93 | + macro_call.span, |
| 94 | + snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability) |
| 95 | + .to_string(), |
| 96 | + ), |
| 97 | + // dbg!(2, 3) |
| 98 | + ExprKind::Tup( |
| 99 | + [ |
| 100 | + Expr { |
| 101 | + kind: ExprKind::Match(first, ..), |
| 102 | + .. |
| 103 | + }, |
| 104 | + .., |
| 105 | + Expr { |
| 106 | + kind: ExprKind::Match(last, ..), |
| 107 | + .. |
| 108 | + }, |
| 109 | + ], |
| 110 | + ) => { |
| 111 | + let snippet = snippet_with_applicability( |
| 112 | + cx, |
| 113 | + first.span.source_callsite().to(last.span.source_callsite()), |
| 114 | + "..", |
| 115 | + &mut applicability, |
| 116 | + ); |
| 117 | + (macro_call.span, format!("({snippet})")) |
| 118 | + }, |
| 119 | + _ => unreachable!(), |
| 120 | + }; |
| 121 | + |
| 122 | + diag.span_suggestion( |
| 123 | + sugg_span, |
| 124 | + "remove the invocation before committing it to a version control system", |
| 125 | + suggestion, |
| 126 | + applicability, |
| 127 | + ); |
| 128 | + }, |
123 | 129 | ); |
124 | 130 | } |
125 | 131 | } |
|
0 commit comments