Add ASSERTIONS_ON_COLLECTION_EMPTINESS lint#17320
Conversation
Suggests replacing �ssert!(x.is_empty()) and �ssert!(!x.is_empty()) with �ssert_eq!(x, []) / �ssert_ne!(x, []) (or �ssert_eq!(x, "") / �ssert_ne!(x, "") for String types), so the collection contents are shown on failure.
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @dswij (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 0470b43
This comment will be updated if you push new changes |
|
r? clippy |
There was a problem hiding this comment.
Community review: assert_eq! and assert_ne! will fail to compile unless the collection elements implement at least PartialEq and Debug. Based on my reading of the code, the collection element typed is not checked at all - can you confirm that for elements that cannot be compared with assert_eq! (or assert_ne!) there isn't an invalid suggestion? Maybe add a test to confirm?
|
Thanks for pushing this forward. I had already put up a deeper implementation for the same lint in #17149, so I took a pass through this PR and the review comments to see whether there was anything useful to carry over there. The main useful bit I found here is the The two PRs are also at fairly different depths. This PR is around 250 lines of changes, while #17149 is currently around 755 inserted lines. That extra size is mostly in type checks, source-preserving suggestions, docs, and UI coverage rather than just implementation bulk. A few gaps in this implementation are already covered over there:
assert_eq!(items, [] as [T; 0]);
assert_ne!(items, [] as [T; 0]);That avoids type inference failures from a bare
I also prefer the lint docs and diagnostic wording in #17149. The docs there spell out the actual failure mode this lint is aimed at: in CI or another remote test service, an The diagnostic wording in #17149 is also more precise about both the checked state and the reason for the replacement. This PR currently describes the predicate as an "empty collection check" / "non-empty collection check" and uses a generic "replace with" help. That is understandable, but it leaves the reader to infer why the replacement is preferred and it gets a little awkward for the negated case. For example, this PR currently reports a non-empty check like this: #17149 reports the same shape like this: The empty case follows the same pattern: That wording ties the diagnostic to the actual assertion meaning ( On macro expansion specifically: this PR emits help-only diagnostics for macro-expanded assert macros. That means it still lints, but it prints textual guidance instead of a machine-applicable rewrite because the relevant span comes from generated code. #17149 intentionally skips macro-expanded assertion conditions/assertions instead. I think that is the better first-version behavior. If the lint fires inside another macro’s expansion, the diagnostic may identify a real pattern, but the user often cannot fix it at the call site, and changing the macro definition may not be correct for every caller. That makes the warning noisier and harder to act on. Keeping the lint on source-level assertions preserves the stronger property that a warning points at code the user can usually rewrite directly. Given the overlap, I think it would be best to consolidate review on #17149 and avoid splitting the discussion across two implementations. |
Add ASSERTIONS_ON_COLLECTION_EMPTINESS lint
Suggests replacing
assert!(x.is_empty())andassert!(!x.is_empty())with
assert_eq!(x, [])/assert_ne!(x, [])(orassert_eq!(x, "")/assert_ne!(x, "")for String types), so the collection contents areshown on failure.
changelog: Added [
assertions_on_collection_emptiness] tonursery