Skip to content

Garbage clippy::expect_fun_call lint for result.expect(if b { "" } else { "" }) #15056

Open
@nabijaczleweli

Description

@nabijaczleweli

Summary

warning: function call inside of `expect`
    --> src\ops\mod.rs:1353:39
     |
1353 |               fs::remove_dir_all(req_p).expect(if symlink {
     |  _______________________________________^
1354 | |                 "Failed to remove requested symlink"
1355 | |             } else {
1356 | |                 "Failed to remove requested directory"
1357 | |             });
     | |______________^
     |
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
     = note: `#[warn(clippy::expect_fun_call)]` on by default
help: try
     |
1353 ~             fs::remove_dir_all(req_p).unwrap_or_else(|_| { panic!("{}", if symlink {
1354 +                 "Failed to remove requested symlink"
1355 +             } else {
1356 +                 "Failed to remove requested directory"
1357 ~             }.to_string()) });
     |

Reproducer

I tried this code:

fn main() {
    let b = false;
    std::fs::read("/ENOENT").expect(if b {
        "a"
    } else {
        "b"
    });
}

I expected to see this happen: nothing

Instead, this happened: I got the obvious garbage from above

The three-and-a-half most obvious issues with this:

  1. this is not a function call
  2. this .to_string()s (string literals!) into a {} format
  3. this isn't actually equivalent, since it destroys the error information from the expect

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-pc-windows-gnu
release: 1.85.1
LLVM version: 19.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions