rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`assert_is_empty` could catch `BTreeMap`

Open hashcatHitman opened this issue 1 month ago • 4 comments

Summary

Of all the collections in std::collections, assert_is_empty only fires on Vec. Here is how it can fire on std::collections::BTreeMap.

To make it more obvious which traits are the bare minimum required on a given type, I introduce this trait to allow me to fill collections generically:

    trait ElementExists {
        const ELEMENT: Self;
    }

...and also leave comments explaining which traits are required.

All cases provided will intentionally fail the assertion, since that's the whole point of the lint. The first case in each set will be the case that the lint should fire on, to show the difference in trait bounds (as some collections have innate bounds).

Parent tracking issue: https://github.com/rust-lang/rust-clippy/issues/17660

Lint Name

assert_is_empty

Reproducer

Clippy should suggest replacing the assert shown in *_normal with the assert shown in *_eq or *_matches if the type(s) in the collection meet the new trait bounds.

    // T: Ord is implied by the requirements of BTreeMap
    fn btreemap_normal<T: ElementExists + Ord, U: ElementExists>() {
        let mut btreemap = BTreeMap::new();
        drop(btreemap.insert(T::ELEMENT, U::ELEMENT));
        assert!(btreemap.is_empty());
    }

    fn btreemap_eq<
        T: ElementExists + Ord + Debug, // T now needs Debug
        U: ElementExists + PartialEq + Debug, // U now needs PartialEq and Debug
    >() {
        let mut btreemap = BTreeMap::new();
        drop(btreemap.insert(T::ELEMENT, U::ELEMENT));
        assert_eq!(btreemap, BTreeMap::new());
    }

Version

rustc 1.100.0-nightly (fb6531d55 2026-08-23)
binary: rustc
commit-hash: fb6531d550e0075b9eb9a51464f404805eec87d9
commit-date: 2026-08-23
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.0

hashcatHitman avatar Oct 27 '26 21:10 hashcatHitman

@CommanderStorm @hashcatHitman can I work on this issue? I see there is a PR active for all the sub issues but hasn't been really changed for a month now..

CoDe-ReDz avatar Oct 28 '26 00:10 CoDe-ReDz

Hi @CoDe-ReDz! Neither of us are rust-clippy maintainers, so I don't think we can say for sure, but here's the suggestion I am making. Go ahead and claim this issue now with @rustbot claim.

I think we're likely going to ask that PR to reduce its scope (which is part of why we broke it up into smaller issues), but maybe try talking to the author before doing anything. They haven't been super active lately, so if you don't hear anything in 2 weeks, I think you can move forward.

hashcatHitman avatar Oct 28 '26 00:10 hashcatHitman

cc @rust-lang/clippy for a sanity check on what I'm suggesting.

hashcatHitman avatar Oct 28 '26 00:10 hashcatHitman

@rustbot claim

CoDe-ReDz avatar Oct 28 '26 00:10 CoDe-ReDz