Skip to content

needless_return doesn't trigger properly with anyhow! macro #10051

@ComputerDruid

Description

@ComputerDruid

Summary

needless_return fails to trigger on return Err(anyhow::anyhow!("failed to do things"));

This leads to weird situations where clippy will clean up one needless return but not another, for example https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b196ba185e8485558293b61fce4f7d7c . I saw this a fair bit when running clippy --fix across the fuchsia codebase, http://fxrev.dev/c/fuchsia/+/773265/2..3 shows me manually fixing up those cases.

Lint Name

needless_return

Reproducer

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

pub fn example() -> Result<(), anyhow::Error> {
    return Err(anyhow::anyhow!("failed to do things"));
}

I expected to see this happen:

warning: unneeded `return` statement
 --> src/lib.rs:2:5
  |
2 |     return Err("failed to do things");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
  = note: `#[warn(clippy::needless_return)]` on by default
  = help: remove `return`

Instead, this happened (no errors):

    Checking playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.40s

Version

rustc 1.67.0-nightly (822f8c22f 2022-11-02)
binary: rustc
commit-hash: 822f8c22f540b12f296d844ad5bf39aaa47bfeb4
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions