-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Return early if the method is a default trait impl for boxed_local
#4833
Conversation
If the parent node is a trait definitions, this lint shouldn't trigger
trait DefaultTraitImplTest { | ||
fn default_impl(self: Box<Self>) -> u32 { | ||
5 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this enough to cover this fix? There might be some more niche cases which I hadn't encountered but which should be tested as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a more general note: Why do you take a box instead of just a reference?
trait Trait {
fn foo(&self) {}
}
should also work.
I don't know, why I always forget about this: #4351 (comment) The fix is not to allow this in traits or allow the lint for unsized types, but to add a suggestion that this should be written as Are you willing to add such a suggestion in this PR or should this just be tracked in an issue (#4740) until someone else decides to add a suggestion? It would definitely be great if you could add to the lint documentation, that this should be rewritten to |
The case which led me to open an issue was something akin to the following: trait Trait {
fn foo(self: Box<Self>) -> Result<Box<dyn Trait>, ()> {
Err(())
}
}
struct Foo {
// some data
}
impl Trait for Foo {
fn foo(self: Box<Self>) -> Result<Box<dyn Trait>, ()> {
// actually do something with "self"
}
} where my intention was to consume the value and receive a new boxed trait object. Due to having a non negligible count of structs which needed to In that situation, having this lint suggest using a reference instead of a boxed trait object would still not align with my intentions. I've later managed to refactor all of that code so it doesn't use dynamic dispatch at all. |
I am willing to give this a try and change the lint documentation to suggest changing to a This can be implemented to suggest changing |
Ah I see now. I think the best way to do this would be to disable this lint for |
Adding this to the documentation is as easy as adding a bad+good example here rust-clippy/clippy_lints/src/escape.rs Lines 29 to 36 in b4f1769
Yes the suggestion should be to just remove the box, if the type is sized and change it to |
add check for a by value self argument in default trait method implementation add an example for when replacing Box<T> with &T would suffice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I started the review, but never pressed "Submit review" 😄
We have to look at every argument of default trait impls, not only self. But only for arguments that are sized. See is_sized
for the documentation.
/// println!("{}", *x); | ||
/// | ||
/// fn foo(arg: Box<[u32]>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That will test-fail. Please leave the foo(*x);
and rename this to bar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
Thanks for the linked docs, I'll get to changing the code in a few days as Uni is heating up at the moment. Should've realized sooner that skipping linting the whole method will not suffice. |
☔ The latest upstream changes (presumably #4938) made this pull request unmergeable. Please resolve the merge conflicts. |
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. |
fixes #4804
changelog: check for a default trait impl and a corresponding test case