Skip to content

Commit 3690199

Browse files
committed
Auto merge of #9601 - evantypanski:et/issue9575, r=Manishearth
[`match_single_binding`] Add curlies for more cases to fix suggestion causes error Fixes #9575 changelog: [`match_single_binding`]: Add curlies for scrutinees with side effects for more cases
2 parents 8f1ebdd + 39164ac commit 3690199

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

clippy_lints/src/matches/match_single_binding.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
5858
&snippet_body,
5959
&mut applicability,
6060
Some(span),
61+
true,
6162
);
6263

6364
span_lint_and_sugg(
@@ -90,6 +91,7 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
9091
&snippet_body,
9192
&mut applicability,
9293
None,
94+
true,
9395
);
9496
(expr.span, sugg)
9597
},
@@ -107,10 +109,14 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
107109
},
108110
PatKind::Wild => {
109111
if ex.can_have_side_effects() {
110-
let indent = " ".repeat(indent_of(cx, expr.span).unwrap_or(0));
111-
let sugg = format!(
112-
"{};\n{indent}{snippet_body}",
113-
snippet_with_applicability(cx, ex.span, "..", &mut applicability)
112+
let sugg = sugg_with_curlies(
113+
cx,
114+
(ex, expr),
115+
(bind_names, matched_vars),
116+
&snippet_body,
117+
&mut applicability,
118+
None,
119+
false,
114120
);
115121

116122
span_lint_and_sugg(
@@ -169,6 +175,7 @@ fn sugg_with_curlies<'a>(
169175
snippet_body: &str,
170176
applicability: &mut Applicability,
171177
assignment: Option<Span>,
178+
needs_var_binding: bool,
172179
) -> String {
173180
let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
174181

@@ -200,9 +207,15 @@ fn sugg_with_curlies<'a>(
200207
s
201208
});
202209

203-
format!(
204-
"{cbrace_start}let {} = {};\n{indent}{assignment_str}{snippet_body}{cbrace_end}",
205-
snippet_with_applicability(cx, bind_names, "..", applicability),
206-
snippet_with_applicability(cx, matched_vars, "..", applicability)
207-
)
210+
let scrutinee = if needs_var_binding {
211+
format!(
212+
"let {} = {}",
213+
snippet_with_applicability(cx, bind_names, "..", applicability),
214+
snippet_with_applicability(cx, matched_vars, "..", applicability)
215+
)
216+
} else {
217+
snippet_with_applicability(cx, matched_vars, "..", applicability).to_string()
218+
};
219+
220+
format!("{cbrace_start}{scrutinee};\n{indent}{assignment_str}{snippet_body}{cbrace_end}")
208221
}

tests/ui/match_single_binding.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,12 @@ fn issue_8723() {
124124

125125
let _ = val;
126126
}
127+
128+
#[allow(dead_code)]
129+
fn issue_9575() {
130+
fn side_effects() {}
131+
let _ = || {
132+
side_effects();
133+
println!("Needs curlies");
134+
};
135+
}

tests/ui/match_single_binding.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,11 @@ fn issue_8723() {
140140

141141
let _ = val;
142142
}
143+
144+
#[allow(dead_code)]
145+
fn issue_9575() {
146+
fn side_effects() {}
147+
let _ = || match side_effects() {
148+
_ => println!("Needs curlies"),
149+
};
150+
}

tests/ui/match_single_binding.stderr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,22 @@ LL + suf
196196
LL ~ };
197197
|
198198

199-
error: aborting due to 13 previous errors
199+
error: this match could be replaced by its scrutinee and body
200+
--> $DIR/match_single_binding.rs:147:16
201+
|
202+
LL | let _ = || match side_effects() {
203+
| ________________^
204+
LL | | _ => println!("Needs curlies"),
205+
LL | | };
206+
| |_____^
207+
|
208+
help: consider using the scrutinee and body instead
209+
|
210+
LL ~ let _ = || {
211+
LL + side_effects();
212+
LL + println!("Needs curlies");
213+
LL ~ };
214+
|
215+
216+
error: aborting due to 14 previous errors
200217

0 commit comments

Comments
 (0)