diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs b/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs index 6793becbd1..7da64c8bf7 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs @@ -392,23 +392,21 @@ impl<'a> SourceOrderVisitor<'a> for QuoteAnnotator<'a> { } self.state.pop(); } + // For the following expressions, we just need to make sure to visit the nested + // expressions using the quote annotator and not use the generator. This is so that any + // subscript elements nested within them are identified and quoted correctly. Expr::List(ast::ExprList { elts, .. }) => { - let Some((first, remaining)) = elts.split_first() else { - return; - }; + let mut first = true; self.annotation.push('['); - self.visit_expr(first); - if let Some(last) = self.state.last_mut() { - if *last == QuoteAnnotatorState::AnnotatedFirst { - *last = QuoteAnnotatorState::AnnotatedRest; + for expr in elts { + if first { + first = false; + } else { + self.annotation.push_str(", "); } - } - for expr in remaining { - self.annotation.push_str(", "); self.visit_expr(expr); } self.annotation.push(']'); - self.state.pop(); } Expr::BinOp(ast::ExprBinOp { left, op, right, ..