Repository navigation
Add double_rev_map lint - #17686
Add double_rev_map lint#17686willwang-io wants to merge 1 commit into
double_rev_map lint#17686Conversation
|
Thanks for the pull request, and welcome! You should hear from one of our reviewers after this PR gets at least 2 reviews from the community. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
f177aa1 to
a14f996
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
I am a bit baffled that people actually do this, but they do..
or at least meta and a bunch of similarly vibed projects do
https://github.com/search?q=%2F%5C.rev%5C%28%5C%29.map%5C%28.%2B%5C%29.rev%5C%28%5C%29%2F&type=code
|
No changes for f5dd6b8 |
f64df2f to
fc4a880
Compare
There was a problem hiding this comment.
Can we add a test where the receiver comes from a macro expansion like this:
vec![0, 1, 2]
.into_iter()
.rev()
.map(|x| x + 1)
.rev()
.collect();I think it is reasonable to lint here since all the three method calls share the same context and are written by the user, and only the receiver originates from a macro.
|
This shouldn't lint on the other hand: macro_rules! reversed {
($iter:expr) => {
$iter.rev()
};
}
let _: Vec<_> = reversed!(0..3)
.map(|x| x + 1)
.rev()
.collect(); |
fc4a880 to
d537f55
Compare
This comment has been minimized.
This comment has been minimized.
|
(make sure to squash the commits into one commit) |
| diag.multipart_suggestion( | ||
| "remove both calls to `rev`", | ||
| vec![(inner_rev_span, String::new()), (outer_rev_span, String::new())], | ||
| Applicability::MaybeIncorrect, |
There was a problem hiding this comment.
I can't think of a case where it would be incorrect to remove both rev calls, can this be possibly machine applicable or do you have some example in mind?
There was a problem hiding this comment.
I originally had this as machine-applicable, but changed it following @CommanderStorm's earlier review: #17686 (comment).
The concern was that removing both reversals preserve the existing iteration behaviour, but might hide a misunderstanding if the author intended reverse-order mapping. MaybeIncorrect leaves that decision to the user. I don't have a counterexample involving iteration order in mind..
There was a problem hiding this comment.
lets add a comment please.
2/2 getting confused on this is a good signal that this needs a comment.
There was a problem hiding this comment.
Add comment: "Removing both reversals preserves the current behavior, but the author may have intended the mapping closure to run in reverse order."
|
r? @llogiq rustbot has assigned @llogiq for the project review. Use Why was this reviewer chosen?The reviewer was selected based on:
|
bf93493 to
462bd8a
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
462bd8a to
84d8963
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
Detect iterator chains that call rev before and after map. Although the chain appears to run the mapping function in reverse order, the reversals cancel and the closure runs in the iterator's original order. Suggest removing both rev calls and explain how to preserve reverse mapping order when that was intended. Restrict the lint to Iterator methods to avoid matching same-named inherent methods. # Conflicts: # CHANGELOG.md # clippy_lints/src/lib.rs
84d8963 to
f5dd6b8
Compare
Detect iterator chains that call rev before and after map. Although the chain appears to run the mapping function in reverse order, the reversals cancel and the closure runs in the iterator's original order.
Suggest removing both rev calls and explain how to preserve reverse mapping order when that was intended. Restrict the lint to Iterator methods to avoid matching same-named inherent methods.
changelog:[
double_rev_map]: warn whenmapis surrounded by cancellingrevcalls.Fixes: #17587
.stderrfile)cargo testpasses locallycargo dev update_lintscargo dev fmt