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

should_impl_trait - ignore methods with lifetime params #5725

Merged
merged 6 commits into from
Aug 16, 2020

Conversation

tnielens
Copy link
Contributor

@tnielens tnielens commented Jun 16, 2020

Fixes: #5617

changelog: don't lint should_implement_trait when an Iterator::next case has explicit parameters

@rust-highfive
Copy link

r? @yaahc

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 16, 2020
yaahc
yaahc previously requested changes Jun 16, 2020
Copy link
Member

@yaahc yaahc left a comment

Choose a reason for hiding this comment

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

Looks good, just needs a changelog: added to the body of the PR

cc @flip1995

clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
@ghost
Copy link

ghost commented Jun 17, 2020

Does this check handle elided lifetimes?

    pub fn next<'b>(&'b mut self) -> Option<&'b mut T> {
        unimplemented!();
    }

is equivalent to

    pub fn next(&mut self) -> Option<&mut T> {
        unimplemented!();
    }

but will the second form be linted?

Also, I think this check should only apply to the next method specifally.
A method like

    fn eq<'a, 'b>(&'a self, other: &'b Self) -> bool { unimplemented!() }

can be moved to a PartialEq implementation regardless of lifetimes.

@yaahc yaahc 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 Jun 17, 2020
@tnielens
Copy link
Contributor Author

The short-circuiting upon lifetime params is a little naive indeed.

I'll start by adding a test case for each trait method in the lint method list and see from there.

@tnielens
Copy link
Contributor Author

I've added a test case for each checked method. All cases can be written with lifetime elision. If the user persists in writing explicitly lifetime params, he'll first hit the needles_lifetime lint.

@tnielens
Copy link
Contributor Author

tnielens commented Jun 19, 2020

Does this check handle elided lifetimes?

It's a check done at hir level. Elided liftetimes are absent from the function declaration struct it seems.

clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
@tnielens
Copy link
Contributor Author

Three approaches to wrap this pr up:

  • This is a rare case, the user can explicitly allow the lint. Revert the lifetime filtering and keep test cases.
  • All trait methods implementations can be written with lifetime elision. The user is unlikely to write lifetimes explicitly down. If it does so he might have a good reason (like for the Iterator::next example, rust hasn't shipped GATs, the Iterator trait cannot model streaming iterators). Clippy should not lint these more complex cases as there is a risk of false positive. The pr is good so.
  • Iterator::next is a special case. Filter on explicit lifetimes only for that one. Other cases remain unchanged.

Any you'd agree with?

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.

  • Iterator::next is a special case. Filter on explicit lifetimes only for that one. Other cases remain unchanged.

Until now, I only remember issues complaining about the next method. To future prove this fix, you could add another field to the TRAIT_METHODS list, that specifies if the lint should be skipped, if there are lifetimes defined.

Extra points if you write a doc-comment for this list of tuples, what each field stands for (please :) ).


Sorry for ignoring this PR for so long, I didn't realize, that I was CCed.

tests/ui/methods.rs Outdated Show resolved Hide resolved
@tnielens tnielens force-pushed the should-impl-trait branch 3 times, most recently from 88e5dda to f300fcd Compare July 29, 2020 23:46
@tnielens
Copy link
Contributor Author

I changed the TRAIT_METHODS items from a large tuple to a new struct. That clarifies a bit the role of each field.

@tnielens tnielens force-pushed the should-impl-trait branch 3 times, most recently from 3129220 to d8f898b Compare July 30, 2020 00:08
@tnielens
Copy link
Contributor Author

tnielens commented Jul 30, 2020

Do you mind if I introduce an exclusion rule for the stderr line count limit?

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.

Please don't use double underscores for test names. If you want to structure the tests better rather put them in their own directory.

tests/ui/methods.rs Show resolved Hide resolved
tests/ui/should_impl_trait__corner_cases.stderr Outdated Show resolved Hide resolved
tests/ui/should_impl_trait__method_list.rs Outdated Show resolved Hide resolved
@flip1995
Copy link
Member

Can you put the tests for this lint in its own directory? There's still an issue with undefined structs in the test cases.

@flip1995
Copy link
Member

Please remove the tests/ui/should_impl_trait/corner_cases.stderr file and this should be good to go.

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

flip1995 commented Aug 4, 2020

@bors r=flip1995,yaahc rollup=always

@bors
Copy link
Collaborator

bors commented Aug 4, 2020

📌 Commit 108032e has been approved by flip1995,yaahc

flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Aug 4, 2020
…95,yaahc

should_impl_trait - ignore methods with lifetime params

Fixes: rust-lang#5617

changelog: don't lint should_implement_trait when an `Iterator::next` case has explicit parameters
@bors
Copy link
Collaborator

bors commented Aug 4, 2020

⌛ Testing commit 108032e with merge 9df7e19...

bors added a commit that referenced this pull request Aug 4, 2020
should_impl_trait - ignore methods with lifetime params

Fixes: #5617

changelog: don't lint should_implement_trait when an `Iterator::next` case has explicit parameters
@bors
Copy link
Collaborator

bors commented Aug 4, 2020

💔 Test failed - checks-action_test

bors added a commit that referenced this pull request Aug 4, 2020
Rollup of 6 pull requests

Successful merges:

 - #5725 (should_impl_trait - ignore methods with lifetime params)
 - #5837 (needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ...)
 - #5846 (Handle mapping to Option in `map_flatten` lint)
 - #5848 (Add derive_ord_xor_partial_ord lint)
 - #5852 (Add lint for duplicate methods of trait bounds)
 - #5856 (Remove old Symbol reexport)

Failed merges:

r? @ghost

changelog: rollup
bors added a commit that referenced this pull request Aug 4, 2020
Rollup of 6 pull requests

Successful merges:

 - #5725 (should_impl_trait - ignore methods with lifetime params)
 - #5837 (needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ...)
 - #5846 (Handle mapping to Option in `map_flatten` lint)
 - #5848 (Add derive_ord_xor_partial_ord lint)
 - #5852 (Add lint for duplicate methods of trait bounds)
 - #5856 (Remove old Symbol reexport)

Failed merges:

r? @ghost

changelog: rollup
@flip1995
Copy link
Member

flip1995 commented Aug 4, 2020

Please fix the dogfood errors.

@flip1995 flip1995 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-bors Status: The marked PR was approved and is only waiting bors labels Aug 4, 2020
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! Waiting for rustup

@flip1995 flip1995 added S-waiting-on-bors Status: The marked PR was approved and is only waiting bors and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Aug 12, 2020
@flip1995
Copy link
Member

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Aug 16, 2020

📌 Commit f9ba829 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Aug 16, 2020

⌛ Testing commit f9ba829 with merge 3bd9889...

@bors
Copy link
Collaborator

bors commented Aug 16, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 3bd9889 to master...

@bors bors merged commit 3bd9889 into rust-lang:master Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: The marked PR was approved and is only waiting bors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive in should_implement_trait
6 participants