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; }