-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Special case iterator chain checks for suggestion #116717
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// run-rustfix | ||
use std::collections::hash_set::Iter; | ||
use std::collections::HashSet; | ||
|
||
fn iter_to_vec<'b, X>(i: Iter<'b, X>) -> Vec<X> where X: Clone { | ||
let i = i.map(|x| x.clone()); | ||
i.collect() //~ ERROR E0277 | ||
} | ||
|
||
fn main() { | ||
let v = vec![(0, 0)]; | ||
let scores = v | ||
.iter() | ||
.map(|(a, b)| { | ||
a + b | ||
}); | ||
println!("{}", scores.sum::<i32>()); //~ ERROR E0277 | ||
println!( | ||
"{}", | ||
vec![0, 1] | ||
.iter() | ||
.map(|x| x * 2) | ||
.map(|x| { x }) | ||
.map(|x| { x }) | ||
.sum::<i32>(), //~ ERROR E0277 | ||
); | ||
println!("{}", vec![0, 1].iter().map(|x| { x }).sum::<i32>()); //~ ERROR E0277 | ||
let a = vec![0]; | ||
let b = a.into_iter(); | ||
let c = b.map(|x| x + 1); | ||
let d = c.filter(|x| *x > 10 ); | ||
let e = d.map(|x| { | ||
x + 1 | ||
}); | ||
let f = e.filter(|_| false); | ||
let g: Vec<i32> = f.collect(); //~ ERROR E0277 | ||
println!("{g:?}"); | ||
|
||
let mut s = HashSet::new(); | ||
s.insert(1u8); | ||
println!("{:?}", iter_to_vec(s.iter())); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// run-rustfix | ||
use std::collections::hash_set::Iter; | ||
use std::collections::HashSet; | ||
|
||
fn iter_to_vec<'b, X>(i: Iter<'b, X>) -> Vec<X> { | ||
let i = i.map(|x| x.clone()); | ||
i.collect() //~ ERROR E0277 | ||
} | ||
|
||
fn main() { | ||
let v = vec![(0, 0)]; | ||
let scores = v | ||
.iter() | ||
.map(|(a, b)| { | ||
a + b; | ||
}); | ||
println!("{}", scores.sum::<i32>()); //~ ERROR E0277 | ||
println!( | ||
"{}", | ||
vec![0, 1] | ||
.iter() | ||
.map(|x| x * 2) | ||
.map(|x| { x; }) | ||
.map(|x| { x }) | ||
.sum::<i32>(), //~ ERROR E0277 | ||
); | ||
println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>()); //~ ERROR E0277 | ||
let a = vec![0]; | ||
let b = a.into_iter(); | ||
let c = b.map(|x| x + 1); | ||
let d = c.filter(|x| *x > 10 ); | ||
let e = d.map(|x| { | ||
x + 1; | ||
}); | ||
let f = e.filter(|_| false); | ||
let g: Vec<i32> = f.collect(); //~ ERROR E0277 | ||
println!("{g:?}"); | ||
|
||
let mut s = HashSet::new(); | ||
s.insert(1u8); | ||
println!("{:?}", iter_to_vec(s.iter())); | ||
} |
157 changes: 157 additions & 0 deletions
157
tests/ui/iterators/invalid-iterator-chain-fixable.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
error[E0277]: a value of type `Vec<X>` cannot be built from an iterator over elements of type `&X` | ||
--> $DIR/invalid-iterator-chain-fixable.rs:7:7 | ||
| | ||
LL | let i = i.map(|x| x.clone()); | ||
| ------- this method call is cloning the reference `&X`, not `X` which doesn't implement `Clone` | ||
LL | i.collect() | ||
| ^^^^^^^ value of type `Vec<X>` cannot be built from `std::iter::Iterator<Item=&X>` | ||
| | ||
= help: the trait `FromIterator<&X>` is not implemented for `Vec<X>` | ||
= help: the trait `FromIterator<X>` is implemented for `Vec<X>` | ||
= help: for that trait implementation, expected `X`, found `&X` | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/invalid-iterator-chain-fixable.rs:5:26 | ||
| | ||
LL | fn iter_to_vec<'b, X>(i: Iter<'b, X>) -> Vec<X> { | ||
| ^^^^^^^^^^^ `Iterator::Item` is `&X` here | ||
LL | let i = i.map(|x| x.clone()); | ||
| ------------------ `Iterator::Item` remains `&X` here | ||
note: required by a bound in `collect` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider further restricting type parameter `X` | ||
| | ||
LL | fn iter_to_vec<'b, X>(i: Iter<'b, X>) -> Vec<X> where X: Clone { | ||
| ++++++++++++++ | ||
|
||
error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `()` | ||
--> $DIR/invalid-iterator-chain-fixable.rs:17:33 | ||
| | ||
LL | println!("{}", scores.sum::<i32>()); | ||
| --- ^^^ value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=()>` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Sum<()>` is not implemented for `i32` | ||
= help: the following other types implement trait `Sum<A>`: | ||
<i32 as Sum> | ||
<i32 as Sum<&'a i32>> | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/invalid-iterator-chain-fixable.rs:14:10 | ||
| | ||
LL | let v = vec![(0, 0)]; | ||
| ------------ this expression has type `Vec<({integer}, {integer})>` | ||
LL | let scores = v | ||
LL | .iter() | ||
| ------ `Iterator::Item` is `&({integer}, {integer})` here | ||
LL | .map(|(a, b)| { | ||
| __________^ | ||
LL | | a + b; | ||
LL | | }); | ||
| |__________^ `Iterator::Item` changed to `()` here | ||
note: required by a bound in `std::iter::Iterator::sum` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider removing this semicolon | ||
| | ||
LL - a + b; | ||
LL + a + b | ||
| | ||
|
||
error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `()` | ||
--> $DIR/invalid-iterator-chain-fixable.rs:25:20 | ||
| | ||
LL | .sum::<i32>(), | ||
| --- ^^^ value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=()>` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Sum<()>` is not implemented for `i32` | ||
= help: the following other types implement trait `Sum<A>`: | ||
<i32 as Sum> | ||
<i32 as Sum<&'a i32>> | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/invalid-iterator-chain-fixable.rs:23:14 | ||
| | ||
LL | vec![0, 1] | ||
| ---------- this expression has type `Vec<{integer}>` | ||
LL | .iter() | ||
| ------ `Iterator::Item` is `&{integer}` here | ||
LL | .map(|x| x * 2) | ||
| -------------- `Iterator::Item` changed to `{integer}` here | ||
LL | .map(|x| { x; }) | ||
| ^^^^^^^^^^^^^^^ `Iterator::Item` changed to `()` here | ||
LL | .map(|x| { x }) | ||
| -------------- `Iterator::Item` remains `()` here | ||
note: required by a bound in `std::iter::Iterator::sum` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider removing this semicolon | ||
| | ||
LL - .map(|x| { x; }) | ||
LL + .map(|x| { x }) | ||
| | ||
|
||
error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `()` | ||
--> $DIR/invalid-iterator-chain-fixable.rs:27:60 | ||
| | ||
LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>()); | ||
| --- ^^^ value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=()>` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Sum<()>` is not implemented for `i32` | ||
= help: the following other types implement trait `Sum<A>`: | ||
<i32 as Sum> | ||
<i32 as Sum<&'a i32>> | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/invalid-iterator-chain-fixable.rs:27:38 | ||
| | ||
LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>()); | ||
| ---------- ------ ^^^^^^^^^^^^^^^ `Iterator::Item` changed to `()` here | ||
| | | | ||
| | `Iterator::Item` is `&{integer}` here | ||
| this expression has type `Vec<{integer}>` | ||
note: required by a bound in `std::iter::Iterator::sum` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider removing this semicolon | ||
| | ||
LL - println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>()); | ||
LL + println!("{}", vec![0, 1].iter().map(|x| { x }).sum::<i32>()); | ||
| | ||
|
||
error[E0277]: a value of type `Vec<i32>` cannot be built from an iterator over elements of type `()` | ||
--> $DIR/invalid-iterator-chain-fixable.rs:36:25 | ||
| | ||
LL | let g: Vec<i32> = f.collect(); | ||
| ^^^^^^^ value of type `Vec<i32>` cannot be built from `std::iter::Iterator<Item=()>` | ||
| | ||
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>` | ||
= help: the trait `FromIterator<i32>` is implemented for `Vec<i32>` | ||
= help: for that trait implementation, expected `i32`, found `()` | ||
note: the method call chain might not have had the expected associated types | ||
--> $DIR/invalid-iterator-chain-fixable.rs:32:15 | ||
| | ||
LL | let a = vec![0]; | ||
| ------- this expression has type `Vec<{integer}>` | ||
LL | let b = a.into_iter(); | ||
| ----------- `Iterator::Item` is `{integer}` here | ||
LL | let c = b.map(|x| x + 1); | ||
| -------------- `Iterator::Item` remains `{integer}` here | ||
LL | let d = c.filter(|x| *x > 10 ); | ||
| -------------------- `Iterator::Item` remains `{integer}` here | ||
LL | let e = d.map(|x| { | ||
| _______________^ | ||
LL | | x + 1; | ||
LL | | }); | ||
| |______^ `Iterator::Item` changed to `()` here | ||
LL | let f = e.filter(|_| false); | ||
| ----------------- `Iterator::Item` remains `()` here | ||
note: required by a bound in `collect` | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider removing this semicolon | ||
| | ||
LL - x + 1; | ||
LL + x + 1 | ||
| | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.