From f9ffa80833f07fdb2a6afa4a2b5c404134e70b68 Mon Sep 17 00:00:00 2001 From: Hoel Bagard Date: Sun, 10 Mar 2024 00:29:50 +0900 Subject: [PATCH] fix: do not trigger E225 and E275 when the next token is a ')' Fixes #10295. --- .../logical_lines/missing_whitespace_after_keyword.rs | 5 ++++- .../logical_lines/missing_whitespace_around_operator.rs | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs index 7aa8cfc24764a..9c7b3df7811a2 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs @@ -59,7 +59,10 @@ pub(crate) fn missing_whitespace_after_keyword( || tok0_kind == TokenKind::Yield && tok1_kind == TokenKind::Rpar || matches!( tok1_kind, - TokenKind::Colon | TokenKind::Newline | TokenKind::NonLogicalNewline + TokenKind::Colon + | TokenKind::Newline + | TokenKind::NonLogicalNewline + | TokenKind::Rpar // In case of a syntax error, do not attempt to add a whitespace. )) && tok0.end() == tok1.start() { diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_around_operator.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_around_operator.rs index c5432802ccd26..bcedfe3d7fa31 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_around_operator.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_around_operator.rs @@ -159,7 +159,13 @@ pub(crate) fn missing_whitespace_around_operator( while let Some(token) = tokens.next() { let kind = token.kind(); - if kind.is_trivia() { + // There should not be a ")" directly after a token, as it is a syntax error. However if that happens, + // allow it in order to prevent entering a loop where E225 adds a space only for E202 to remove it. + if kind.is_trivia() + || tokens + .peek() + .is_some_and(|token| token.kind() == TokenKind::Rpar) + { continue; }