False positives and false negatives for clippy::missing_asserts_for_indexing
.
#14079
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-negative
Issue: The lint should have been triggered on code, but wasn't
Summary
The code
let _square = slice[0] * slice[0];
falsely fires theclippy::missing_asserts_for_indexing
lint. Following the suggestion to addassert!(slice.len() > 0);
fires theclippy::len_zero
lint instead (as doesassert!(slice.len() >= 1);
). Followingclippy::len_zero
's suggestion to change it toassert!(!slice.is_empty());
causesclippy::missing_asserts_for_indexing
to reappear. Addingassert_ne!(slice.len(), 0);
has no effect (arguably a false negative forclippy::len_zero
), andassert!(slice.len() != 0);
just gets both lints firing.The false positive for
clippy::missing_asserts_for_indexing
occurs for multiple accesses to any index, but the unfortunate interaction withclippy::len_zero
is specific to the case where that index is 0.On a somewhat related note,
clippy::missing_asserts_for_indexing
also seems to ignore the ordering of things. While it correctly fires forlet _product = slice[0] * slice[1];
, it produces a false positive forlet _product = slice[1] * slice[0];
and a false negative forlet _product = slice[0] * slice[1]; assert!(slice.len() > 1);
.Possible fixes:
assert!
that does not come before the first indexing operation.assert!
, treat the first indexing operation as equivalent to anassert!
clippy::missing_asserts_for_indexing
about.is_empty()
is possible, but this would not be needed if either of the previous two points were implemented.Lint Name
clippy::missing_asserts_for_indexing
Reproducer
I tried this code:
I expected to see this happen:
A warning for
baz
, but nothing forfoo
andbar
.Instead, this happened:
Version
The text was updated successfully, but these errors were encountered: