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

wrong_self_convention: fix FP inside trait impl for to_* method taking &self #7002

Conversation

mgacek8
Copy link
Contributor

@mgacek8 mgacek8 commented Mar 29, 2021

fixes #6983
changelog: wrong_self_convention: fix FP inside trait impl for to_* method taking &self

@rust-highfive
Copy link

r? @phansch

(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 Mar 29, 2021
@mgacek8 mgacek8 force-pushed the issue6983_wrong_self_convention_inside_trait_impls branch from 10d9622 to 409cbe8 Compare March 29, 2021 20:37
When the `to_*` method takes `&self` and it is a trait implementation,
we don't trigger the lint.
@mgacek8 mgacek8 force-pushed the issue6983_wrong_self_convention_inside_trait_impls branch from 409cbe8 to 6966c78 Compare March 30, 2021 06:49
@phansch
Copy link
Member

phansch commented Apr 1, 2021

@bors r+ thanks!

@bors
Copy link
Collaborator

bors commented Apr 1, 2021

📌 Commit 6966c78 has been approved by phansch

@bors
Copy link
Collaborator

bors commented Apr 1, 2021

⌛ Testing commit 6966c78 with merge 38b1fd0...

@bors
Copy link
Collaborator

bors commented Apr 1, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: phansch
Pushing 38b1fd0 to master...

@bors bors merged commit 38b1fd0 into rust-lang:master Apr 1, 2021
Comment on lines +2 to +4
--> $DIR/wrong_self_convention2.rs:28:19
|
LL | fn to_u64(self) -> u64 {
Copy link
Member

Choose a reason for hiding this comment

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

This still triggers on the trait implementation, not on the trait definition. Is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I put that intentionally.
The issue pointed out that a trait method signature is controlled by the trait, and there might be other types implementing the same trait that are not Copy. That's why we should allow a Copy type to take &self instead of just self in the case of to_* method when it comes from a trait.
But, the other way around, where we have a to_* method from a trait that requires self and the type implementing it is not Copy then probably that's not something desirable. As I see (e.g. based on to_string(&self) from std::string::ToString trait) if you expect types implementing your trait to not always be Copy you should mark your to_* method as &self. That's why I thought a lint, in this case, might be useful. Having said that, I'm not super strong on that, so if someone thinks differently that we should also allow this case, I can change that.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, but in order to change this, you would also have to change the trait definition, so you could just as well directly lint on the trait definition. Do you have a reason why it shouldn't lint on trait definitions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One reason is that from trait definition alone you don't know what types will implement it. It might be that a particular trait is implemented only by Copy types. E.g. as in the #6727

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. Do you think we should stop linting completely then, if traits are involved?

Copy link
Member

Choose a reason for hiding this comment

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

Are you motivated to put some more work into this, so that this lint doesn't trigger on the to variant in trait definitions and implementations anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, sure, I can do that. Do you think there might be some valid use-cases for that scenario ? (non-Copy type implementing trait's to_* method taking self). Or is it just better to stay less stringent?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, sure, I can do that.

Thanks!

Do you think there might be some valid use-cases for that scenario ?

Yes, I think there is value to a lint saying "your trying to implement a trait that is probably meant for a Copy type, but you're implementing it on a non-Copy type". But I don't think we should do this in this lint, but rather make a new allow-by-default (pedantic) lint for this. I don't think Clippy should warn by default for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, agree that having this as allow-by-default would be better!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR: #7182

bors added a commit that referenced this pull request May 7, 2021
…ip1995

For `to_*` variant don't lint in trait impl taking `self` when non-`Copy` type

Lint name: `wrong_self_convention`.
It relaxes rules for `to_*` variant, so it doesn't lint in trait definitions and implementations anymore.
Although, non-`Copy` type implementing trait's `to_*` method taking
`self` feels not good (consumes ownership, so should be rather named `into_`), it would be better if this case was a pedantic lint (allow-by-default) instead.
More information in the discussion with `@flip1995` [here](#7002 (comment))

changelog: `wrong_self_convention`: For `to_*` variant don't lint in trait impl taking `self` when non-`Copy` type
r? `@flip1995`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive wrong_self_convention inside trait impls
5 participants