Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint suggestion: Replace .chars().enumerate() with .char_indices() when index is used in subsequent string slice operations #10202

Open
messense opened this issue Jan 14, 2023 · 0 comments · May be fixed by #13435
Assignees
Labels
A-lint Area: New lints

Comments

@messense
Copy link
Contributor

What it does

Suggest instances where .chars().enumerate() should be replaced with .char_indices() instead for str slices when the index returned by .enumerate() is used in subsequent string slice operations like s[i..] or s.split_at(i).

Here is a real world example of this kind of issue: RustPython/RustPython#4444, failure to correctly handle multi-byte character when doing string slice operations leads to panic at runtime.

Lint Name

No response

Category

correctness

Advantage

  • Correctly handle multi-byte character in string slice operations

Drawbacks

No response

Example

let s = "{a:%ЫйЯЧ}";
for (i, c) in s.chars().enumerate() {
    let (_left, _right) = s.split_at(i);
}

Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants