Skip to content

Conversation

@andjsrk
Copy link

@andjsrk andjsrk commented Dec 14, 2025

Fixes #16237

I think current behavior is unsatisfying because comparing v.len() with const values in assert!() is pretty common (31.2k results on GitHub).
This PR would make enabling this lint less painful in my opinion.

Example

#![warn(clippy::missing_asserts_for_indexing)]

const MIN_LEN: usize = 2;
fn assert_cmp_to_const(v: &[u8]) {
    assert!(v.len() >= MIN_LEN);

    let _ = v[0] + v[1]; // was warning, now ok
    let _ = v[0] + v[1] + v[2]; // correctly warned
}

changelog: [missing_asserts_for_indexing]: support comparing to const values

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 14, 2025

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@andjsrk
Copy link
Author

andjsrk commented Dec 14, 2025

I have changed suggesting behavior as below, but I'm not sure if it is reasonable:

if all of these were satisfied, suggest v.len() > N_ORIGINAL_EXPR rather than v.len() >= N_PLUS_ONE_LITERAL:

  • v.len() is compared to a const value.
  • the operator in the assertion is either <= or >=.
  • the highest index is equal to asserted length.

@andjsrk andjsrk force-pushed the missing-asserts-for-indexing-cmp-const branch from b087cc1 to 0c7b43b Compare December 14, 2025 22:36
@andjsrk andjsrk force-pushed the missing-asserts-for-indexing-cmp-const branch from 0c7b43b to 4af1bef Compare December 15, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

missing_asserts_for_indexing: false positive when length is compared with const value

3 participants