-
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
False positive with use_self and generics #3410
Comments
Here is a another example that doesn't involve traits.
The problem is that the code checks if two paths refer to the same type by comparing the I couldn't find a way to get the actual type including type parameters for a general path. Maybe someone has a suggestion. I can see how to handle this for types in function signatures though. I'll submit a PR for that later. |
3423: Fix `use_self` false positive r=flip1995 a=mikerite This fixes the first error reported in issue #3410. Co-authored-by: Michael Wright <[email protected]>
Not involving generics but another false positive: #![warn(clippy::use_self)]
#![allow(dead_code)]
#[derive(Clone)]
struct S;
impl S {
fn foo() {
impl Copy for S {}
}
}
fn main() {}
With generics, the error also happens with associated types: #![warn(clippy::use_self)]
#![allow(dead_code)]
struct S<T1, T2> {
t1: T1,
t2: T2,
}
impl<T1: Copy, T2: Copy> Iterator for S<T1, T2> {
type Item = S<T2, T1>;
fn next(&mut self) -> Option<Self::Item> {
Some(S {
t1: self.t2,
t2: self.t1,
})
}
}
fn main() {}
(Both warnings are wrong.) |
I don't know how to do that but there is a pull request #1997. There was no activity for a long time but there is a bit of discussion. |
IMO clippy should just use It's unnecessary to compare |
I started working on this based on the recommendation and example committed by @eddyb . |
…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
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.
I think this is the same problem in a slightly different shape:
The false positive gets triggered twice: once somewhere in the macro (match by accident) and once in the declaration. |
I think that one's different. You could say E.g. even in the body of an impl for |
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.
As far as I can tell, the
use_self
is incorrectly ignoring generic parameters when implementing traits for some types with generics.Here's my example:
This, as far as I can tell, uses
Self
as much as possible. But with-W clippy::pedantic
, clippy warns of three more places where it thinks I should be able to useSelf
:There are two mistakes: it's treating
Vec<IntermediatePhase>
asVec<FinalPhase>
, and it's treatingVec<FinalPhase>
asVec<T>
.Hope the example is minimal enough - my original trigger was with a custom trait like this, and it seemed hard to come up with any other reasonable use case which caused this situation.
Clippy version:
Related to #1993.
The text was updated successfully, but these errors were encountered: