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

Fix corner case involving terminal backslash after fixing W293 #5172

Merged
merged 5 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions crates/ruff/src/checkers/physical_lines.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Lint rules based on checking physical lines.

use ruff_text_size::TextSize;
use std::path::Path;

use ruff_diagnostics::Diagnostic;
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
use ruff_python_whitespace::UniversalNewlines;
use ruff_python_whitespace::{Line, UniversalNewlines};

use crate::registry::Rule;
use crate::rules::copyright::rules::missing_copyright_notice;
Expand Down Expand Up @@ -58,6 +57,8 @@ 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<Line> = None;

for (index, line) in locator.contents().universal_newlines().enumerate() {
while commented_lines_iter
.next_if(|comment_range| line.range().contains_range(**comment_range))
Expand Down Expand Up @@ -146,7 +147,7 @@ pub(crate) fn check_physical_lines(
}

if enforce_trailing_whitespace || enforce_blank_line_contains_whitespace {
if let Some(diagnostic) = trailing_whitespace(&line, settings) {
if let Some(diagnostic) = trailing_whitespace(&line, &prev_line, settings) {
diagnostics.push(diagnostic);
}
}
Expand All @@ -156,6 +157,8 @@ pub(crate) fn check_physical_lines(
diagnostics.push(diagnostic);
}
}

prev_line = Some(line);
}

if enforce_no_newline_at_end_of_file {
Expand Down
30 changes: 27 additions & 3 deletions crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ impl AlwaysAutofixableViolation for BlankLineWithWhitespace {
}

/// W291, W293
pub(crate) fn trailing_whitespace(line: &Line, settings: &Settings) -> Option<Diagnostic> {
pub(crate) fn trailing_whitespace(
line: &Line,
prev_line: &Option<Line>,
settings: &Settings,
) -> Option<Diagnostic> {
let whitespace_len: TextSize = line
.chars()
.rev()
Expand All @@ -85,10 +89,30 @@ pub(crate) fn trailing_whitespace(line: &Line, settings: &Settings) -> Option<Di
if range == line.range() {
if settings.rules.enabled(Rule::BlankLineWithWhitespace) {
let mut diagnostic = Diagnostic::new(BlankLineWithWhitespace, range);

if settings.rules.should_fix(Rule::BlankLineWithWhitespace) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range)));
// 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('\\') {
evanrittenhouse marked this conversation as resolved.
Show resolved Hide resolved
let initial_len = prev.text_len();
diagnostic.range = range.sub_start(
// Shift by the amount of whitespace in the previous line, plus the
// newline, plus the slash, plus any remaining whitespace after it.
evanrittenhouse marked this conversation as resolved.
Show resolved Hide resolved
initial_len - trimmed.text_len()
+ TextSize::from(1)
+ '\\'.text_len()
+ (trimmed.text_len() - trimmed.trim_end().text_len()
+ TextSize::from(1)),
);
}
}

diagnostic.set_fix(Fix::suggested(Edit::range_deletion(diagnostic.range)));
}

return Some(diagnostic);
}
} else if settings.rules.enabled(Rule::TrailingWhitespace) {
Expand Down