Skip to content

Conversation

@TaKO8Ki
Copy link
Contributor

@TaKO8Ki TaKO8Ki commented Oct 22, 2025

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.

/// 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.
Copy link
Member

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)

Copy link
Contributor Author

@TaKO8Ki TaKO8Ki Oct 22, 2025

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) {

Copy link
Member

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)

Copy link
Contributor Author

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.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 22, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@TaKO8Ki TaKO8Ki requested a review from MichaReiser October 28, 2025 12:24
@MichaReiser MichaReiser changed the title [syntax-error] Return None from trailing_quote for single-quote tokens [ISC001] fix panic when string literals are unclosed Oct 28, 2025
@MichaReiser MichaReiser added the bug Something isn't working label Oct 28, 2025
@MichaReiser MichaReiser enabled auto-merge (squash) October 28, 2025 19:06
@MichaReiser MichaReiser merged commit d0aebaa into astral-sh:main Oct 28, 2025
36 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ISC001 panic: begin <= end (1 <= 0) when slicing '

2 participants