about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMara <m-ou.se@m-ou.se>2021-03-05 10:57:18 +0100
committerGitHub <noreply@github.com>2021-03-05 10:57:18 +0100
commit20887b7ebf6f5d4e993e942f03209a2d20256b85 (patch)
tree47c1c61b6c9866833741eb6f97bd9641b1a664c1 /src
parent60138110d7b00c8c788526cad525942e732db6ca (diff)
parenta3db47ab6c976137a26976d5c8060e7eadc05232 (diff)
downloadrust-20887b7ebf6f5d4e993e942f03209a2d20256b85.tar.gz
rust-20887b7ebf6f5d4e993e942f03209a2d20256b85.zip
Rollup merge of #81939 - kper:fixing-81584-allocate-in-iter, r=davidtwco
Add suggestion `.collect()` for iterators in iterators

Closes #81584

```
error[E0515]: cannot return value referencing function parameter `y`
 --> main3.rs:4:38
  |
4 | ...                   .map(|y| y.iter().map(|x| x + 1))
  |                                -^^^^^^^^^^^^^^^^^^^^^^
  |                                |
  |                                returns a value referencing data owned by the current function
  |                                `y` is borrowed here
  |                                help: Maybe use `.collect()` to allocate the iterator
```

Added the suggestion: `help: Maybe use `.collect()` to allocate the iterator`
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/issues/issue-81584.fixed8
-rw-r--r--src/test/ui/issues/issue-81584.rs8
-rw-r--r--src/test/ui/issues/issue-81584.stderr14
-rw-r--r--src/test/ui/static/static-reference-to-fn-2.stderr2
4 files changed, 32 insertions, 0 deletions
diff --git a/src/test/ui/issues/issue-81584.fixed b/src/test/ui/issues/issue-81584.fixed
new file mode 100644
index 00000000000..1cad59f1062
--- /dev/null
+++ b/src/test/ui/issues/issue-81584.fixed
@@ -0,0 +1,8 @@
+// run-rustfix
+fn main() {
+        let _ = vec![vec![0, 1], vec![2]]
+            .into_iter()
+            .map(|y| y.iter().map(|x| x + 1).collect::<Vec<_>>())
+                  //~^ ERROR cannot return value referencing function parameter `y`
+            .collect::<Vec<_>>();
+}
diff --git a/src/test/ui/issues/issue-81584.rs b/src/test/ui/issues/issue-81584.rs
new file mode 100644
index 00000000000..452288db08b
--- /dev/null
+++ b/src/test/ui/issues/issue-81584.rs
@@ -0,0 +1,8 @@
+// run-rustfix
+fn main() {
+        let _ = vec![vec![0, 1], vec![2]]
+            .into_iter()
+            .map(|y| y.iter().map(|x| x + 1))
+                  //~^ ERROR cannot return value referencing function parameter `y`
+            .collect::<Vec<_>>();
+}
diff --git a/src/test/ui/issues/issue-81584.stderr b/src/test/ui/issues/issue-81584.stderr
new file mode 100644
index 00000000000..d57f1b778df
--- /dev/null
+++ b/src/test/ui/issues/issue-81584.stderr
@@ -0,0 +1,14 @@
+error[E0515]: cannot return value referencing function parameter `y`
+  --> $DIR/issue-81584.rs:5:22
+   |
+LL |             .map(|y| y.iter().map(|x| x + 1))
+   |                      -^^^^^^^^^^^^^^^^^^^^^^
+   |                      |
+   |                      returns a value referencing data owned by the current function
+   |                      `y` is borrowed here
+   |
+   = help: use `.collect()` to allocate the iterator
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0515`.
diff --git a/src/test/ui/static/static-reference-to-fn-2.stderr b/src/test/ui/static/static-reference-to-fn-2.stderr
index 028e11a60ce..ff15884bd44 100644
--- a/src/test/ui/static/static-reference-to-fn-2.stderr
+++ b/src/test/ui/static/static-reference-to-fn-2.stderr
@@ -40,6 +40,8 @@ LL | |         statefn: &id(state1 as StateMachineFunc)
    | |                   ------------------------------ temporary value created here
 LL | |     }
    | |_____^ returns a value referencing data owned by the current function
+   |
+   = help: use `.collect()` to allocate the iterator
 
 error: aborting due to 4 previous errors