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

unit_cmp: special handling of assert_eq!(Option.unwrap(), ()); #4661

Open
matthiaskrgr opened this issue Oct 13, 2019 · 5 comments
Open

unit_cmp: special handling of assert_eq!(Option.unwrap(), ()); #4661

matthiaskrgr opened this issue Oct 13, 2019 · 5 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@matthiaskrgr
Copy link
Member

pub fn main() {
    let x: Option<()> = Some(());
    assert_eq!(x.unwrap(), ());
}

Clippy warns here ``assert_eq of unit values detected. This will always succeed, however this code will actually check and panic if x is None.

Can we suggest to replace it with
assert!(x.is_some()) in this case?

Found in cargo codebase of cargo.

clippy 0.0.212 (5cb9833 2019-10-08)

@matthiaskrgr matthiaskrgr added the L-suggestion Lint: Improving, adding or fixing lint suggestions label Oct 13, 2019
@flip1995 flip1995 added the C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages label Oct 14, 2019
@devonhollowood
Copy link
Contributor

I've been working on this a bit, but I'm having trouble getting the left and right sides of the assert_eq!(). What's the best way to get the Expr for opt.unwrap() in assert_eq!(opt.unwrap(), ())? The left binding in UnitCmp::check_expr() (here) seems to be bound to something different, I'm assuming due to the macro expansion.

@flip1995
Copy link
Member

flip1995 commented Oct 22, 2019

There will be a utils function for this (hopefully) soon, see #4694

@hellow554
Copy link
Contributor

hellow554 commented Nov 12, 2019

Clippy warns here assert_eq of unit values detected. This will always succeed, however this code will actually check and panic if x is None.

Yes and no. The assert_eq! will always succeed, because the unwrap panics and not the assert_eq in case of None. So clippy is right here.

Can we suggest to replace it with
assert!(x.is_some()) in this case?

Would be a smart play :)

@martin-t
Copy link
Contributor

martin-t commented Feb 5, 2022

Would it make sense to allow cases where either left or right is explicitly ()?

My usecase is testing that a macro i wrote always evaluates to () as part of testing that it's usable in expression position - e.g. assert_eq!(my_macro!(), ());. It'll fail at compile time if it's wrong so it always succeeds but i don't think there's an easier way to test this. If there is, then the lint should suggest it instead.

@flip1995
Copy link
Member

flip1995 commented Feb 7, 2022

Maybe let _: () = my_macro!(); is the better alternative here. assert! is not really meant for compile time checks IMO.

OTOH, assert maybe conveys the intention of this line better.

For .unwrap special casing it to is_some might make sense too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

5 participants