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 2 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
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
27 changes: 22 additions & 5 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,17 +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
// Shift the diagnostic to remove the continuation as well.
diagnostic.range = range.sub_start(
(prev.text_len() - trimmed.text_len()) + TextSize::from(2),
);
}
}

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

return Some(diagnostic);
}
} else if settings.rules.enabled(Rule::TrailingWhitespace) {
let mut diagnostic = Diagnostic::new(TrailingWhitespace, range);
if settings.rules.should_fix(Rule::TrailingWhitespace) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range)));
diagnostic.set_fix(Fix::automatic(Edit::range_deletion(range)));
}
return Some(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ W29.py:4:6: W291 [*] Trailing whitespace
|
= help: Remove trailing whitespace

Suggested fix
Fix
1 1 | #: Okay
2 2 | # 情
3 3 | #: W291:1:6
Expand All @@ -33,7 +33,7 @@ W29.py:11:35: W291 [*] Trailing whitespace
|
= help: Remove trailing whitespace

Suggested fix
Fix
8 8 | bang = 12
9 9 | #: W291:2:35
10 10 | '''multiline
Expand All @@ -54,7 +54,7 @@ W29.py:13:6: W291 [*] Trailing whitespace
|
= help: Remove trailing whitespace

Suggested fix
Fix
10 10 | '''multiline
11 11 | string with trailing whitespace'''
12 12 | #: W291 W292 noeol
Expand Down
Loading