Skip to content
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

FP: unused_self triggers if only the lifetime of self is used #4928

Open
FaultyRAM opened this issue Dec 21, 2019 · 0 comments
Open

FP: unused_self triggers if only the lifetime of self is used #4928

FaultyRAM opened this issue Dec 21, 2019 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@FaultyRAM
Copy link

FaultyRAM commented Dec 21, 2019

Tested on clippy shipping with Rust 1.40.0:

C:\>cargo clippy -V
clippy 0.0.212 (c8e3cfbd 2019-10-28)

Here's a contrived example that triggers the unused_self lint and fails to build:

#![deny(clippy::unused_self)]

use std::marker::PhantomData;

pub struct A;

pub struct B<'a>(PhantomData<&'a A>);

impl A {
    pub fn b(&self) -> B<'_> {
        B(PhantomData)
    }
}

This is actually a valid pattern. In FFI code for instance, A might be responsible for initialising and freeing some part of a library, and the function b is used to create objects that belong to the library, and therefore can't outlive it. So A can just be a ZST while B uses PhantomData<&'a A> to convey ownership semantics.

If we rewrite the example so it directly uses self it works:

#![deny(clippy::unused_self)]

pub struct A;

pub struct B<'a>(&'a A);

impl A {
    pub fn b(&self) -> B<'_> {
        B(self)
    }
}

But we don't want to hold pointers to A, we just want to express that instances of B belong to an instance of A.

tl;dr unused_self doesn't catch edge cases where self is used without actually being used.

@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-bug Category: Clippy is not doing the correct thing labels Dec 21, 2019
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

3 participants