-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add lint for lifetimes or generic types not required by trait / struct / enum #18549
Comments
For the record, here's what 0.12 would say about this code: <anon>:7:9: 7:26 error: unable to infer enough type information to locate the impl of the trait `core::kinds::Sized` for the type `<generic #1>`; type annotations required
<anon>:7 self.parse_item();
^~~~~~~~~~~~~~~~~
error: aborting due to previous error |
@shepmaster Could you post a bit more code to show how |
@sfackler Here's the full shebang. Specifically the call to Your comment makes me think that I have a large conceptual mismatch. I was under the impression that only functions that actively used the type parameter would be specialized (in this example, nothing would happen). Is it more correct to think the entire impl that gets specialized? I would have expected that the version of I think that the error message part about |
Everything in the impl block is parameterized. I've actually never seen type parameters added to impl blocks themselves that aren't part of the trait or type definition. It's far more common to parameterize the individual methods that need it. |
Moving the type parameters to each function worked well to get me unstuck, thanks!. However, is there a way of avoiding so much repetition? That was my original goal of moving it up to the // This is easier for me to understand
impl<T : PartialEq> Foo {
fn method1(&self, a: T);
fn method2(&self, a: T);
fn method3(&self, a: T);
fn method4(&self, a: T);
fn method5(&self, a: T);
}
// But it needs to be this way?
impl Foo {
fn method1<T : PartialEq>(&self, a: T);
fn method2<T : PartialEq>(&self, a: T);
fn method3<T : PartialEq>(&self, a: T);
fn method4<T : PartialEq>(&self, a: T);
fn method5<T : PartialEq>(&self, a: T);
} |
@sfackler I've seen this same problem occur a few times now on Stack Overflow questions. I propose we transmute this issue to become something like "Add lint for lifetimes or generic types not required by trait / struct / enum" |
👍 |
_
" when type parameter is used
Triage: still gives T is not constrained error, but nothing about the lifetimes. |
Since new lints have a big impact on users of rustc, the policy is that they should go through the RFC process like other user-facing changes. As such, I'm going to give this one a close, but if anyone comes across this ticket and wants this lint, consider adding it to clippy and/or writing up an RFC. Thanks! |
This code
Should generate two lint warnings on the
impl
line'a
is not needed by the struct.T
is not needed by the struct.This should apply similarly to trait impls where the lifetime / types are not needed by either the trait or struct.
Update as of
rustc 1.0.0-dev (82cc57b6a 2015-01-13 09:54:08 -0800)
Type parameters now have a warning like "the type parameter
T
is not constrained by the impl trait, self type, or predicates", so that part is good. I think we still want something for lifetimes.Original report below
This code (playpen link)
Fails to compile:
Removing the
<Z>
allows the code to compile. This is a reduced example from my full app, where the type parameter is actually used. 😀The text was updated successfully, but these errors were encountered: