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.
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.
This should be a style or complexity lint IMO.
Everything else LGTM
flip1995
left a comment
There was a problem hiding this comment.
You have to run update_lints
Also both lints can be easily implemented as MachineApplicable lints. Steps to do this:
- swap the
span_lintfunction with thespan_lint_and_suggfunction - 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 itargor 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-rustfixto the top of the test file - Update the
.stderrfile.
|
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_identityto detect unnecessary calls toflat_map