oxc-project / oxc

⚓ A collection of JavaScript tools written in Rust.
https://oxc.rs
MIT License
12.42k stars 456 forks source link

Ban index methods on `std::str::Chars` #6071

Closed Boshen closed 1 month ago

Boshen commented 1 month ago

Background:

A quick grep:

crates/oxc_linter/src/rules/nextjs/no_typos.rs|136 col 22| for (i, s1) in a.chars().enumerate() {
crates/oxc_linter/src/rules/nextjs/no_typos.rs|138 col 26| for (j, s2) in b.chars().enumerate() {
crates/oxc_codegen/src/lib.rs|318 col 63| unsafe { std::str::from_utf8_unchecked(self.code()) }.chars().nth_back(n)
crates/oxc_linter/src/partial_loader/astro.rs|75 col 49| if let Some('/') = self.source_text.chars().nth(js_start - 2) {
crates/oxc_linter/src/rules/eslint/no_useless_escape.rs|199 col 33| let escape_char = char_text.chars().nth(1).unwrap();
crates/oxc_linter/src/rules/eslint/no_useless_escape.rs|227 col 60| if let Some(prev_char) = ctx.source_text().chars().nth(span.end as usize) {
crates/oxc_linter/src/rules/eslint/no_useless_escape.rs|233 col 65| if let Some(prev_prev_char) = ctx.source_text().chars().nth(span.start as usize - 1)
crates/oxc_linter/src/rules/eslint/no_useless_escape.rs|290 col 28| if let Some(c) = s.chars().nth(1) {
crates/oxc_linter/src/rules/oxc/only_used_in_recursion.rs|239 col 19| let c = s.chars().nth(i).unwrap();
crates/oxc_linter/src/utils/react.rs|256 col 37| name.starts_with("use") && name.chars().nth(3).map_or(true, char::is_uppercase)
crates/oxc_linter/src/utils/react.rs|259 col 40| // name.starts_with("use") && name.chars().nth(3).is_some_and(char::is_uppercase)
crates/oxc_minifier/src/node_util/mod.rs|393 col 38| let radix: u32 = match s.chars().nth(1) {
crates/oxc_prettier/src/format/mod.rs|84 col 66| if let Some(c) = p.source_text[self.span.end as usize..].chars().nth(1) {
crates/oxc_sourcemap/src/sourcemap_visualizer.rs|144 col 46| if ch == '\r' && content.chars().nth(i + 1) == Some('\n') {
Boshen commented 1 month ago

We should add https://ast-grep.github.io/catalog/rust/#beware-of-char-offset-when-iterate-over-a-string

image