rust-lang / rust-lang/rust-clippy
`use_self` does not notice Self with lifetimes
Open
@Ethiraric is already working on this.
Since Mar 1, 2024.
C-bug
I-false-negative
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Types where a lifetime is involved don't get noticed by use_self. Generics are no issue. Cow<'a, B> with both lifetime and generic are also not noticed.
clippy::return_self_not_must_use finds these cases.
Lint Name
clippy::use_self
Reproducer
I tried this code:
#![warn(clippy::use_self)]
use std::borrow::Cow;
trait Foo {
type Item;
fn bar(&self) -> Self::Item;
}
#[derive(Clone)]
struct Data<'a> {
value: &'a str,
}
impl<'a> Data<'a> {
pub const fn new(value: &'a str) -> Data<'a> {
Data {value}
}
}
impl<'a> Foo for Data<'a> {
type Item = Data<'a>;
fn bar(&self) -> Self::Item {
self.clone()
}
}
impl<'a, T: ToOwned> Foo for Cow<'a, T> {
type Item = Cow<'a, T>;
fn bar(&self) -> Self::Item {
self.clone()
}
}
struct Something<T> {
value: T,
}
impl<T> Something<T> {
pub const fn new(value: T) -> Something<T> {
Something { value }
}
}
impl<T: Clone> Foo for Something<T> {
type Item = Something<T>;
fn bar(&self) -> Self::Item {
Something { value: self.value.clone() }
}
}
fn main() {
let data = Data::new("Hello");
let first = data.bar().value;
let something = Something::new("world");
let second = something.bar().value;
println!("{first}, {second}!");
}
I expected to see this happen:
Data<'a> should be replaced by Self for type Item = , Data {…} and -> Data<'a>.
Instead, this happened:
Only Something<T> is noticed.
Version
Both current stable and current nightly.
$ rustc -Vv
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6
rustc 1.78.0-nightly (ef324565d 2024-02-27)
binary: rustc
commit-hash: ef324565d071c6d7e2477a195648549e33d6a465
commit-date: 2024-02-27
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.