about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2023-03-15 17:51:33 +0530
committerGitHub <noreply@github.com>2023-03-15 17:51:33 +0530
commit19d575851a38f990aca8cef8b6b2f3eb0e6502dd (patch)
treefb7c663c46a0669ac27b6ec7042d0ad77fc72854
parenta08516be868bebe384abc31dc00bce13c8c42a4b (diff)
parentb3af5e2f8b58c96704d6a26a7a0e25b3b2bec6ad (diff)
downloadrust-19d575851a38f990aca8cef8b6b2f3eb0e6502dd.tar.gz
rust-19d575851a38f990aca8cef8b6b2f3eb0e6502dd.zip
Rollup merge of #109154 - chenyukang:yukang/fix-109152, r=compiler-errors
Fix MappingToUnit  to support no span of arg_ty

Fixes #109152
-rw-r--r--compiler/rustc_lint/src/map_unit_fn.rs11
-rw-r--r--tests/ui/lint/issue-109152.rs7
-rw-r--r--tests/ui/lint/issue-109152.stderr23
3 files changed, 39 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/map_unit_fn.rs b/compiler/rustc_lint/src/map_unit_fn.rs
index 62e8b4fe9e4..7c692fee333 100644
--- a/compiler/rustc_lint/src/map_unit_fn.rs
+++ b/compiler/rustc_lint/src/map_unit_fn.rs
@@ -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();
@@ -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,
@@ -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,
diff --git a/tests/ui/lint/issue-109152.rs b/tests/ui/lint/issue-109152.rs
new file mode 100644
index 00000000000..daf530e6d0b
--- /dev/null
+++ b/tests/ui/lint/issue-109152.rs
@@ -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
+}
diff --git a/tests/ui/lint/issue-109152.stderr b/tests/ui/lint/issue-109152.stderr
new file mode 100644
index 00000000000..7db9e71a584
--- /dev/null
+++ b/tests/ui/lint/issue-109152.stderr
@@ -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
+   |                     |   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
+