-
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
iter() -> into_iter() #7459
Comments
The lint should therefore only suggest the usage of I like the lint suggestion, and these are only some pointers for the implementation. @rustbot label +good-first-issue |
I am aware of the semantic differences. I just want to say if |
Could be a duplicate of #5305 ^^ We should be careful though. |
Using https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#intoiterator-for-arrays |
Hey @Luro02, could you elaborate your point a bit? I'm sadly not quite sure what you mean. 🙃 |
@rustbot claim |
I had a first try to tackle this issue and it seems to me that
So unlike into_iter_on_ref which is an expression lint this one should be a function wide lint somewhat similar to redundant_clone but a bit less complex I think. Do I make sense or am I missing something and going to the wrong direction? |
I played a bit with this one but it is way above my current knowledge. This what I got rsmantini#2 let v7: Vec<String> = vec!["a".into()];
// this triggers the lint :(
let _: Vec<&str> = v7.iter().map(|x| x.as_ref()).collect(); The code very naive, probably inefficient and might have reinvented the wheel in some dumb ways. Also I think this is way too complicated from a good-first-issue although I had fun with it :) |
Hey @rsmantini, sorry, I missed your first comment. These things sadly sometimes get lost. If you get stuck and no one answers you, please ping us directly or ask on Zulip 🙃 Anyway, I labeled this as a good first issue, my rough idea was outlined in this comment. The idea had these steps:
If you would rather not continue this issue, it's totally fine. It's good to hear that you at least had fun 🙃 |
Hi @xFrednet no worries :) About the implementation, I had trouble with step 4. Maybe I'm over complicating things, but I think it's analogous to what the borrow checker does, so it needs to be done at the mir level. fn foo() {
let v: Vec<String> = vec!["a".into()];
let _: Vec<&str> = v.iter().map(|x| x.as_ref()).collect();
} The collection |
Ohh, I didn't think about the fact that the new target value could still reference the old one from In Clippy we rarely interact with the borrow checker. I can't think of a lint where we actually do that. Often we just check for these rules by checking ownership based on types. But I'm unsure how that would be done in this case. Thank you for pointing that out 🙃 @rustbot label -good-first-issue |
As a cheap solution in this case, the lint could only trigger when the item type of the iterator before |
I think redundant_clone does something similar. I took some inspiration there but I still don't quite understand how the GenKillAnalysis work :(
Interesting idea, shouldn't be too hard to do. I wonder if there are methods other than |
Ah, my idea doesn't actually work.. This (silly) example compiles fine, but breaks with fn foo() {
let v: Vec<String> = vec!["1".into()];
let _: Vec<u32> = v
.iter()
.map(|x| x.as_str())
.map(|x| x.parse().unwrap())
.collect();
} |
What it does
Propose to change
iter()
tointo_iter()
Categories (optional)
What is the advantage of the recommended code over the original code
For example:
My old code:
foo.unwrap_or_default().iter()....
By hand I changed it to:
foo.unwrap_or_default().into_iter()....
Then clippy proposed to remove some
clone()
sWhen I removed the
clone()
s, clippy proposed to changefilter
+map
tofilter_map
clippy is awesome. The code looks beautiful now.
The suggestion to change
iter()
tointo_iter()
was missing and blocking the other lintsDrawbacks
None.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: