Skip to content

Commit

Permalink
Do not raise violations if preceded by a continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Aug 22, 2023
1 parent f0db7a4 commit 6f863fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x = 1 \
# coding=utf8
x = 2

"""
Should not be modified due to continuation in preceding line
"""
1 change: 1 addition & 0 deletions crates/ruff/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mod tests {
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_6.py"))]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_7.py"))]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_8.py"))]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_9.py"))]
#[test_case(Rule::UnicodeKindPrefix, Path::new("UP025.py"))]
#[test_case(Rule::UnnecessaryBuiltinImport, Path::new("UP029.py"))]
#[test_case(Rule::UnnecessaryClassParentheses, Path::new("UP039.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ pub(crate) fn unnecessary_coding_comment(
continue;
}

// Do not apply to lines with continuations e.g. as a fix will result in invalid syntax
// ```
// x = 1 \
// # coding=utf8
// x = 2
// ```
if let Some(_) = indexer.preceded_by_continuations(line_range.start(), locator) {
continue;
}

let mut diagnostic = Diagnostic::new(UTF8EncodingDeclaration, *comment_range);
if settings.rules.should_fix(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::automatic(Edit::deletion(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/pyupgrade/mod.rs
---

0 comments on commit 6f863fc

Please sign in to comment.