-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Resolves #12321. The physical-line-based `RUF054` checks for form feed characters that are preceded by only tabs and spaces, but not any other characters, including form feeds. ## Test Plan `cargo nextest run` and `cargo insta test`.
- Loading branch information
1 parent
9ae98d4
commit f367aa8
Showing
10 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
############# Warning ############ | ||
# This file contains form feeds. # | ||
############# Warning ############ | ||
|
||
|
||
# Errors | ||
|
||
|
||
|
||
|
||
|
||
def _(): | ||
pass | ||
|
||
if False: | ||
print('F') | ||
print('T') | ||
|
||
|
||
# No errors | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def _(): | ||
pass | ||
|
||
def f(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
crates/ruff_linter/src/rules/ruff/rules/indented_form_feed.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use memchr::memchr; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, ViolationMetadata}; | ||
use ruff_source_file::Line; | ||
use ruff_text_size::{TextRange, TextSize}; | ||
|
||
/// ## What it does | ||
/// Checks for form feed characters preceded by either a space or a tab. | ||
/// | ||
/// ## Why is this bad? | ||
/// [The language reference][lexical-analysis-indentation] states: | ||
/// | ||
/// > A formfeed character may be present at the start of the line; | ||
/// > it will be ignored for the indentation calculations above. | ||
/// > Formfeed characters occurring elsewhere in the leading whitespace | ||
/// > have an undefined effect (for instance, they may reset the space count to zero). | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```python | ||
/// if foo():\n \fbar() | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```python | ||
/// if foo():\n bar() | ||
/// ``` | ||
/// | ||
/// [lexical-analysis-indentation]: https://docs.python.org/3/reference/lexical_analysis.html#indentation | ||
#[derive(ViolationMetadata)] | ||
pub(crate) struct IndentedFormFeed; | ||
|
||
impl Violation for IndentedFormFeed { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
"Indented form feed".to_string() | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some("Remove form feed".to_string()) | ||
} | ||
} | ||
|
||
const FORM_FEED: u8 = b'\x0c'; | ||
const SPACE: u8 = b' '; | ||
const TAB: u8 = b'\t'; | ||
|
||
/// RUF054 | ||
pub(crate) fn indented_form_feed(line: &Line) -> Option<Diagnostic> { | ||
let index_relative_to_line = memchr(FORM_FEED, line.as_bytes())?; | ||
|
||
if index_relative_to_line == 0 { | ||
return None; | ||
} | ||
|
||
if line[..index_relative_to_line] | ||
.as_bytes() | ||
.iter() | ||
.any(|byte| *byte != SPACE && *byte != TAB) | ||
{ | ||
return None; | ||
} | ||
|
||
let relative_index = u32::try_from(index_relative_to_line).ok()?; | ||
let absolute_index = line.start() + TextSize::new(relative_index); | ||
let range = TextRange::at(absolute_index, 1.into()); | ||
|
||
Some(Diagnostic::new(IndentedFormFeed, range)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF054_RUF054.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
--- | ||
RUF054.py:8:2: RUF054 Indented form feed | ||
| | ||
6 | # Errors | ||
7 | | ||
8 | | ||
| ^ RUF054 | ||
| | ||
= help: Remove form feed | ||
|
||
RUF054.py:10:3: RUF054 Indented form feed | ||
| | ||
10 | | ||
| ^ RUF054 | ||
11 | | ||
12 | def _(): | ||
| | ||
= help: Remove form feed | ||
|
||
RUF054.py:13:2: RUF054 Indented form feed | ||
| | ||
12 | def _(): | ||
13 | pass | ||
| ^ RUF054 | ||
14 | | ||
15 | if False: | ||
| | ||
= help: Remove form feed | ||
|
||
RUF054.py:17:5: RUF054 Indented form feed | ||
| | ||
15 | if False: | ||
16 | print('F') | ||
17 | print('T') | ||
| ^ RUF054 | ||
| | ||
= help: Remove form feed |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters