Skip to content
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

Remove special-casing for whitespace-around-@ #4458

Merged
merged 1 commit into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::checkers::logical_lines::LogicalLinesContext;
use crate::rules::pycodestyle::rules::logical_lines::{LogicalLine, LogicalLineToken};
use ruff_diagnostics::{DiagnosticKind, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::token_kind::TokenKind;

use crate::checkers::logical_lines::LogicalLinesContext;
use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;

// E225
#[violation]
pub struct MissingWhitespaceAroundOperator;
Expand Down Expand Up @@ -49,14 +50,22 @@ impl Violation for MissingWhitespaceAroundModuloOperator {
}

/// E225, E226, E227, E228
#[allow(clippy::if_same_then_else)]
pub(crate) fn missing_whitespace_around_operator(
line: &LogicalLine,
context: &mut LogicalLinesContext,
) {
let mut parens = 0u32;
let mut prev_token: Option<&LogicalLineToken> = None;
let mut tokens = line.tokens().iter().peekable();
let first_token = tokens.by_ref().find_map(|token| {
let kind = token.kind();
(!kind.is_trivia()).then_some(token)
});
let Some(mut prev_token) = first_token else {
return;
};
let mut parens = u32::from(matches!(
prev_token.kind(),
TokenKind::Lpar | TokenKind::Lambda
));

while let Some(token) = tokens.next() {
let kind = token.kind();
Expand Down Expand Up @@ -86,26 +95,20 @@ pub(crate) fn missing_whitespace_around_operator(
);

NeedsSpace::from(!slash_in_func)
} else if kind.is_unary()
|| matches!(
kind,
TokenKind::Star | TokenKind::DoubleStar | TokenKind::At
)
{
let is_binary = prev_token.map_or(false, |prev_token| {
} else if kind.is_unary() || matches!(kind, TokenKind::Star | TokenKind::DoubleStar) {
let is_binary = {
let prev_kind = prev_token.kind();

// Check if the operator is used as a binary operator.
// Allow unary operators: -123, -x, +1.
// Allow argument unpacking: foo(*args, **kwargs)
// Allow decorators: @foo, @foo(1)
matches!(
prev_kind,
TokenKind::Rpar | TokenKind::Rsqb | TokenKind::Rbrace
) || !(prev_kind.is_operator()
|| prev_kind.is_keyword()
|| prev_kind.is_soft_keyword())
});
};

if is_binary {
if kind == TokenKind::DoubleStar {
Expand All @@ -124,20 +127,18 @@ pub(crate) fn missing_whitespace_around_operator(
};

if needs_space != NeedsSpace::No {
let has_leading_trivia = prev_token.map_or(true, |prev| {
prev.end() < token.start() || prev.kind().is_trivia()
});
let has_leading_trivia =
prev_token.end() < token.start() || prev_token.kind().is_trivia();

let has_trailing_trivia = tokens.peek().map_or(true, |next| {
token.end() < next.start() || next.kind().is_trivia()
});

match (has_leading_trivia, has_trailing_trivia) {
// Operator with trailing but no leading space, enforce consistent spacing
// Operator with trailing but no leading space, enforce consistent spacing.
(false, true) |
// Operator with leading but no trailing space, enforce consistent spacing.
(true, false)
=> {
(true, false) => {
context.push(MissingWhitespaceAroundOperator, token.range());
}
// Operator with no space, require spaces if it is required by the operator.
Expand All @@ -147,12 +148,12 @@ pub(crate) fn missing_whitespace_around_operator(
}
}
(true, true) => {
// Operator has leading and trailing spaces, all good
// Operator has leading and trailing spaces, all good.
}
}
}

prev_token = Some(token);
prev_token = token;
}
}

Expand Down