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

Suggestion does not compile: use self with when implementing From conversion for the Box<dyn Trait>. #4305

Closed
alekseysidorov opened this issue Jul 29, 2019 · 2 comments · Fixed by #6179
Labels
A-documentation Area: Adding or improving documentation C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@alekseysidorov
Copy link

clippy 0.0.212 (082cfa7 2019-06-07)

Clippy incorrectly suggests to use Self when implementing From conversion for the Box<dyn Trait> type.

#![deny(clippy::pedantic)]

trait Foo: 'static {}

struct Bar;

impl Foo for Bar {}

impl<T: Foo> From<T> for Box<dyn Foo> {
    fn from(t: T) -> Self {
        Box::new(t)
    }
}

Results the following error:

unnecessary structure name repetition
#[deny(clippy::use_self)] implied by #[deny(clippy::pedantic)]
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_selfclippy(use_self)
lib.rs(1, 9): lint level defined here

But code with this suggestion does not compile:

#![deny(clippy::pedantic)]

trait Foo: 'static {}

struct Bar;

impl Foo for Bar {}

impl<T: Foo> From<T> for Box<dyn Foo> {
    fn from(t: T) -> Self {
        Self::new(t)
    }
}
no function or associated item named `new` found for type `std::boxed::Box<(dyn Foo + 'static)>` in the current scope
the method `new` exists but the following trait bounds were not satisfied:
`dyn Foo : std::marker::Sized`
@flip1995 flip1995 added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jul 29, 2019
@flip1995
Copy link
Member

flip1995 commented Jul 29, 2019

This needs a check if the dyn Trait is Sized. More precisely: This situation, where the method of a type must have a bound, but the type itself not, can only happen with the special ?Sized bound. This means, on functions, this lint has to check if the argument needs the Sized bound and if it is satisfied.

This may not be that easy to implement, so I would suggest to add this to the Known Problems section of the lint, until someone gets to fix this.


good-first-issue: Just document this.
E-medium: Fixing this issue.

@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. good-first-issue These issues are a good way to get started with Clippy A-documentation Area: Adding or improving documentation labels Jul 29, 2019
@ghost
Copy link

ghost commented Jul 30, 2019

I don't think it's a problem with the bounds. This looks like another instance of use_self not checking generics. In the example, it's actually Box::<T>::new(t) and in the suggestion it's Box::<dyn Foo>::new(t).

tnielens added a commit to tnielens/rust-clippy that referenced this issue Apr 26, 2020
tnielens added a commit to tnielens/rust-clippy that referenced this issue Apr 26, 2020
tnielens added a commit to tnielens/rust-clippy that referenced this issue Apr 26, 2020
bors added a commit that referenced this issue Oct 13, 2020
…itive, r=<try>

rework use_self impl based on ty::Ty comparison #3410

`use_self` lint refactoring. As suggested by `@eddyb` , implement the lint based on `ty::Ty` comparison instead of `hir::Path` checks.

This PR introduces negative regressions. The cases were covered in the previous implementation. See the test file.

It fixes #3410, #2843, #3859, #4734 and #4140. Should fix #4143 (example doesn't compile). #4305 false positive seems also fixed but the uitest fails to compile the unmodified test case in `use_self.rs.fixed`. Implements #5078.
Generally the implementation checks are done at mir level and offers higher guarantees against false positives. It is probably fixing other unreported false positives.

changelog: fix use_self generics false positives
bors added a commit that referenced this issue Jan 19, 2021
Rework use_self impl based on ty::Ty comparison #3410 | Take 2

This builds on top of #5531

I already reviewed and approved the commits by `@montrivo.` So only the review of my commits should be necessary.

I would also appreciate your review `@montrivo,` since you are familiar with the challenges here.

Fixes #3410 and Fixes #4143 (same problem)
Fixes #2843
Fixes #3859
Fixes #4734 and fixes #6221
Fixes #4305
Fixes #5078 (even at expression level now 🎉)
Fixes #3881 and Fixes #4887 (same problem)
Fixes #3909

Not yet: #4140 (test added)

All the credit for the fixes goes to `@montrivo.` I only refactored and copy and pasted his code.

changelog: rewrite [`use_self`] lint and fix multiple (8) FPs. One to go.
bors added a commit that referenced this issue Feb 10, 2021
Rework use_self impl based on ty::Ty comparison #3410 | Take 2

This builds on top of #5531

I already reviewed and approved the commits by `@montrivo.` So only the review of my commits should be necessary.

I would also appreciate your review `@montrivo,` since you are familiar with the challenges here.

Fixes #3410 and Fixes #4143 (same problem)
Fixes #2843
Fixes #3859
Fixes #4734 and fixes #6221
Fixes #4305
Fixes #5078 (even at expression level now 🎉)
Fixes #3881 and Fixes #4887 (same problem)
Fixes #3909

Not yet: #4140 (test added)

All the credit for the fixes goes to `@montrivo.` I only refactored and copy and pasted his code.

changelog: rewrite [`use_self`] lint and fix multiple (8) FPs. One to go.
@bors bors closed this as completed in 605e9ba Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documentation Area: Adding or improving documentation C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants