Skip to content

Commit 1ff81c1

Browse files
committed
Auto merge of rust-lang#5350 - ThibsG:FixSingleBindingClosure, r=flip1995
Fix single binding closure Fix the `match_single_binding` lint when triggered inside a closure. Fixes: rust-lang#5347 changelog: Improve suggestion for [`match_single_binding`]
2 parents 43bdee0 + badfbbb commit 1ff81c1

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

clippy_lints/src/matches.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use crate::utils::usage::is_unused;
55
use crate::utils::{
6-
expr_block, get_arg_name, in_macro, indent_of, is_allowed, is_expn_of, is_refutable, is_wild, match_qpath,
7-
match_type, match_var, multispan_sugg, remove_blocks, snippet, snippet_block, snippet_with_applicability,
8-
span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then, walk_ptrs_ty,
6+
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable, is_wild,
7+
match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet, snippet_block,
8+
snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
9+
walk_ptrs_ty,
910
};
1011
use if_chain::if_chain;
1112
use rustc::lint::in_external_macro;
@@ -928,14 +929,27 @@ fn check_match_single_binding<'a>(cx: &LateContext<'_, 'a>, ex: &Expr<'a>, arms:
928929
),
929930
)
930931
} else {
932+
// If we are in closure, we need curly braces around suggestion
933+
let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
934+
let (mut cbrace_start, mut cbrace_end) = ("".to_string(), "".to_string());
935+
if let Some(parent_expr) = get_parent_expr(cx, expr) {
936+
if let ExprKind::Closure(..) = parent_expr.kind {
937+
cbrace_end = format!("\n{}}}", indent);
938+
// Fix body indent due to the closure
939+
indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
940+
cbrace_start = format!("{{\n{}", indent);
941+
}
942+
};
931943
(
932944
expr.span,
933945
format!(
934-
"let {} = {};\n{}{}",
946+
"{}let {} = {};\n{}{}{}",
947+
cbrace_start,
935948
snippet_with_applicability(cx, bind_names, "..", &mut applicability),
936949
snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
937-
" ".repeat(indent_of(cx, expr.span).unwrap_or(0)),
938-
snippet_body
950+
indent,
951+
snippet_body,
952+
cbrace_end
939953
),
940954
)
941955
};

tests/ui/match_single_binding.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ fn main() {
6767
// Lint
6868
let Point { x, y } = coords();
6969
let product = x * y;
70+
// Lint
71+
let v = vec![Some(1), Some(2), Some(3), Some(4)];
72+
#[allow(clippy::let_and_return)]
73+
let _ = v
74+
.iter()
75+
.map(|i| {
76+
let unwrapped = i.unwrap();
77+
unwrapped
78+
})
79+
.collect::<Vec<u8>>();
7080
}

tests/ui/match_single_binding.rs

+9
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,13 @@ fn main() {
8080
let product = match coords() {
8181
Point { x, y } => x * y,
8282
};
83+
// Lint
84+
let v = vec![Some(1), Some(2), Some(3), Some(4)];
85+
#[allow(clippy::let_and_return)]
86+
let _ = v
87+
.iter()
88+
.map(|i| match i.unwrap() {
89+
unwrapped => unwrapped,
90+
})
91+
.collect::<Vec<u8>>();
8392
}

tests/ui/match_single_binding.stderr

+18-1
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,22 @@ LL | let Point { x, y } = coords();
150150
LL | let product = x * y;
151151
|
152152

153-
error: aborting due to 10 previous errors
153+
error: this match could be written as a `let` statement
154+
--> $DIR/match_single_binding.rs:88:18
155+
|
156+
LL | .map(|i| match i.unwrap() {
157+
| __________________^
158+
LL | | unwrapped => unwrapped,
159+
LL | | })
160+
| |_________^
161+
|
162+
help: consider using `let` statement
163+
|
164+
LL | .map(|i| {
165+
LL | let unwrapped = i.unwrap();
166+
LL | unwrapped
167+
LL | })
168+
|
169+
170+
error: aborting due to 11 previous errors
154171

0 commit comments

Comments
 (0)