-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 suggestions that need parens in from_iter_instead_of_collect
lint
#6657
Conversation
r? @llogiq (rust-highfive has picked a reviewer for you, use r? to override) |
from_iter_instead_of_collect
lint
from_iter_instead_of_collect
lintfrom_iter_instead_of_collect
lint
Edit WIP: forgot about the turbo fish, the suggestion is still not working (rustc cannot infer the type) |
f01c581
to
fa5fcc0
Compare
from_iter_instead_of_collect
lintfrom_iter_instead_of_collect
lint
@@ -10,4 +10,7 @@ fn main() { | |||
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)); |
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.
let mut b = VecDeque::from_iter(0..3);
// let mut b = `(0..3).collect::<Vec<_>>()` // the suggested code
b.push_back(4);
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
(here VecDeque
)
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 unneeded std::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
to String
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
if !self.collected.contains(&call_site) { | |
let name = if name.contains("::") { | |
name.split("::").last().unwrap().to_string() | |
} else { | |
name.to_string() | |
}; | |
self.mac_refs.push(MacroRefData::new(name, callee.def_site, cx)); | |
self.collected.insert(call_site); | |
} |
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)
fa5fcc0
to
7825bf3
Compare
Good improvement so far! If you need something, let me know. Otherwise I'd like to see more tests around the type inference problem outlined by @giraffate. |
@ThibsG this appears to fix the turbofish issue. Good tests, too 👍. Is this ready to merge or do you intend to keep improving this PR? |
Partially addresses #6533. This is by no means required for this PR, but I want to make a note. It would be nice to have suggestions that use
|
I reworked the turbofish extraction to cover cases like the ones suggested by @camsteffen. |
Great! I think we can have further improvements in a followup PR then. Thanks to both of you! @bors r+ |
📌 Commit b932587 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes broken suggestions that need parens (i.e.: range)
Fixes: #6648
changelog: none