Skip to content

Lint: comparing two expressions of unit type #4481

Closed
@timotree3

Description

@timotree3

I recently wrote a test like this

assert_eq!(&my_complicated_function().sort(), &[4, 3, 7, 1].sort()) // works!

but much to my surprise, the test kept passing even if I changed the literal

assert_eq!(&my_complicated_function().sort(), &[4, 3].sort()) // works!

at this point I realized that what I wrote was as good as this useless comparison

assert_eq!((), ())

In order to catch people who try to do things like this, I suggest that we lint comparing two expressions of unit type, since the result will always be true and it would be more straightforward just to write two lines for each expression e.g.

my_complicated_function();
[4, 3, 7, 1];

In addition to the canonical Unit Type (), I suggest that we lint for expressions of any user-created unit type such as:

struct UnitStruct;
enum UnitEnum {
    OnlyVariant,
}
// and maybe
enum TechnichallyUnitStruct {
    unit_field: (),
}
enum TechnichallyUnitEnum {
    UnitVariant(UnitStruct, ()),
    NeverVariant(!),
}
// etc...

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions