-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix suggestions that need parens in from_iter_instead_of_collect
lint
#6657
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 all commits
Commits
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,48 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::from_iter_instead_of_collect)] | ||
#![allow(unused_imports)] | ||
|
||
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque}; | ||
use std::iter::FromIterator; | ||
|
||
fn main() { | ||
let iter_expr = std::iter::repeat(5).take(5); | ||
let _ = iter_expr.collect::<Vec<_>>(); | ||
|
||
let _ = vec![5, 5, 5, 5].iter().enumerate().collect::<HashMap<usize, &i8>>(); | ||
|
||
Vec::from_iter(vec![42u32]); | ||
|
||
let a = vec![0, 1, 2]; | ||
assert_eq!(a, (0..3).collect::<Vec<_>>()); | ||
assert_eq!(a, (0..3).collect::<Vec<i32>>()); | ||
|
||
let mut b = (0..3).collect::<VecDeque<_>>(); | ||
b.push_back(4); | ||
|
||
let mut b = (0..3).collect::<VecDeque<i32>>(); | ||
b.push_back(4); | ||
|
||
{ | ||
use std::collections; | ||
let mut b = (0..3).collect::<collections::VecDeque<i32>>(); | ||
b.push_back(4); | ||
} | ||
|
||
let values = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]; | ||
let bm = values.iter().cloned().collect::<BTreeMap<_, _>>(); | ||
let mut bar = bm.range(0..2).collect::<BTreeMap<_, _>>(); | ||
bar.insert(&4, &'e'); | ||
|
||
let mut bts = (0..3).collect::<BTreeSet<_>>(); | ||
bts.insert(2); | ||
{ | ||
use std::collections; | ||
let _ = (0..3).collect::<collections::BTreeSet<_>>(); | ||
let _ = (0..3).collect::<collections::BTreeSet<u32>>(); | ||
} | ||
|
||
for _i in [1, 2, 3].iter().collect::<Vec<_>>() {} | ||
for _i in [1, 2, 3].iter().collect::<Vec<&i32>>() {} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,48 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::from_iter_instead_of_collect)] | ||
#![allow(unused_imports)] | ||
|
||
use std::collections::HashMap; | ||
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque}; | ||
use std::iter::FromIterator; | ||
|
||
fn main() { | ||
let iter_expr = std::iter::repeat(5).take(5); | ||
Vec::from_iter(iter_expr); | ||
let _ = Vec::from_iter(iter_expr); | ||
|
||
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); | ||
let _ = HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); | ||
|
||
Vec::from_iter(vec![42u32]); | ||
|
||
let a = vec![0, 1, 2]; | ||
assert_eq!(a, Vec::from_iter(0..3)); | ||
assert_eq!(a, Vec::<i32>::from_iter(0..3)); | ||
|
||
let mut b = VecDeque::from_iter(0..3); | ||
b.push_back(4); | ||
|
||
let mut b = VecDeque::<i32>::from_iter(0..3); | ||
b.push_back(4); | ||
|
||
{ | ||
use std::collections; | ||
let mut b = collections::VecDeque::<i32>::from_iter(0..3); | ||
b.push_back(4); | ||
} | ||
|
||
let values = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]; | ||
let bm = BTreeMap::from_iter(values.iter().cloned()); | ||
let mut bar = BTreeMap::from_iter(bm.range(0..2)); | ||
bar.insert(&4, &'e'); | ||
|
||
let mut bts = BTreeSet::from_iter(0..3); | ||
bts.insert(2); | ||
{ | ||
use std::collections; | ||
let _ = collections::BTreeSet::from_iter(0..3); | ||
let _ = collections::BTreeSet::<u32>::from_iter(0..3); | ||
} | ||
|
||
for _i in Vec::from_iter([1, 2, 3].iter()) {} | ||
for _i in Vec::<&i32>::from_iter([1, 2, 3].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 |
---|---|---|
@@ -1,16 +1,88 @@ | ||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:8:5 | ||
--> $DIR/from_iter_instead_of_collect.rs:11:13 | ||
| | ||
LL | Vec::from_iter(iter_expr); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `iter_expr.collect()` | ||
LL | let _ = Vec::from_iter(iter_expr); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `iter_expr.collect::<Vec<_>>()` | ||
| | ||
= note: `-D clippy::from-iter-instead-of-collect` implied by `-D warnings` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:10:5 | ||
--> $DIR/from_iter_instead_of_collect.rs:13:13 | ||
| | ||
LL | HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `vec![5, 5, 5, 5].iter().enumerate().collect()` | ||
LL | let _ = HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `vec![5, 5, 5, 5].iter().enumerate().collect::<HashMap<usize, &i8>>()` | ||
|
||
error: aborting due to 2 previous errors | ||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:18:19 | ||
| | ||
LL | assert_eq!(a, Vec::from_iter(0..3)); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<Vec<_>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:19:19 | ||
| | ||
LL | assert_eq!(a, Vec::<i32>::from_iter(0..3)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<Vec<i32>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:21:17 | ||
| | ||
LL | let mut b = VecDeque::from_iter(0..3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<VecDeque<_>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:24:17 | ||
| | ||
LL | let mut b = VecDeque::<i32>::from_iter(0..3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<VecDeque<i32>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:29:21 | ||
| | ||
LL | let mut b = collections::VecDeque::<i32>::from_iter(0..3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<collections::VecDeque<i32>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:34:14 | ||
| | ||
LL | let bm = BTreeMap::from_iter(values.iter().cloned()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `values.iter().cloned().collect::<BTreeMap<_, _>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:35:19 | ||
| | ||
LL | let mut bar = BTreeMap::from_iter(bm.range(0..2)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `bm.range(0..2).collect::<BTreeMap<_, _>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:38:19 | ||
| | ||
LL | let mut bts = BTreeSet::from_iter(0..3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<BTreeSet<_>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:42:17 | ||
| | ||
LL | let _ = collections::BTreeSet::from_iter(0..3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<collections::BTreeSet<_>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:43:17 | ||
| | ||
LL | let _ = collections::BTreeSet::<u32>::from_iter(0..3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<collections::BTreeSet<u32>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:46:15 | ||
| | ||
LL | for _i in Vec::from_iter([1, 2, 3].iter()) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `[1, 2, 3].iter().collect::<Vec<_>>()` | ||
|
||
error: usage of `FromIterator::from_iter` | ||
--> $DIR/from_iter_instead_of_collect.rs:47:15 | ||
| | ||
LL | for _i in Vec::<&i32>::from_iter([1, 2, 3].iter()) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `[1, 2, 3].iter().collect::<Vec<&i32>>()` | ||
|
||
error: aborting due to 14 previous errors | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this case? The suggested code seems not to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed, turbofish should be customized to match the type implementing
from_iter
(hereVecDeque
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks better now.
I wanted to shorten the suggestion from
(0..3).collect::<std::collections::VecDeque<i32>>()
to(0..3).collect::<VecDeque<i32>>()
by removing unneededstd::collections
but I am not sure this is even possible so I need to dig more into this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find any util method. IMHO it might be good to just convert
ty
toString
and use.split("::").last().unwrap()
if.contains("::")
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I thought about this also, but it sounds a bit too hacky 😄
Ultimately I will consider it if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, and yes it's a little hacky. FYI similar codes already exist here
rust-clippy/clippy_lints/src/macro_use.rs
Lines 79 to 88 in e5a1f22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, using items in scope to shorten the paths seems like a worthwile improvement. However, I don't think that should block this PR.
So I'd r+ this unless you want to continue improving it, @ThibsG.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to give a try to improve this. I should be able to work on this really soon, and I keep you posted 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try
cx.tcx.trimmed_def_paths(LOCAL_CRATE).get(&def_id)