diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index df26ac670601c..8300f98fb1cd5 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -1050,6 +1050,30 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { if let Some(found_span) = found_span { err.span_label(found_span, format!("takes {}", found_str)); + // Suggest to take and ignore the arguments with expected_args_length `_`s if + // found arguments is empty (assume the user just wants to ignore args in this case). + // For example, if `expected_args_length` is 2, suggest `|_, _|`. + if found_args.is_empty() && is_closure { + let mut underscores = "_".repeat(expected_args.len()) + .split("") + .filter(|s| !s.is_empty()) + .collect::>() + .join(", "); + err.span_suggestion_with_applicability( + found_span, + &format!( + "consider changing the closure to take and ignore the expected argument{}", + if expected_args.len() < 2 { + "" + } else { + "s" + } + ), + format!("|{}|", underscores), + Applicability::MachineApplicable, + ); + } + if let &[ArgKind::Tuple(_, ref fields)] = &found_args[..] { if fields.len() == expected_args.len() { let sugg = fields.iter() diff --git a/src/test/ui/mismatched_types/closure-arg-count.stderr b/src/test/ui/mismatched_types/closure-arg-count.stderr index 6270e79449876..057cf6efa1dea 100644 --- a/src/test/ui/mismatched_types/closure-arg-count.stderr +++ b/src/test/ui/mismatched_types/closure-arg-count.stderr @@ -5,6 +5,10 @@ LL | [1, 2, 3].sort_by(|| panic!()); | ^^^^^^^ -- takes 0 arguments | | | expected closure that takes 2 arguments +help: consider changing the closure to take and ignore the expected arguments + | +LL | [1, 2, 3].sort_by(|_, _| panic!()); + | ^^^^^^ error[E0593]: closure is expected to take 2 arguments, but it takes 1 argument --> $DIR/closure-arg-count.rs:17:15 @@ -51,6 +55,10 @@ note: required by `f` | LL | fn f>(_: F) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider changing the closure to take and ignore the expected argument + | +LL | f(|_| panic!()); + | ^^^ error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 2 distinct arguments --> $DIR/closure-arg-count.rs:26:53