Skip to content
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

issue#3424 improve filter_map lint docs #3461

Closed
wants to merge 8 commits into from

Conversation

waynr
Copy link
Contributor

@waynr waynr commented Nov 27, 2018

for #3424

@waynr
Copy link
Contributor Author

waynr commented Nov 27, 2018

I've tested all of these on the rust playground. One thing I'm not certain about is whether or not it's okay to create multiple example headings -- I thought it would be a good idea to create a separate section for each combination.

Also, I have to say that the filter + flat_map and filter_map + flat_map examples that I came up with don't inspire confidence that the suggested approach is actually better. I guess that's why it's marked as pedantic and lints can be pretty easily disabled in one's project...

@ghost
Copy link

ghost commented Nov 27, 2018

We had discussion about this lint in issue #3188 and concluded that filter(..).map(..) should only be replaced with filter_map if there is some unwrapping going on. IMO, filter(..).flat_map(..) and filter_map(..).flat_map(..) shouldn't be linted at all.

@ghost
Copy link

ghost commented Nov 27, 2018

Actually linting filter(..).flat_map(..) in is OK in some cases (#3188 has an example), but not filter_map(..).flat_map(..).

}

/// lint use of `filter().flat_map()` for `Iterators`
fn lint_filter_flat_map<'a, 'tcx>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikerite I added a commit to this PR that removes the linting of filter().flat_map() since I couldn't think of a simpler way to achieve the same behavior based on the example you gave. I'd be happy to add it back and include it in the updated docstring if you have any guidance for me here. Thanks!

@waynr waynr force-pushed the issue#3424 branch 2 times, most recently from 6926b28 to c2eb74f Compare December 6, 2018 00:20
@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 6, 2018
@bors
Copy link
Collaborator

bors commented Dec 12, 2018

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

@flip1995
Copy link
Member

Ping @waynr. I'm going over old PRs, that were abandoned by us reviewers (sorry for that!) or by the authors. Are you still interested in completing this?

@waynr
Copy link
Contributor Author

waynr commented Jun 19, 2019

@flip1995 I am still interested, it just fell off my radar! I'll try to get to it no later than Monday next week (but possibly as early as tonight). Thanks for the ping!

* issue#3188 make the 'filter_map' lint more specific
* Remove filter + flat_map lint
* Run 'util/dev update_lints'
* Update filter_methods.stderr
* Address dogfood ci failure
* Add FILTER_MAP_MAP to the methods LintPass
@waynr
Copy link
Contributor Author

waynr commented Jun 23, 2019

Okay I've resolved the merge conflicts and I think everything is order again so this should be ready for re-review. Reviewers may want to ensure that I haven't unintentionally changed anything important while rebasing -- I don't remember much about this work and haven't worked with Rust since so I'm missing much of the context I had back in December when originally working on it.

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.

Thanks for getting back to this!

cc @mikerite can you also take a look at this please?

clippy_lints/src/methods/mod.rs Show resolved Hide resolved
pedantic,
"using `filter_map` followed by `map` can more concisely be written as a single method call"
}

declare_clippy_lint! {
/// **What it does:** Checks for usage of `_.filter(_).map(_)`,
/// `_.filter(_).flat_map(_)`, `_.filter_map(_).flat_map(_)` and similar.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc needs to be adapted

tests/ui/filter_methods.rs Show resolved Hide resolved
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.

I need to do a in depth review of this, but probably won't have time for this until next week...

/// println!("{:?}", correct);
/// assert_eq!(correct, more_correct);
/// ```
/// .iter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to remove the old example code 😉

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.

I finally got to reviewing this more in depth. Sorry for the long wait time :(

I think removing both filter().flat_map() and filter_map().flat_map() is totally fine, since I can only think of some few edge cases where rewriting this into one function call would maybe make sense.

let mut span = expr.span;
if let hir::ExprKind::MethodCall(_, _, ref expr_vec) = expr.node {
if let hir::ExprKind::MethodCall(_, ref filter_span, _) = expr_vec[0].node {
span = Span::new(filter_span.lo(), expr.span.hi(), expr.span.ctxt());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a function for this on Span: https://doc.rust-lang.org/nightly/nightly-rustc/src/syntax_pos/lib.rs.html#217-219

Suggested change
span = Span::new(filter_span.lo(), expr.span.hi(), expr.span.ctxt());
span = expr.span.with_lo(filter_span.lo());


error: called `filter_map(p).flat_map(q)` on an `Iterator`. This is more succinctly expressed by calling `.flat_map(..)` and filtering by returning an empty Iterator.
--> $DIR/filter_methods.rs:13:21
error: redundant closure found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you allow redundant_closure for these tests?

.collect();

// validate iterator of options triggers filter + flat_map lint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was removed in this PR, wasn't it?

.map(|s| format!("{}{}", s, s))
.collect();

// validate iterator of values **does not** trigger filter + flat_map lint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: Wasn't this removed completely anyway?

@bors
Copy link
Collaborator

bors commented Jul 17, 2019

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

@phansch phansch added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 17, 2019
@phansch
Copy link
Member

phansch commented Apr 26, 2020

Hi @waynr, would you still be up to finish this PR? No worries if not, it's been a long time =)

@flip1995
Copy link
Member

Thanks for contributing to Clippy! Sadly this PR was not updated in quite some time. If you waited on input from a reviewer, we're sorry that this fell under the radar. If you want to continue to work on this, just reopen the PR and/or ping a reviewer.

@flip1995 flip1995 closed this May 25, 2020
@flip1995 flip1995 added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 25, 2020
@jonboh
Copy link
Contributor

jonboh commented Oct 10, 2023

@rustbot label -S-inactive-closed

@rustbot rustbot removed the S-inactive-closed Status: Closed due to inactivity label Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants