Skip to content

Conversation

jeremystucki
Copy link
Contributor

@jeremystucki jeremystucki commented Jun 24, 2019

Fixes #4224

changelog: New Lint flat_map_identity to detect unnecessary calls to flat_map

@Manishearth
Copy link
Member

PR body needs a changelog entry

Also, not sure if this lint name is appropriate

@flip1995
Copy link
Member

Maybe FLAT_MAP_IDENTITY?

@lopopolo
Copy link

Does this handle .flat_map(std::convert::identity) too? 😄

@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Jul 3, 2019
@bors
Copy link
Contributor

bors commented Jul 17, 2019

☔ The latest upstream changes (presumably #4259) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995
Copy link
Member

flip1995 commented Aug 1, 2019

Ping from triage @jeremystucki. Do you want to continue working on this?

@jeremystucki
Copy link
Contributor Author

Ah sorry totally forgot about that one, I've been really busy.
I only got time in a week or so. Would you like to continue working on this in the meantime?

@flip1995 flip1995 added the E-help-wanted Call for participation: Help is requested to fix this issue. label Aug 5, 2019
@flip1995
Copy link
Member

flip1995 commented Aug 5, 2019

No worries. Just return to this once you have more time. I labeled it with help-wanted, in case someone wants to take over in the mean time.

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;
Copy link
Member

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:

Suggested change
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`
}
}

jeremystucki and others added 3 commits August 11, 2019 21:02
Co-authored-by: Philipp Krones <hello@philkrones.com>
@jeremystucki
Copy link
Contributor Author

jeremystucki commented Aug 12, 2019

r? @flip1995

/// iter.flatten();
/// ```
pub FLAT_MAP_IDENTITY,
pedantic,
Copy link
Member

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

Copy link
Member

@flip1995 flip1995 left a 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:

  1. swap the span_lint function with the span_lint_and_sugg function
  2. As the span, only use the span of the call to flat_map(..)
  3. Add the sugg-message: "try" (or something similar)
  4. Add as the sugg: "flatten()"
  5. In the second if_chain!: don't shadow expr, but name it arg or something else. (This binding can also be shared between both inner if_chain!s)
  6. Remove "This can be simplified by calling flatten()" from the lint message.
  7. Add // run-rustfix to the top of the test file
  8. Update the .stderr file.

@jeremystucki
Copy link
Contributor Author

jeremystucki commented Aug 12, 2019

I can't quite figure out how to get the correct span. Can anyone help me?

This is the current diff:

 error: called `flat_map(|x| x)` on an `Iterator`
   --> $DIR/unnecessary_flat_map.rs:9:14
    |
 LL |     iterator.flat_map(|x| x);
-   |              ^^^^^^^^^^^^^^^ help: try: `flatten()`
+   |              ^^^^^^^^ help: try: `flatten()`
    |
    = note: `-D clippy::flat-map-identity` implied by `-D warnings`
 
 error: called `flat_map(std::convert::identity)` on an `Iterator`
   --> $DIR/unnecessary_flat_map.rs:12:14
    |
 LL |     iterator.flat_map(convert::identity);
-   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
+   |              ^^^^^^^^ help: try: `flatten()`
 
 error: aborting due to 2 previous errors

@flip1995
Copy link
Member

flip1995 commented Aug 13, 2019

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.

@flip1995
Copy link
Member

@bors r+

Thanks!

@bors
Copy link
Contributor

bors commented Aug 14, 2019

📌 Commit 2bfcf89 has been approved by flip1995

@bors
Copy link
Contributor

bors commented Aug 14, 2019

⌛ Testing commit 2bfcf89 with merge 2cf7ee5...

bors added a commit that referenced this pull request Aug 14, 2019
Implement flat_map lint

Fixes #4224

changelog: New Lint `flat_map_identity` to detect unnecessary calls to `flat_map`
@bors
Copy link
Contributor

bors commented Aug 14, 2019

💔 Test failed - status-appveyor

@phansch
Copy link
Contributor

phansch commented Aug 14, 2019

@bors retry (should work after #4378)

@bors
Copy link
Contributor

bors commented Aug 14, 2019

⌛ Testing commit 2bfcf89 with merge 4f8bdf3...

bors added a commit that referenced this pull request Aug 14, 2019
Implement flat_map lint

Fixes #4224

changelog: New Lint `flat_map_identity` to detect unnecessary calls to `flat_map`
@bors
Copy link
Contributor

bors commented Aug 14, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing 4f8bdf3 to master...

@bors bors merged commit 2bfcf89 into rust-lang:master Aug 14, 2019
@jeremystucki jeremystucki deleted the flat-map branch August 14, 2019 11:42
magurotuna added a commit to magurotuna/rust-clippy that referenced this pull request Feb 3, 2021
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
bors added a commit that referenced this pull request Feb 3, 2021
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-help-wanted Call for participation: Help is requested to fix this issue. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint .flat_map(|x| x) into .flatten()
6 participants