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

warn impl Deref #13665

Open
cathaysia opened this issue Nov 8, 2024 · 0 comments
Open

warn impl Deref #13665

cathaysia opened this issue Nov 8, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@cathaysia
Copy link

cathaysia commented Nov 8, 2024

What it does

deref is usually implemented for transparent types such as pointers, but some people use it for other purposes, such as polymorphism, etc.

Without the help of lsp, it is difficult for the reader to know where this method comes from.

Advantage

More clear code.

Drawbacks

If possible, we should warn users when they implement deref

Example

struct Parent {}

impl Parent {

   fn some_op(&self) {}
}

struct Child {
    parent: Parent 
}

impl Deref<Parent> for Child {
       // ^ warning: DON'T implement Deref
  // ...
}

Could be written as:

struct Parent {}

impl Parent {

   fn some_op(&self) {}
}

struct Child {
    parent: Parent 
}

impl Child {
    fn some_op(&self) {
       self.parent.some_op(&self)
   }
}
@cathaysia cathaysia added the A-lint Area: New lints label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant