Open
Description
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.