Skip to content

Commit 87f0c1f

Browse files
committed
Suggest to take and ignore args while closure args count mismatching
1 parent c7cba3d commit 87f0c1f

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,24 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10481048
err.span_label(span, format!( "expected {} that takes {}", kind, expected_str));
10491049

10501050
if let Some(found_span) = found_span {
1051-
err.span_label(found_span, format!("takes {}", found_str));
1051+
// Suggest to take and ignore the arguments with expected_args_length `_`s if
1052+
// found arguments is empty(Suppose the user just wants to ignore args in this case).
1053+
// like `|_, _|` for closure with 2 expected args.
1054+
if found_args.is_empty() && is_closure {
1055+
let mut underscores = "_".repeat(expected_args.len())
1056+
.split("")
1057+
.filter(|s| !s.is_empty())
1058+
.collect::<Vec<_>>()
1059+
.join(", ");
1060+
err.span_suggestion(
1061+
found_span,
1062+
"consider changing this to",
1063+
format!("|{}|", underscores),
1064+
);
1065+
} else {
1066+
err.span_label(found_span, format!("takes {}", found_str));
1067+
}
1068+
10521069

10531070
if let &[ArgKind::Tuple(_, ref fields)] = &found_args[..] {
10541071
if fields.len() == expected_args.len() {

0 commit comments

Comments
 (0)