Compare commits

...

4 Commits

Author SHA1 Message Date
Charlie Marsh
2abdc2540a Use preceded_by_continuations 2023-06-19 22:06:45 -04:00
Charlie Marsh
1e36145972 Merge branch 'main' into 4404_fix_e703_w293 2023-06-19 22:05:45 -04:00
Evan Rittenhouse
bd2c50be86 Simplify calculation 2023-06-19 14:54:20 -05:00
Evan Rittenhouse
51bc758d18 Fix corner case with terminal backslash in W293 2023-06-18 21:12:17 -05:00
3 changed files with 28 additions and 11 deletions

View File

@@ -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;
@@ -146,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, settings) {
if let Some(diagnostic) = trailing_whitespace(&line, locator, indexer, settings) {
diagnostics.push(diagnostic);
}
}

View File

@@ -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;
@@ -72,7 +74,12 @@ impl AlwaysAutofixableViolation for BlankLineWithWhitespace {
}
/// W291, W293
pub(crate) fn trailing_whitespace(line: &Line, settings: &Settings) -> Option<Diagnostic> {
pub(crate) fn trailing_whitespace(
line: &Line,
locator: &Locator,
indexer: &Indexer,
settings: &Settings,
) -> Option<Diagnostic> {
let whitespace_len: TextSize = line
.chars()
.rev()
@@ -85,17 +92,28 @@ 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)));
// 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)));
}
}
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);
}

View File

@@ -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
@@ -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
@@ -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