-
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
to_vec
on for loop expression
#8069
Comments
Note that in |
Also But nevertheless it should be linted |
But then we should be careful while linting:
Even then there is no guarantee of the type's |
This can be covered by |
Are we proposing that there be a code suggestion to rewrite the entire loop? |
No. I would either add |
Please forgive me, @llogiq, but it sounds like you have some idea as to how you would make this determination. If that's right, could you please share? (If not, that's cool too.) EDIT: Determine whether |
I was thinking about letting an |
In the specific example in my opening post it isn't necessary to make a copy or clone, because the elements are use as reference only. |
Thanks, @llogiq. I'll give this a shot. |
I am wondering if what we're describing should be two lints. There's essentially two things going on here:
Notionally, what we're saying is: notice the first bullet, transform it into the form of the second, and then determine whether the second bullet applies. But each of the above bullets could appear in code, and so each could be linted for independently. So what I am proposing concretely is to include just the first bullet in I am happy to tackle both. I just think it might make sense to separate them into two lints. Thoughts? |
I think both variants could be |
Consider this example (which is meant to be similar to the original example): fn check_files(file_types: &[FileType]) -> bool {
for t in file_types.to_vec() {
let path = match get_file_path(&t) {
Ok(p) => p,
Err(_) => {
return false;
},
};
if !path.is_file() {
return false;
}
}
true
}
fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> {
Ok(std::path::PathBuf::new())
} If you drop the |
Yes. Otherwise the lint would have to suggest both removing the |
So, I guess I could add suggestions to remove those
But that would be overkill? (Sorry, I should have thought about this more.) |
I think doing two lints in one, could be error prone and sometimes not replicable because a lint is triggered from two different places. So, ... ? |
Sorry, @hellow554, I'm not sure what you're suggesting. What I wrote was probably unclear. I meant the lint could suggest to change multiple lines. To elaborate, the error message would look something like this:
|
So I think the question is: would it be too unruly to have a |
Either this or leave it to the |
I think I have this working in #7978. |
@hellow554 #7978 was merged. Maybe we can close this? |
@smoelius you could have added this issue to your close keyword so it would have been automatically closed ;) thanks for the hard work! I really appreciate this! That's amazing work you have done. |
What it does
Warns about a
to_vec()
on a for loop expression.There is this real world example (please don't see as an offence!):
I know, that there are similar lints, e.g. for a String, that gets imediatly dereferenced to a
&str
, so maybe one could extend that lint?Lint Name
intermediate_vec_on_loop_expression
Category
perf
Advantage
Remove the creation of an intermediate object and allocation
Drawbacks
no as far as I can tell
Example
Could be written as:
The text was updated successfully, but these errors were encountered: