Skip to content

Commit

Permalink
Avoid nested loops in missing_whitespace (#3688)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Mar 23, 2023
1 parent 8a2d1a3 commit 71c0da2
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions crates/ruff/src/rules/pycodestyle/rules/missing_whitespace.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#![allow(dead_code, unused_imports, unused_variables)]

use itertools::Itertools;
use rustpython_parser::ast::Location;
use rustpython_parser::Tok;

use ruff_diagnostics::DiagnosticKind;
use ruff_diagnostics::Fix;
use ruff_diagnostics::Violation;
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::types::Range;

use crate::registry::AsRule;
use crate::rules::pycodestyle::helpers::{is_keyword_token, is_singleton_token};

#[violation]
pub struct MissingWhitespace {
pub token: String,
Expand Down Expand Up @@ -40,21 +36,26 @@ pub fn missing_whitespace(
indent_level: usize,
) -> Vec<Diagnostic> {
let mut diagnostics = vec![];
for (idx, char) in line.chars().enumerate() {
if idx + 1 == line.len() {
break;

let mut num_lsqb = 0;
let mut num_rsqb = 0;
let mut prev_lsqb = None;
let mut prev_lbrace = None;
for (idx, (char, next_char)) in line.chars().tuple_windows().enumerate() {
if char == '[' {
num_lsqb += 1;
prev_lsqb = Some(idx);
} else if char == ']' {
num_rsqb += 1;
} else if char == '{' {
prev_lbrace = Some(idx);
}
let next_char = line.chars().nth(idx + 1).unwrap();

if ",;:".contains(char) && !char::is_whitespace(next_char) {
let before = &line[..idx];
if char == ':'
&& before.matches('[').count() > before.matches(']').count()
&& before.rfind('{') < before.rfind('[')
{
if (char == ',' || char == ';' || char == ':') && !char::is_whitespace(next_char) {
if char == ':' && num_lsqb > num_rsqb && prev_lsqb > prev_lbrace {
continue; // Slice syntax, no space required
}
if char == ',' && ")]".contains(next_char) {
if char == ',' && (next_char == ')' || next_char == ']') {
continue; // Allow tuple with only one element: (3,)
}
if char == ':' && next_char == '=' {
Expand Down

0 comments on commit 71c0da2

Please sign in to comment.