Skip to content

Commit ee12b12

Browse files
committed
Auto merge of rust-lang#9943 - dswij:pr-9940, r=Jarcho
manual_let_else: keep macro call on suggestion blocks Closes rust-lang#9940 changelog: [`manual_let_else`]: Do not expand macro calls on suggestions
2 parents 1cdb06d + c502dee commit ee12b12

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 12 additions & 14 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_with_context;
66
use clippy_utils::ty::is_type_diagnostic_item;
77
use clippy_utils::visitors::{for_each_expr, Descend};
88
use if_chain::if_chain;
@@ -141,20 +141,18 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat:
141141
// * unused binding collision detection with existing ones
142142
// * putting patterns with at the top level | inside ()
143143
// for this to be machine applicable.
144-
let app = Applicability::HasPlaceholders;
144+
let mut app = Applicability::HasPlaceholders;
145+
let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", &mut app);
146+
let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app);
147+
let (sn_else, _) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app);
145148

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-
}
149+
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) {
150+
sn_else.into_owned()
151+
} else {
152+
format!("{{ {sn_else} }}")
153+
};
154+
let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};");
155+
diag.span_suggestion(span, "consider writing", sugg, app);
158156
},
159157
);
160158
}

tests/ui/manual_let_else.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,18 @@ fn not_fire() {
234234
// If a type annotation is present, don't lint as
235235
// expressing the type might be too hard
236236
let v: () = if let Some(v_some) = g() { v_some } else { panic!() };
237+
238+
// Issue 9940
239+
// Suggestion should not expand macros
240+
macro_rules! macro_call {
241+
() => {
242+
return ()
243+
};
244+
}
245+
246+
let ff = Some(1);
247+
let _ = match ff {
248+
Some(value) => value,
249+
_ => macro_call!(),
250+
};
237251
}

tests/ui/manual_let_else.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,14 @@ LL | create_binding_if_some!(w, g());
259259
|
260260
= note: this error originates in the macro `create_binding_if_some` (in Nightly builds, run with -Z macro-backtrace for more info)
261261

262-
error: aborting due to 17 previous errors
262+
error: this could be rewritten as `let...else`
263+
--> $DIR/manual_let_else.rs:247:5
264+
|
265+
LL | / let _ = match ff {
266+
LL | | Some(value) => value,
267+
LL | | _ => macro_call!(),
268+
LL | | };
269+
| |______^ help: consider writing: `let Some(value) = ff else { macro_call!() };`
270+
271+
error: aborting due to 18 previous errors
263272

0 commit comments

Comments
 (0)