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 c9621ea
Showing 4 changed files with 23 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
@@ -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"))]
Original file line number Diff line number Diff line change
@@ -65,6 +65,17 @@ pub(crate) fn unnecessary_coding_comment(
continue;
}

// Do not apply to lines with continuations; a fix will result in invalid syntax
// Ex)
// ```
// 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(
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 c9621ea

Please sign in to comment.