Skip to content

Commit 19d5758

Browse files
authored
Rollup merge of #109154 - chenyukang:yukang/fix-109152, r=compiler-errors
Fix MappingToUnit to support no span of arg_ty Fixes #109152
2 parents a08516b + b3af5e2 commit 19d5758

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Diff for: compiler/rustc_lint/src/map_unit_fn.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
5656
return;
5757
}
5858
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
59+
let default_span = args[0].span;
5960
if let ty::FnDef(id, _) = arg_ty.kind() {
6061
let fn_ty = cx.tcx.fn_sig(id).skip_binder();
6162
let ret_ty = fn_ty.output().skip_binder();
@@ -64,7 +65,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
6465
MAP_UNIT_FN,
6566
span,
6667
MappingToUnit {
67-
function_label: cx.tcx.span_of_impl(*id).unwrap(),
68+
function_label: cx
69+
.tcx
70+
.span_of_impl(*id)
71+
.unwrap_or(default_span),
6872
argument_label: args[0].span,
6973
map_label: arg_ty.default_span(cx.tcx),
7074
suggestion: path.ident.span,
@@ -80,7 +84,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
8084
MAP_UNIT_FN,
8185
span,
8286
MappingToUnit {
83-
function_label: cx.tcx.span_of_impl(*id).unwrap(),
87+
function_label: cx
88+
.tcx
89+
.span_of_impl(*id)
90+
.unwrap_or(default_span),
8491
argument_label: args[0].span,
8592
map_label: arg_ty.default_span(cx.tcx),
8693
suggestion: path.ident.span,

Diff for: tests/ui/lint/issue-109152.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![deny(map_unit_fn)]
2+
3+
#![crate_type = "lib"]
4+
fn _y() {
5+
vec![42].iter().map(drop);
6+
//~^ ERROR `Iterator::map` call that discard the iterator's values
7+
}

Diff for: tests/ui/lint/issue-109152.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: `Iterator::map` call that discard the iterator's values
2+
--> $DIR/issue-109152.rs:5:21
3+
|
4+
LL | vec![42].iter().map(drop);
5+
| ^^^^----^
6+
| | |
7+
| | this function returns `()`, which is likely not what you wanted
8+
| | called `Iterator::map` with callable that returns `()`
9+
| after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
10+
|
11+
= note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated
12+
note: the lint level is defined here
13+
--> $DIR/issue-109152.rs:1:9
14+
|
15+
LL | #![deny(map_unit_fn)]
16+
| ^^^^^^^^^^^
17+
help: you might have meant to use `Iterator::for_each`
18+
|
19+
LL | vec![42].iter().for_each(drop);
20+
| ~~~~~~~~
21+
22+
error: aborting due to previous error
23+

0 commit comments

Comments
 (0)