From 2abdc2540a0ea7756274cbd3c7a53713f8a5d1b2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 19 Jun 2023 22:06:45 -0400 Subject: [PATCH] Use preceded_by_continuations --- crates/ruff/src/checkers/physical_lines.rs | 6 +--- .../pycodestyle/rules/trailing_whitespace.rs | 29 ++++++++++--------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/crates/ruff/src/checkers/physical_lines.rs b/crates/ruff/src/checkers/physical_lines.rs index 9c283e18db..3bf5400cd3 100644 --- a/crates/ruff/src/checkers/physical_lines.rs +++ b/crates/ruff/src/checkers/physical_lines.rs @@ -57,8 +57,6 @@ pub(crate) fn check_physical_lines( let mut commented_lines_iter = indexer.comment_ranges().iter().peekable(); let mut doc_lines_iter = doc_lines.iter().peekable(); - let mut prev_line: Option = None; - for (index, line) in locator.contents().universal_newlines().enumerate() { while commented_lines_iter .next_if(|comment_range| line.range().contains_range(**comment_range)) @@ -147,7 +145,7 @@ pub(crate) fn check_physical_lines( } if enforce_trailing_whitespace || enforce_blank_line_contains_whitespace { - if let Some(diagnostic) = trailing_whitespace(&line, &prev_line, settings) { + if let Some(diagnostic) = trailing_whitespace(&line, locator, indexer, settings) { diagnostics.push(diagnostic); } } @@ -157,8 +155,6 @@ pub(crate) fn check_physical_lines( diagnostics.push(diagnostic); } } - - prev_line = Some(line); } if enforce_no_newline_at_end_of_file { diff --git a/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs index 1f9035870c..740d43d964 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs @@ -2,6 +2,8 @@ use ruff_text_size::{TextLen, TextRange, TextSize}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers; +use ruff_python_ast::source_code::{Indexer, Locator}; use ruff_python_whitespace::Line; use crate::registry::Rule; @@ -74,7 +76,8 @@ impl AlwaysAutofixableViolation for BlankLineWithWhitespace { /// W291, W293 pub(crate) fn trailing_whitespace( line: &Line, - prev_line: &Option, + locator: &Locator, + indexer: &Indexer, settings: &Settings, ) -> Option { let whitespace_len: TextSize = line @@ -91,20 +94,18 @@ pub(crate) fn trailing_whitespace( let mut diagnostic = Diagnostic::new(BlankLineWithWhitespace, range); if settings.rules.should_fix(Rule::BlankLineWithWhitespace) { - // If this line is blank with whitespace, we have to ensure that the previous line - // doesn't end with a backslash. If it did, the file would end with a backslash - // and therefore have an "unexpected EOF" SyntaxError, so we have to remove it. - if let Some(prev) = prev_line { - let trimmed = prev.trim_end(); - if trimmed.ends_with('\\') { - // Shift the diagnostic to remove the continuation as well. - diagnostic.range = range.sub_start( - (prev.text_len() - trimmed.text_len()) + TextSize::from(2), - ); - } + // Remove any preceding continuations, to avoid introducing a potential + // syntax error. + if let Some(continuation) = + helpers::preceded_by_continuations(line.start(), locator, indexer) + { + diagnostic.set_fix(Fix::suggested(Edit::range_deletion(TextRange::new( + continuation, + range.end(), + )))); + } else { + diagnostic.set_fix(Fix::suggested(Edit::range_deletion(range))); } - - diagnostic.set_fix(Fix::suggested(Edit::range_deletion(diagnostic.range))); } return Some(diagnostic);