-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[pycodestyle
] Implement redundant-backslash
(E502
)
#10292
Merged
charliermarsh
merged 5 commits into
astral-sh:main
from
augustelalande:redundant-backslash
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1ebfe9
Implement E502 redundant-backslash
augustelalande fae43f0
remove display which was added for debugging
augustelalande a8d03a0
add to KNOWN_FORMATTING_VIOLATIONS
augustelalande 9a34392
fix clippy issues
augustelalande c5f4073
rewrite using indexer
augustelalande File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
crates/ruff_linter/resources/test/fixtures/pycodestyle/E502.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,88 @@ | ||
a = 2 + 2 | ||
|
||
a = (2 + 2) | ||
|
||
a = 2 + \ | ||
3 \ | ||
+ 4 | ||
|
||
a = (3 -\ | ||
2 + \ | ||
7) | ||
|
||
z = 5 + \ | ||
(3 -\ | ||
2 + \ | ||
7) + \ | ||
4 | ||
|
||
b = [2 + | ||
2] | ||
|
||
b = [ | ||
2 + 4 + 5 + \ | ||
44 \ | ||
- 5 | ||
] | ||
|
||
c = (True and | ||
False \ | ||
or False \ | ||
and True \ | ||
) | ||
|
||
c = (True and | ||
False) | ||
|
||
d = True and \ | ||
False or \ | ||
False \ | ||
and not True | ||
|
||
|
||
s = { | ||
'x': 2 + \ | ||
2 | ||
} | ||
|
||
|
||
s = { | ||
'x': 2 + | ||
2 | ||
} | ||
|
||
|
||
x = {2 + 4 \ | ||
+ 3} | ||
|
||
y = ( | ||
2 + 2 # \ | ||
+ 3 # \ | ||
+ 4 \ | ||
+ 3 | ||
) | ||
|
||
|
||
x = """ | ||
(\\ | ||
) | ||
""" | ||
|
||
|
||
("""hello \ | ||
""") | ||
|
||
("hello \ | ||
") | ||
|
||
|
||
x = "abc" \ | ||
"xyz" | ||
|
||
x = ("abc" \ | ||
"xyz") | ||
|
||
|
||
def foo(): | ||
x = (a + \ | ||
2) |
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
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
92 changes: 92 additions & 0 deletions
92
crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/redundant_backslash.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,92 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_index::Indexer; | ||
use ruff_python_parser::TokenKind; | ||
use ruff_source_file::Locator; | ||
use ruff_text_size::{Ranged, TextRange, TextSize}; | ||
|
||
use crate::checkers::logical_lines::LogicalLinesContext; | ||
|
||
use super::LogicalLine; | ||
|
||
/// ## What it does | ||
/// Checks for redundant backslashes between brackets. | ||
/// | ||
/// ## Why is this bad? | ||
/// Explicit line joins using a backslash are redundant between brackets. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// x = (2 + \ | ||
/// 2) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// x = (2 + | ||
/// 2) | ||
/// ``` | ||
/// | ||
/// [PEP 8]: https://peps.python.org/pep-0008/#maximum-line-length | ||
#[violation] | ||
pub struct RedundantBackslash; | ||
|
||
impl AlwaysFixableViolation for RedundantBackslash { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Redundant backslash") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
"Remove redundant backslash".to_string() | ||
} | ||
} | ||
|
||
/// E502 | ||
pub(crate) fn redundant_backslash( | ||
line: &LogicalLine, | ||
locator: &Locator, | ||
indexer: &Indexer, | ||
context: &mut LogicalLinesContext, | ||
) { | ||
let mut parens = 0; | ||
let continuation_lines = indexer.continuation_line_starts(); | ||
let mut start_index = 0; | ||
|
||
for token in line.tokens() { | ||
match token.kind() { | ||
TokenKind::Lpar | TokenKind::Lsqb | TokenKind::Lbrace => { | ||
if parens == 0 { | ||
let start = locator.line_start(token.start()); | ||
start_index = continuation_lines | ||
.binary_search(&start) | ||
.map_or_else(|err_index| err_index, |ok_index| ok_index); | ||
} | ||
parens += 1; | ||
} | ||
TokenKind::Rpar | TokenKind::Rsqb | TokenKind::Rbrace => { | ||
parens -= 1; | ||
if parens == 0 { | ||
let end = locator.line_start(token.start()); | ||
let end_index = continuation_lines | ||
.binary_search(&end) | ||
.map_or_else(|err_index| err_index, |ok_index| ok_index); | ||
for continuation_line in &continuation_lines[start_index..end_index] { | ||
let backslash_end = locator.line_end(*continuation_line); | ||
let backslash_start = backslash_end - TextSize::new(1); | ||
let mut diagnostic = Diagnostic::new( | ||
RedundantBackslash, | ||
TextRange::new(backslash_start, backslash_end), | ||
); | ||
diagnostic.set_fix(Fix::safe_edit(Edit::deletion( | ||
backslash_start, | ||
backslash_end, | ||
))); | ||
context.push_diagnostic(diagnostic); | ||
} | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a struct called
Indexer
that already stores the locations of all continuations (i.e., the line positions following a\
). I'm wondering if we could instead iterate over the token stream, identify parenthesized ranges, and then search forcontinuation_lines
offsets within parenthesized ranges?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done