Skip to content

Commit 052bdff

Browse files
committed
lint based on closure pipe span
1 parent 4ec0ba9 commit 052bdff

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,13 +1092,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10921092
if let Some(found_span) = found_span {
10931093
err.span_label(found_span, format!("takes {}", found_str));
10941094

1095+
// move |_| { ... }
1096+
// ^^^^^^^^-- def_span
1097+
//
1098+
// move |_| { ... }
1099+
// ^^^^^-- prefix
1100+
let prefix_span = self.tcx.sess.source_map().span_until_char(found_span, '|');
1101+
// move |_| { ... }
1102+
// ^^^-- pipe_span
1103+
let pipe_span = if let Some(span) = found_span.trim_start(prefix_span) {
1104+
span
1105+
} else {
1106+
found_span
1107+
};
1108+
10951109
// Suggest to take and ignore the arguments with expected_args_length `_`s if
10961110
// found arguments is empty (assume the user just wants to ignore args in this case).
10971111
// For example, if `expected_args_length` is 2, suggest `|_, _|`.
10981112
if found_args.is_empty() && is_closure {
10991113
let underscores = vec!["_"; expected_args.len()].join(", ");
11001114
err.span_suggestion_with_applicability(
1101-
found_span,
1115+
pipe_span,
11021116
&format!(
11031117
"consider changing the closure to take and ignore the expected argument{}",
11041118
if expected_args.len() < 2 {

src/test/ui/mismatched_types/closure-arg-count.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fn main() {
2222
//~^ ERROR closure is expected to take
2323
f(|| panic!());
2424
//~^ ERROR closure is expected to take
25+
f(move || panic!());
26+
//~^ ERROR closure is expected to take
2527

2628
let _it = vec![1, 2, 3].into_iter().enumerate().map(|i, x| i);
2729
//~^ ERROR closure is expected to take

0 commit comments

Comments
 (0)