Skip to content

Add lint for lifetimes or generic types not required by trait / struct / enum #18549

Closed
@shepmaster

Description

@shepmaster

This code

struct Alpha;
impl<'a, T> Alpha {
  fn yeah(arg: &'a T) {}
}

Should generate two lint warnings on the impl line

  1. The lifetime 'a is not needed by the struct.
  2. The type 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

$ rustc --version=verbose
rustc 0.13.0-dev (3327ecca4 2014-11-01 22:41:48 +0000)
binary: rustc
commit-hash: 3327ecca422046699315122345c6c050ab73804b
commit-date: 2014-11-01 22:41:48 +0000
host: x86_64-apple-darwin
release: 0.13.0-dev

This code (playpen link)

struct Parser;

impl<Z> Parser {
    fn parse_item(&self) {}

    fn parse_items(&self) {
        self.parse_item();
    }
}

fn main() {}

Fails to compile:

$ rustc --test repro.rs
repro.rs:7:9: 7:26 error: unable to infer enough type information about `_`; type annotations required
repro.rs:7         self.parse_item();
                   ^~~~~~~~~~~~~~~~~

Removing the <Z> allows the code to compile. This is a reduced example from my full app, where the type parameter is actually used. 😀

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions