Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MappingToUnit to support no span of arg_ty #109154

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions compiler/rustc_lint/src/map_unit_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
return;
}
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
let default_span = args[0].span;
if let ty::FnDef(id, _) = arg_ty.kind() {
let fn_ty = cx.tcx.fn_sig(id).skip_binder();
let ret_ty = fn_ty.output().skip_binder();
Expand All @@ -64,7 +65,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
MAP_UNIT_FN,
span,
MappingToUnit {
function_label: cx.tcx.span_of_impl(*id).unwrap(),
function_label: cx
.tcx
.span_of_impl(*id)
.unwrap_or(default_span),
argument_label: args[0].span,
map_label: arg_ty.default_span(cx.tcx),
suggestion: path.ident.span,
Expand All @@ -80,7 +84,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
MAP_UNIT_FN,
span,
MappingToUnit {
function_label: cx.tcx.span_of_impl(*id).unwrap(),
function_label: cx
.tcx
.span_of_impl(*id)
.unwrap_or(default_span),
argument_label: args[0].span,
map_label: arg_ty.default_span(cx.tcx),
suggestion: path.ident.span,
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/lint/issue-109152.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![deny(map_unit_fn)]

#![crate_type = "lib"]
fn _y() {
vec![42].iter().map(drop);
//~^ ERROR `Iterator::map` call that discard the iterator's values
}
23 changes: 23 additions & 0 deletions tests/ui/lint/issue-109152.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: `Iterator::map` call that discard the iterator's values
--> $DIR/issue-109152.rs:5:21
|
LL | vec![42].iter().map(drop);
| ^^^^----^
| | |
| | this function returns `()`, which is likely not what you wanted
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use args[0].span as the default span seems reasonable and code is more clean.
@compiler-errors how do you think.

| | called `Iterator::map` with callable that returns `()`
| 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
|
= 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
note: the lint level is defined here
--> $DIR/issue-109152.rs:1:9
|
LL | #![deny(map_unit_fn)]
| ^^^^^^^^^^^
help: you might have meant to use `Iterator::for_each`
|
LL | vec![42].iter().for_each(drop);
| ~~~~~~~~

error: aborting due to previous error