[flake8-simplify] Fix incorrect fix for positive maxsplit without separator (SIM905)#20056
Conversation
|
ntBre
left a comment
There was a problem hiding this comment.
Thanks for jumping on this!
I'm kind of leaning away from defining our own iterator, if we can avoid it.
| let start = self | ||
| .remaining | ||
| .rfind(py_unicode_is_whitespace) | ||
| .map(|pos| pos + 1) |
There was a problem hiding this comment.
I don't think this is safe to do. rfind returns the byte offset of the first character of the match. I tried a case like this:
const fn py_unicode_is_whitespace(ch: char) -> bool {
matches!(ch, '\u{3000}')
}
fn main() {
let text = "\u{3000}next";
let start = text.rfind(py_unicode_is_whitespace).unwrap();
dbg!(&text[start + 1..]);
}in the Rust playground, and it panics with a byte index is not a char boundary error. This would be the case for any of the multi-byte whitespace characters in our real py_unicode_is_whitespace function.
There was a problem hiding this comment.
Right, I need to increment by char::len_utf8.
crates/ruff_linter/src/rules/flake8_simplify/rules/split_static_string.rs
Outdated
Show resolved
Hide resolved
Depending how you feel about it we could just remove the fix, since it wasn't spotted in #19851 by me or in review. The iterator is a bit verbose but I think the logic is ok. |
1a408cd to
b69799f
Compare
ntBre
left a comment
There was a problem hiding this comment.
Thanks! I think you're right, I was being overly scared of the iterator. This looks good to me overall, just one idea for simplifying a bit by using (r)split_once. It seemed to work well for me locally, but let me know if I'm still missing something.
crates/ruff_linter/src/rules/flake8_simplify/rules/split_static_string.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_simplify/rules/split_static_string.rs
Outdated
Show resolved
Hide resolved
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
b69799f to
f4c9f98
Compare
|
I took care of the merge conflicts. It looks like |
flake8-simplify] Fix incorrect fix for positive maxsplit without separator (SIM905)flake8-simplify] Fix incorrect fix for positive maxsplit without separator (SIM905)
Summary
Resolves #20033
Test Plan
unit tests added to the new split function, existing snapshot test updated.