-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add new trivial_map_over_range
lint
#13034
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @y21 (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
da1c65e
to
91a94cf
Compare
☔ The latest upstream changes (presumably #12873) made this pull request unmergeable. Please resolve the merge conflicts. |
6f27001
to
768963e
Compare
I can agree on that this is more explicit and better expresses intent, but I feel like the range map pattern is pretty common and an accepted way of collecting into vecs and generally considered idiomatic, so I'm a bit unsure about the category here and this being warn-by-default. Also the |
Thank you for your speedy response! I would argue that if you are using a I always have a mental hiccough when I see this pattern and have to think about why it was written that way (does the author want to highlight that really we aren't taking At the end of the day though this is perhaps a slightly personal-stylistic and opinionated lint. @y21, would you consider this under a different category? Footnotes
|
☔ The latest upstream changes (presumably #10155) made this pull request unmergeable. Please resolve the merge conflicts. |
The reason for why I brought up the missing Other warnings from a lintcheck run
Most of them are true positives though, except for the mentioned regex case. For the category, I would personally put this in pedantic or even restriction because I still feel like this is such a common pattern (e.g. clippy itself has a suggestion that goes against this lint), but we can also wait until the FCP where other team members have a last look over this and we can see what others think. (It's entirely possible that more people disagree with my opinion :)) |
Thank you for your detailed comments. This is my first clippy lint and my first time working with things like |
No worries, I'll change the label here to clean up my queue, you can write @rustbot author |
@rustbot ready |
☔ The latest upstream changes (presumably #13125) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
Sorry for taking a bit longer to respond here, have been a bit busy recently 😓
clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs
Outdated
Show resolved
Hide resolved
clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs
Outdated
Show resolved
Hide resolved
There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged. You can start a rebase with the following commands:
The following commits are merge commits: |
363b547
to
bda0423
Compare
@rustbot ready |
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.
Three small things then I think this should be good :)
clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs
Outdated
Show resolved
Hide resolved
clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs
Outdated
Show resolved
Hide resolved
1da8d2c
to
e55b47c
Compare
Thanks for guiding this PR @y21 . I've made your suggested changes and rebased, but am happy to take further suggestions/nix the lint if we decide against it. |
☔ The latest upstream changes (presumably #13421) made this pull request unmergeable. Please resolve the merge conflicts. |
It's been a few weeks since the FCP has started. A few things have come up:
|
Hey @rspencer01, this is a ping from triage, since there hasn't been any activity in some time. Have you seen the latest comment? Once that part is fixed, this should be good to go :D If you have any questions, you're always welcome to ask them in this PR or on Zulip. @rustbot author |
Thanks, this is on my todo list! Sorry its taken me a while to get back to it. I hope to have fixes for the two comments soon. |
52d35de
to
208a7e6
Compare
Checks seem to be failing due to an unrelated @rustbot ready |
Hm, might be spurious? Let's see what's up with that error. @bors try |
Add new `trivial_map_over_range` lint This lint checks for code that looks like ```rust let something : Vec<_> = (0..100).map(|_| { 1 + 2 + 3 }).collect(); ``` which is more clear as ```rust let something : Vec<_> = std::iter::repeat_with(|| { 1 + 2 + 3 }).take(100).collect(); ``` That is, a map over a range which does nothing with the parameter passed to it is simply a function (or closure) being called `n` times and could be more semantically expressed using `take`. - [x] Followed [lint naming conventions][lint_naming] - [x] Added passing UI tests (including committed `.stderr` file) - [x] `cargo test` passes locally - [x] Executed `cargo dev update_lints` - [x] Added lint documentation - [x] Run `cargo dev fmt` changelog: new lint: [`trivial_map_over_range`] `restriction`
☀️ Try build successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
/// let f : Vec<_> = std::iter::repeat( 3 + 1 ).take(10).collect(); | ||
/// ``` | ||
/// | ||
/// ### Known Issues |
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.
Good news: Take<Repeat>
and Take<RepeatWith>
now implement DoubleEndedIterator and ExactSizeIterator as of 1.82.0, so I think we can remove this note?
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.
Ah, I'd missed that in the notes. Does Take<RepeatWith>
implement DoubleEndedIterator
though? The playground (and release notes) suggest not.
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.
Right, it doesn't. I only actually looked at ExactSizeIterator and made wrong assumptions about DoubleEndedIterator
ex.span.shrink_to_hi(), | ||
if use_take { | ||
format!(".take({count})") | ||
} else { | ||
String::new() | ||
}, |
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.
These empty replacements (empty suggestion and empty span) trigger a debug assertion in the compiler, so this needs to conditionally push the replacement into the vec
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 think it would be easier to just filter the vec. Let me know if you disagree stylistically.
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 don't think the filtering is easier than something like
let mut parts = vec![
(receiver.span.to(method_call_span), format!("std::iter::{method_to_use_name}")),
new_span,
];
if use_take {
parts.push((ex.span.shrink_to_hi(), format!(".take({count})")));
}
I also find it a lot clearer because the current filtering makes me think that there could be other replacements that are empty when it's (as far as I can tell?) really only the last one, and the last one can only be empty because of an explicit String::new()
so it would be IMO clearer to just not create an empty replacement like that in the first place and only push a non-empty replacement if necessary.
(The debug assertion also only exists to find bugs in suggestions, so it feels more like a workaround)
The lintcheck failure is weird, no idea what's up with that. Maybe try another rebase? Haven't seen this in other PRs |
3e199e9
to
83d3e9d
Compare
☔ The latest upstream changes (presumably #13437) made this pull request unmergeable. Please resolve the merge conflicts. |
Looks all good to me now. Can you rebase one last time? Then I can r+ |
This lint checks for code that looks like ```rust let something : Vec<_> = (0..100).map(|_| { 1 + 2 + 3 }).collect(); ``` which is more clear as ```rust let something : Vec<_> = std::iter::repeat_with(|| { 1 + 2 + 3 }).take(100).collect(); ``` or ```rust let something : Vec<_> = std::iter::repeat_n(1 + 2 + 3, 100) .collect(); ``` That is, a map over a range which does nothing with the parameter passed to it is simply a function (or closure) being called `n` times and could be more semantically expressed using `take`.
83d3e9d
to
acc3842
Compare
Thanks! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Again, thank you @y21 for your guidance! |
This lint checks for code that looks like
which is more clear as
That is, a map over a range which does nothing with the parameter passed to it is simply a function (or closure) being called
n
times and could be more semantically expressed usingtake
..stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt
changelog: new lint: [
trivial_map_over_range
]restriction