Skip to content

mismatched_lifetime_syntaxes should treat &T and ContainsLifetime as equivalent #145312

@stevenroose

Description

@stevenroose

Problem

warning: hiding a lifetime that's elided elsewhere is confusing
   --> lib/src/connectors.rs:175:20
    |
175 |     pub fn connectors(&self) -> ConnectorIter {
    |                       ^^^^^     ------------- the same lifetime is hidden here
    |                       |
    |                       the lifetime is elided here
    |
    = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
help: use `'_` for type paths
    |
175 |     pub fn connectors(&self) -> ConnectorIter<'_> {
    |                                              ++++

I just tried to upgrade to Rust v1.89.0 and suddenly I see various of these compiler warnings.

The argument it seems to make is that "hiding" a lifetime is not the same as "eliding" it. However, in both cases, the lifetime is hidden.

Both &self and ConnectorIter hide the lifetime, implicitly eliding all possible lifetimes it could have.

The warning suggests to explicitly elide the lifetime on the return value, ConnectorIter<'_>, but it doesn't recommend also explicitly eliding it on self: f.e. &'_ self. It seems that this lint is quite broken then.

I can agree that having &'a self and a hidden lifetime on the return type is confusing and could be linted, but in the example I gave, both ref and return type hide (and hence elide) the lifetime, which should be no problem.

Steps

pub struct HasRef<'a>(pub &'a str);

pub fn return_has_ref(_v: &str) -> HasRef { todo!() }

Possible Solution(s)

Consider &self as a hidden lifetime just as hiding the lifetime of a struct. Or disable mismatched_lifetime_syntaxes by default.

Notes

No response

Version

1.89.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions