Skip to content

Fix for assertions_on_result_states incorrectly changes return type #9450

Closed
@glittershark

Description

@glittershark

Summary

assert!(res.is_err()) always returns (), but res.unwrap_err() returns the err type itself - this can cause auto-fixed code to fail to compile if there's a missing semicolon at the end of a test, for example.

Reproducer

I tried this code:

#[test]
fn my_test() {
    let res: Result<i32, i32> = Ok(1);
    assert!(res.is_err())
}

I expected to see this happen:

#[test]
fn my_test() {
    let res: Result<i32, i32> = Ok(1);
    res.unwrap_err();
}

Instead, this happened:

#[test]
fn my_test() {
    let res: Result<i32, i32> = Ok(1);
    res.unwrap_err()
}

causing a type error

Version

❯ rustc -Vv
rustc 1.64.0-nightly (fe3342816 2022-08-01)
binary: rustc
commit-hash: fe3342816a282949f014caa05ea2e669ff9d3d3c
commit-date: 2022-08-01
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

@rustbot label +I-suggestion-causes-err

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions