-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing trailing whitespace inside multiline strings is unsafe (#9744)
Fix #8037.
- Loading branch information
Showing
7 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
crates/ruff_linter/resources/test/fixtures/pycodestyle/W291.py
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,2 @@ | ||
'''trailing whitespace | ||
inside a multiline string''' |
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
17 changes: 17 additions & 0 deletions
17
...src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__W291_W291.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,17 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
W291.py:1:23: W291 [*] Trailing whitespace | ||
| | ||
1 | '''trailing whitespace | ||
| ^ W291 | ||
2 | inside a multiline string''' | ||
| | ||
= help: Remove trailing whitespace | ||
|
||
ℹ Unsafe fix | ||
1 |-'''trailing whitespace | ||
1 |+'''trailing whitespace | ||
2 2 | inside a multiline string''' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod comment_ranges; | ||
mod fstring_ranges; | ||
mod indexer; | ||
mod multiline_ranges; | ||
|
||
pub use comment_ranges::{tokens_and_ranges, CommentRangesBuilder}; | ||
pub use indexer::Indexer; |
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,46 @@ | ||
use ruff_python_parser::Tok; | ||
use ruff_text_size::TextRange; | ||
|
||
/// Stores the range of all multiline strings in a file sorted by | ||
/// [`TextRange::start`]. | ||
pub struct MultilineRanges { | ||
ranges: Vec<TextRange>, | ||
} | ||
|
||
impl MultilineRanges { | ||
/// Returns `true` if the given range is inside a multiline string. | ||
pub fn intersects(&self, target: TextRange) -> bool { | ||
self.ranges | ||
.binary_search_by(|range| { | ||
if range.contains_range(target) { | ||
std::cmp::Ordering::Equal | ||
} else if range.end() < target.start() { | ||
std::cmp::Ordering::Less | ||
} else { | ||
std::cmp::Ordering::Greater | ||
} | ||
}) | ||
.is_ok() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub(crate) struct MultilineRangesBuilder { | ||
ranges: Vec<TextRange>, | ||
} | ||
|
||
impl MultilineRangesBuilder { | ||
pub(crate) fn visit_token(&mut self, token: &Tok, range: TextRange) { | ||
if let Tok::String { triple_quoted, .. } = token { | ||
if *triple_quoted { | ||
self.ranges.push(range); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn finish(self) -> MultilineRanges { | ||
MultilineRanges { | ||
ranges: self.ranges, | ||
} | ||
} | ||
} |