rust-lang / rust-lang/rust-clippy

`return_self_not_must_use` is reported for types that already have a `#[must_use]` attribute

Open
#8,279 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy reports return_self_must_use for traits in a generic context, even if the return type already has #[must_use].

Lint Name

return_self_must_use

Reproducer

I tried this code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c6a72eb40b979421206de10daf27d85d

use std::fmt::Display;
use std::error::Error;
pub type BoxError = Box<dyn Error + Send + Sync>;
pub type MyResult<T> = Result<T, BoxError>;

pub trait MyResultExt<T, E> {
    fn log_err(self, msg: &str) -> Self
    where
        E: Display;
}

impl<T> MyResultExt<T, BoxError> for MyResult<T> {
    fn log_err(self, _: &str) -> Self {
        self
    }
}

fn main() {
    Ok(()).log_err("oops");
}

I saw this happen:

warning: missing `#[must_use]` attribute on a method returning `Self`
 --> src/main.rs:7:5
  |
7 | /     fn log_err(self, msg: &str) -> Self
8 | |     where
9 | |         E: Display;
  | |___________________^
  |
  = 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 `std::result::Result` that must be used
  --> src/main.rs:19:5
   |
19 |     Ok(()).log_err("oops");
   |     ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_must_use)]` on by default
   = note: this `Result` may be an `Err` variant, which should be handled

I expected to see this happen: No return_self_not_must_use warning, because the compiler already reports unused_must_use for this code.

Version
1.60.0-nightly (2022-01-13 22e491ac7ed454d34669)
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation and existing tests for the return_self_must_use lint, then reproduce the supplied generic-trait example with the Rust playground configuration. Trace how the lint determines whether a return type is already #[must_use]. Done means the redundant lint warning is absent for this case while the lint still reports applicable non-must-use returns.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.