Compare commits
4 Commits
dcreager/i
...
4404_fix_e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2abdc2540a | ||
|
|
1e36145972 | ||
|
|
bd2c50be86 | ||
|
|
51bc758d18 |
@@ -1,11 +1,10 @@
|
|||||||
//! Lint rules based on checking physical lines.
|
//! Lint rules based on checking physical lines.
|
||||||
|
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::TextSize;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
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::registry::Rule;
|
||||||
use crate::rules::copyright::rules::missing_copyright_notice;
|
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 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);
|
diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
|
|||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
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 ruff_python_whitespace::Line;
|
||||||
|
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
@@ -72,7 +74,12 @@ impl AlwaysAutofixableViolation for BlankLineWithWhitespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// W291, W293
|
/// 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
|
let whitespace_len: TextSize = line
|
||||||
.chars()
|
.chars()
|
||||||
.rev()
|
.rev()
|
||||||
@@ -85,17 +92,28 @@ pub(crate) fn trailing_whitespace(line: &Line, settings: &Settings) -> Option<Di
|
|||||||
if range == line.range() {
|
if range == line.range() {
|
||||||
if settings.rules.enabled(Rule::BlankLineWithWhitespace) {
|
if settings.rules.enabled(Rule::BlankLineWithWhitespace) {
|
||||||
let mut diagnostic = Diagnostic::new(BlankLineWithWhitespace, range);
|
let mut diagnostic = Diagnostic::new(BlankLineWithWhitespace, range);
|
||||||
|
|
||||||
if settings.rules.should_fix(Rule::BlankLineWithWhitespace) {
|
if settings.rules.should_fix(Rule::BlankLineWithWhitespace) {
|
||||||
#[allow(deprecated)]
|
// Remove any preceding continuations, to avoid introducing a potential
|
||||||
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range)));
|
// 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);
|
return Some(diagnostic);
|
||||||
}
|
}
|
||||||
} else if settings.rules.enabled(Rule::TrailingWhitespace) {
|
} else if settings.rules.enabled(Rule::TrailingWhitespace) {
|
||||||
let mut diagnostic = Diagnostic::new(TrailingWhitespace, range);
|
let mut diagnostic = Diagnostic::new(TrailingWhitespace, range);
|
||||||
if settings.rules.should_fix(Rule::TrailingWhitespace) {
|
if settings.rules.should_fix(Rule::TrailingWhitespace) {
|
||||||
#[allow(deprecated)]
|
diagnostic.set_fix(Fix::automatic(Edit::range_deletion(range)));
|
||||||
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range)));
|
|
||||||
}
|
}
|
||||||
return Some(diagnostic);
|
return Some(diagnostic);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ W29.py:4:6: W291 [*] Trailing whitespace
|
|||||||
|
|
|
|
||||||
= help: Remove trailing whitespace
|
= help: Remove trailing whitespace
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Fix
|
||||||
1 1 | #: Okay
|
1 1 | #: Okay
|
||||||
2 2 | # 情
|
2 2 | # 情
|
||||||
3 3 | #: W291:1:6
|
3 3 | #: W291:1:6
|
||||||
@@ -33,7 +33,7 @@ W29.py:11:35: W291 [*] Trailing whitespace
|
|||||||
|
|
|
|
||||||
= help: Remove trailing whitespace
|
= help: Remove trailing whitespace
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Fix
|
||||||
8 8 | bang = 12
|
8 8 | bang = 12
|
||||||
9 9 | #: W291:2:35
|
9 9 | #: W291:2:35
|
||||||
10 10 | '''multiline
|
10 10 | '''multiline
|
||||||
@@ -54,7 +54,7 @@ W29.py:13:6: W291 [*] Trailing whitespace
|
|||||||
|
|
|
|
||||||
= help: Remove trailing whitespace
|
= help: Remove trailing whitespace
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Fix
|
||||||
10 10 | '''multiline
|
10 10 | '''multiline
|
||||||
11 11 | string with trailing whitespace'''
|
11 11 | string with trailing whitespace'''
|
||||||
12 12 | #: W291 W292 noeol
|
12 12 | #: W291 W292 noeol
|
||||||
|
|||||||
Reference in New Issue
Block a user