Skip to content

Commit bbcc260

Browse files
committed
manual_let_else: keep macro call on suggestion blocks
1 parent 1a96571 commit bbcc260

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::higher::IfLetOrMatch;
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::peel_blocks;
5-
use clippy_utils::source::snippet_opt;
5+
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
66
use clippy_utils::ty::is_type_diagnostic_item;
77
use clippy_utils::visitors::{for_each_expr, Descend};
88
use if_chain::if_chain;
@@ -143,18 +143,22 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat:
143143
// for this to be machine applicable.
144144
let app = Applicability::HasPlaceholders;
145145

146-
if let Some(sn_pat) = snippet_opt(cx, pat.span) &&
147-
let Some(sn_expr) = snippet_opt(cx, expr.span) &&
148-
let Some(sn_else) = snippet_opt(cx, else_body.span)
149-
{
150-
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) {
151-
sn_else
152-
} else {
153-
format!("{{ {sn_else} }}")
154-
};
155-
let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};");
156-
diag.span_suggestion(span, "consider writing", sugg, app);
157-
}
146+
let snippet_fn = if span.from_expansion() {
147+
snippet
148+
} else {
149+
snippet_with_macro_callsite
150+
};
151+
let sn_pat = snippet_fn(cx, pat.span, "");
152+
let sn_expr = snippet_fn(cx, expr.span, "");
153+
let sn_else = snippet_fn(cx, else_body.span, "");
154+
155+
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) {
156+
sn_else.into_owned()
157+
} else {
158+
format!("{{ {sn_else} }}")
159+
};
160+
let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};");
161+
diag.span_suggestion(span, "consider writing", sugg, app);
158162
},
159163
);
160164
}

0 commit comments

Comments
 (0)