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

new lint: `chars_enumerate_for_byte_indices`

Open y21 opened this issue 1 year ago • 2 comments

Closes #10202.

This adds a new lint that checks for uses of the .chars().enumerate() position in a context where a byte index is required and suggests changing it to use .char_indices() instead.

I'm planning to extend this lint to also detect uses of the position in iterator chains, e.g. s.chars().enumerate().for_each(|(i, _)| s.split_at(i));, but that's for another time


changelog: new lint: chars_enumerate_for_byte_indices

y21 avatar Sep 21 '24 20:09 y21

r? @Centri3

rustbot has assigned @Centri3. 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

rustbot avatar Sep 21 '24 20:09 rustbot

Thank you for working on this.

The linter we are working on (oxlint) has encountered dozens of crashes because of this, and there were no ways to forbid such usages.

Boshen avatar Sep 26 '24 08:09 Boshen

:umbrella: The latest upstream changes (presumably 1f966e98db0c83fc76657fbc0bd50d11526787b1) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Dec 01 '24 12:12 bors

Added a note for str::bytes and also renamed the lint to be slightly more general in case we have other sources of char indices in the future as Jarcho suggested on zulip. Left them as separate commits for now so it's easier to review but will squash once you think it's ok

y21 avatar Dec 03 '24 03:12 y21

Hey @y21, can you take a look at this again? Is there anything from my review that doesn't make sense/you're stuck on?

Centri3 avatar Feb 14 '25 14:02 Centri3

Oops, I got busy and forgot about this. I'll look at this again over the weekend!

y21 avatar Feb 14 '25 14:02 y21