Skip to content

False positive for redundant closure lint with track_caller #12199

@Mark-Simulacrum

Description

@Mark-Simulacrum

Clippy suggests changing working to broken, which changes behavior of the add_location function since it's now emitting a location in libcore rather than in my code. This probably deserves a comment in the code anyway so humans don't clean things up, but I don't want to write a comment and an allow.

Should be detectable if the function being called inside the closure is marked track_caller.

#[track_caller]
fn add_location(s: u32) -> String {
    format!("{} @ {:?}", s, std::panic::Location::caller())
}

fn broken(r: Result<(), u32>) -> Result<(), String> {
    // broken code, erases the caller location
    r.map_err(add_location)
}

fn working(r: Result<(), u32>) -> Result<(), String> {
    r.map_err(|e| add_location(e))
}

fn main() {
    assert!(dbg!(broken(Err(0)).unwrap_err()).contains("core/src/ops/function.rs"));
    assert!(dbg!(working(Err(0)).unwrap_err()).contains("src/main.rs"));
}

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions