rust-lang / rust-lang/rust-clippy
return_self_not_must_use strange interaction with trait methods
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Should this run on traits at all?
Currently whether the trait is public or not affects the warning.
Lint Name
return_self_not_must_use
Reproducer
I tried this code:
pub trait Trait1 {
fn set_config1(self) -> Self; // always warns
}
trait Trait2 {
fn set_config2(self) -> Self; // never warns
}
#[must_use]
struct Builder1;
impl Trait1 for Builder1 {
fn set_config1(self) -> Self {
Builder1
}
}
impl Trait2 for Builder1 {
fn set_config2(self) -> Self {
Builder1
}
}
struct Builder2;
impl Trait1 for Builder2 {
fn set_config1(self) -> Self {
Builder2
}
}
impl Trait2 for Builder2 {
fn set_config2(self) -> Self {
Builder2
}
}
fn main() {
Builder1.set_config1();
Builder1.set_config2();
Builder2.set_config1();
Builder2.set_config2();
}
I saw this happen:
warning: missing `#[must_use]` attribute on a method returning `Self`
--> src/main.rs:2:5
|
2 | fn set_config1(self) -> Self; // always warns
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(clippy::return_self_not_must_use)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#return_self_not_must_use
warning: unused `Builder1` that must be used
--> src/main.rs:40:5
|
40 | Builder1.set_config1();
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_must_use)]` on by default
warning: unused `Builder1` that must be used
--> src/main.rs:41:5
|
41 | Builder1.set_config2();
| ^^^^^^^^^^^^^^^^^^^^^^^
I expected to see this happen:
I'm not really sure if we should be warning on traits at all, but we should be consistent about it regardless of if the trait is public or not.
Version
rustc 1.59.0-nightly (399ba6bb3 2022-01-03)
binary: rustc
commit-hash: 399ba6bb377ce02224b57c4d6e127e160fa76b34
commit-date: 2022-01-03
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0
Additional Labels
No response
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.
Research direction
Start by locating the implementation and tests for the return_self_not_must_use lint, then run the Rust reproducer to compare public and private traits. Determine the intended behavior for trait methods, make handling consistent, and add regression coverage for the shown combinations of traits and builders.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100