Skip to content

Commit 4efda7a

Browse files
Rollup merge of #154074 - dianne:dbg-temp-scopes, r=Mark-Simulacrum
don't drop arguments' temporaries in `dbg!` Fixes rust-lang/rust#153850 Credit to @theemathas for help with macro engineering ^^ r? libs
2 parents 6e68667 + e689235 commit 4efda7a

2 files changed

Lines changed: 5 additions & 43 deletions

File tree

clippy_lints/src/dbg_macro.rs

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::{is_in_test, sym};
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{Arm, Closure, ClosureKind, CoroutineKind, Expr, ExprKind, LetStmt, LocalSource, Node, Stmt, StmtKind};
8+
use rustc_hir::{Closure, ClosureKind, CoroutineKind, Expr, ExprKind, LetStmt, LocalSource, Node, Stmt, StmtKind};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_session::impl_lint_pass;
1111
use rustc_span::{Span, SyntaxContext};
@@ -92,16 +92,15 @@ impl LateLintPass<'_> for DbgMacro {
9292
(macro_call.span, String::from("()"))
9393
}
9494
},
95-
ExprKind::Match(first, arms, _) => {
96-
let vals = collect_vals(first, arms);
97-
let suggestion = match *vals.as_slice() {
95+
ExprKind::Match(args, _, _) => {
96+
let suggestion = match args.kind {
9897
// dbg!(1) => 1
99-
[val] => {
98+
ExprKind::Tup([val]) => {
10099
snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability)
101100
.to_string()
102101
},
103102
// dbg!(2, 3) => (2, 3)
104-
[first, .., last] => {
103+
ExprKind::Tup([first, .., last]) => {
105104
let snippet = snippet_with_applicability(
106105
cx,
107106
first.span.source_callsite().to(last.span.source_callsite()),
@@ -165,39 +164,3 @@ fn is_async_move_desugar<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx
165164
fn first_dbg_macro_in_expansion(cx: &LateContext<'_>, span: Span) -> Option<MacroCall> {
166165
macro_backtrace(span).find(|mc| cx.tcx.is_diagnostic_item(sym::dbg_macro, mc.def_id))
167166
}
168-
169-
/// Extracts all value expressions from the `match`-tree generated by `dbg!`.
170-
///
171-
/// E.g. from
172-
/// ```rust, ignore
173-
/// match 1 {
174-
/// tmp_1 => match 2 {
175-
/// tmp_2 => {
176-
/// /* printing */
177-
/// (tmp_1, tmp_2)
178-
/// }
179-
/// }
180-
/// }
181-
/// ```
182-
/// this extracts `1` and `2`.
183-
fn collect_vals<'hir>(first: &'hir Expr<'hir>, mut arms: &'hir [Arm<'hir>]) -> Vec<&'hir Expr<'hir>> {
184-
let mut vals = vec![first];
185-
loop {
186-
let [arm] = arms else {
187-
unreachable!("dbg! macro expansion only has single-arm matches")
188-
};
189-
190-
match is_async_move_desugar(arm.body)
191-
.unwrap_or(arm.body)
192-
.peel_drop_temps()
193-
.kind
194-
{
195-
ExprKind::Block(..) => return vals,
196-
ExprKind::Match(val, a, _) => {
197-
vals.push(val);
198-
arms = a;
199-
},
200-
_ => unreachable!("dbg! macro expansion only results in block or match expressions"),
201-
}
202-
}
203-
}

clippy_utils/src/sym.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ generate! {
200200
cx,
201201
cycle,
202202
cyclomatic_complexity,
203-
dbg_macro,
204203
de,
205204
debug_struct,
206205
deprecated_in_future,

0 commit comments

Comments
 (0)