Skip to content

Impl trait lifetime mismatch #6434

Open
@camsteffen

Description

@camsteffen

What it does

Detect impl items where the lifetimes do not match the trait item definition. I believe this can be more specifically described as having "stricter" lifetime requirements than the trait item definition.

Categories (optional)

  • Kind: style

What is the advantage of the recommended code over the original code

Inconsistency between the trait and impl is confusing and less self-documenting. Also it may lead to preventable lifetime errors. Also, since the lifetime is already named and available in the impl block, it seems appropriate to use it wherever applicable.

Drawbacks

It is more convenient to elide lifetimes if the particular implementation allows it.

Example

trait Foo<'a> {
    fn hi(a: &'a str, b: &'a str);
}

struct Bar;
struct Baz;

impl<'a> Foo<'a> for Bar {
    // here the trait lifetime is unused
    fn hi(_: &str, _: &str) {
        unimplemented!()
    }
}

impl<'a> Foo<'a> for Baz {
    // here different lifetime semantics are applied
    fn hi<'b>(_: &'b str, _: &'b str) {
        unimplemented!()
    }
}

Could be written as:

impl<'a> Foo<'a> for Bar {
    fn hi(_: &'a str, _: &'a str) {
        unimplemented!()
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.T-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions