-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[ISC001] fix panic when string literals are unclosed
#21034
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
[ISC001] fix panic when string literals are unclosed
#21034
Conversation
update snapshots
crates/ruff_python_ast/src/str.rs
Outdated
| /// Return the trailing quote string for a string, template, or bytes literal (e.g., `"""`). | ||
| pub fn trailing_quote(content: &str) -> Option<&str> { | ||
| // Tokens like "'" or "\"" represent unterminated literals that end with a single quote | ||
| // character, so they can't provide both the opening and closing delimiter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have a closer look tomorrow but I wonder if we could change the rule instead to get the quote style from the token flags (and/or not call this function if the string token is unterminated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's possible. The problem is concatenate_strings that calls trailing_quote, so If we check the string flags a_token and b_token and avoid calling concatenate_strings when there’s an issue, it will resolve the problem.
| if let Some(fix) = concatenate_strings(a_range, b_range, locator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd change concatenate_strings to take a Token instead and make it return None if a_range.string_flags()? is None. I'm also leaning towards not providing a fix when either of the strings are uncloded, but I could be convinced that we should still provide a fix.
Using StringFlags has the benefit that it also works for triple quoted strings (where the `content.len() <= 1 check isn't sufficient)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated concatenate_strings to validate token.string_flags.
|
syntax-error] Return None from trailing_quote for single-quote tokensISC001] fix panic when string literals are unclosed
Summary
Fixes #21023
Return None from trailing_quote for single-quote tokens
Test Plan
I created
crates/ruff_linter/resources/test/fixtures/flake8_implicit_str_concat/ISC_syntax_error_2.py.