Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertions_on_result_states leads to less clear tests #9263

Closed
Nemo157 opened this issue Jul 30, 2022 · 4 comments · Fixed by #9273
Closed

assertions_on_result_states leads to less clear tests #9263

Nemo157 opened this issue Jul 30, 2022 · 4 comments · Fixed by #9273
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Nemo157
Copy link
Member

Nemo157 commented Jul 30, 2022

Summary

IMO using assert!(foo().is_err()) is clearer within a test to imply that the assertion is the point of the test rather than foo().unwrap_err().

Lint Name

assertions_on_result_states

Reproducer

I tried this code:

    #[test]
    fn error() {
        struct Foo;

        impl stylish::Display for Foo {
            fn fmt(&self, _: &mut stylish::Formatter<'_>) -> std::fmt::Result {
                Err(std::fmt::Error)
            }
        }

        let mut s = Vec::<u8>::new();
        let mut writer = stylish::io::Plain::new(&mut s);
        assert!(stylish::writeln!(writer, "{:s}", Foo).is_err());
    }

I saw this happen:

error: called `assert!` with `Result::is_err`
Error:   --> tests/tests.rs:70:9
   |
70 |         assert!(stylish::writeln!(writer, "{:s}", Foo).is_err());
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `stylish::writeln!(writer, "{:s}", Foo).unwrap_err()`
   |
   = note: `-D clippy::assertions-on-result-states` implied by `-D warnings`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_result_states

I expected to see this happen:

Version

info: syncing channel updates for 'nightly-x86_64-unknown-linux-gnu'
info: latest update on 2022-07-30, rust version 1.64.0-nightly (3924dac7b 2022-07-29)

Additional Labels

No response

@Nemo157 Nemo157 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 30, 2022
@Nemo157
Copy link
Member Author

Nemo157 commented Jul 30, 2022

Alternatively, in this sort of situation maybe it'd be better to recommend .expect_err(..).

@Jarcho
Copy link
Contributor

Jarcho commented Jul 30, 2022

Assuming stylish::writeln has a unit type for Result::Ok then the reasoning for the lint doesn't apply.

It might also be worth putting the lint is restriction as well. assert will print the failing expression which might be more useful than printing the debug output of the value.

@Nemo157
Copy link
Member Author

Nemo157 commented Jul 30, 2022

Assuming stylish::writeln has a unit type for Result::Ok

It is indeed, Result<(), std::fmt::Error>.

For comparison here's the two outputs if the test fails:

assert!(stylish::writeln!(writer, "{:s}", Foo).is_err());
// 'assertion failed: stylish::writeln!(writer, \"{:s}\", Foo).is_err()', tests/tests.rs:71:9

stylish::writeln!(writer, "{:s}", Foo).unwrap_err();
// 'called `Result::unwrap_err()` on an `Ok` value: ()', tests/tests.rs:71:48

@tabokie
Copy link
Contributor

tabokie commented Aug 1, 2022

Disabling it here: #9273

@bors bors closed this as completed in a5a6c95 Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants