Skip to content
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

Fix uncessary-coding-comment fix when there's leading content #6775

Merged
merged 8 commits into from
Aug 23, 2023
Merged
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding=utf8
print("Hello world")

"""
Regression test for https://github.com/astral-sh/ruff/issues/6756
The leading space must be removed to prevent invalid syntax.
"""
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding=utf8
print("Hello world")

"""
Regression test for https://github.com/astral-sh/ruff/issues/6756
The leading tab must be removed to prevent invalid syntax.
"""
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
print("foo") # coding=utf8
print("Hello world")

"""
Should not be modified due to leading code on same line
"""
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
"""
4 changes: 4 additions & 0 deletions crates/ruff/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ mod tests {
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_3.py"))]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_4.py"))]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_5.py"))]
#[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 @@ -55,20 +55,35 @@ pub(crate) fn unnecessary_coding_comment(
) {
// The coding comment must be on one of the first two lines. Since each comment spans at least
// one line, we only need to check the first two comments at most.
for range in indexer.comment_ranges().iter().take(2) {
let line = locator.slice(*range);
for comment_range in indexer.comment_ranges().iter().take(2) {
let line_range = locator.full_line_range(comment_range.start());
let line = locator.slice(line_range);
if CODING_COMMENT_REGEX.is_match(line) {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
if line.to_zero_indexed() > 1 {
let index = locator.compute_line_index(line_range.start());
if index.to_zero_indexed() > 1 {
continue;
}

let mut diagnostic = Diagnostic::new(UTF8EncodingDeclaration, *range);
// Do not apply to lines with continuations; a fix will result in invalid syntax
// Ex)
// ```
// x = 1 \
// # coding=utf8
// x = 2
// ```
if indexer
.preceded_by_continuations(line_range.start(), locator)
.is_some()
{
continue;
}

let mut diagnostic = Diagnostic::new(UTF8EncodingDeclaration, *comment_range);
if settings.rules.should_fix(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::automatic(Edit::deletion(
range.start(),
locator.full_line_end(range.end()),
line_range.start(),
line_range.end(),
zanieb marked this conversation as resolved.
Show resolved Hide resolved
)));
}
diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff/src/rules/pyupgrade/mod.rs
---
UP009_6.py:1:2: UP009 [*] UTF-8 encoding declaration is unnecessary
|
1 | # coding=utf8
| ^^^^^^^^^^^^^ UP009
2 | print("Hello world")
|
= help: Remove unnecessary coding comment

ℹ Fix
1 |- # coding=utf8
2 1 | print("Hello world")
3 2 |
4 3 | """


Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff/src/rules/pyupgrade/mod.rs
---
UP009_7.py:1:2: UP009 [*] UTF-8 encoding declaration is unnecessary
|
1 | # coding=utf8
| ^^^^^^^^^^^^^ UP009
2 | print("Hello world")
|
= help: Remove unnecessary coding comment

ℹ Fix
1 |- # coding=utf8
2 1 | print("Hello world")
3 2 |
4 3 | """


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/pyupgrade/mod.rs
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/pyupgrade/mod.rs
---