-
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
Implement flat_map lint #4231
Implement flat_map lint #4231
Conversation
PR body needs a changelog entry Also, not sure if this lint name is appropriate |
Maybe |
Does this handle |
☔ The latest upstream changes (presumably #4259) made this pull request unmergeable. Please resolve the merge conflicts. |
Ping from triage @jeremystucki. Do you want to continue working on this? |
Ah sorry totally forgot about that one, I've been really busy. |
No worries. Just return to this once you have more time. I labeled it with |
clippy_lints/src/methods/mod.rs
Outdated
if match_trait_method(cx, expr, &paths::ITERATOR); | ||
|
||
if flat_map_args.len() == 2; | ||
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_args[1].node; |
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.
You could insert the then
of the if_chain!
here and then split up the two if chains for |x| x
and convert::identity
, like this:
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_args[1].node; | |
then { | |
let arg = flat_map_args[1]; | |
if_chain! { | |
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg.node; | |
// check for `|x| x` | |
} | |
if_chain! { | |
if let hir::ExprKind::Path(ref qpath) = arg.node; | |
// check for `convert::identity` | |
} | |
} |
Co-authored-by: Philipp Krones <hello@philkrones.com>
r? @flip1995 |
clippy_lints/src/methods/mod.rs
Outdated
/// iter.flatten(); | ||
/// ``` | ||
pub FLAT_MAP_IDENTITY, | ||
pedantic, |
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.
This should be a style
or complexity
lint IMO.
Everything else LGTM
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.
You have to run update_lints
Also both lints can be easily implemented as MachineApplicable
lints. Steps to do this:
- swap the
span_lint
function with thespan_lint_and_sugg
function - As the span, only use the span of the call to
flat_map(..)
- Add the sugg-message: "try" (or something similar)
- Add as the sugg: "flatten()"
- In the second
if_chain!
: don't shadowexpr
, but name itarg
or something else. (This binding can also be shared between both innerif_chain!
s) - Remove "This can be simplified by calling
flatten()
" from the lint message. - Add
// run-rustfix
to the top of the test file - Update the
.stderr
file.
I can't quite figure out how to get the correct span. Can anyone help me? This is the current diff:
|
The refactor looks really good! It seems, that you have to construct the span (In the apply_lints closure): span.with_hi(expr.span.hi()) This should give you the correct span. |
@bors r+ Thanks! |
📌 Commit 2bfcf89 has been approved by |
Implement flat_map lint Fixes #4224 changelog: New Lint `flat_map_identity` to detect unnecessary calls to `flat_map`
💔 Test failed - status-appveyor |
Implement flat_map lint Fixes #4224 changelog: New Lint `flat_map_identity` to detect unnecessary calls to `flat_map`
☀️ Test successful - checks-travis, status-appveyor |
This commit fixes the file names of the `flat_map_identity` test. Previously, their names were started with `unnecessary_flat_map` even though the lint rule name is `flat_map_identity`. This inconsistency happened probably because the rule name was changed during the discussion in the PR where this rule was introduced. ref: rust-lang#4231
Fix file names of flat_map_identity test This patch fixes the file names of the `flat_map_identity` test. Previously, their names were started with `unnecessary_flat_map` even though the lint rule name is `flat_map_identity`. This inconsistency happened probably because the rule name was changed during the discussion in the PR where this rule was introduced. ref: #4231 changelog: none
Fixes #4224
changelog: New Lint
flat_map_identity
to detect unnecessary calls toflat_map