Skip to content

Commit

Permalink
Fix uncessary-coding-comment fix when there's leading content (#6775)
Browse files Browse the repository at this point in the history
Closes #6756

Including whitespace, code, and continuations.
  • Loading branch information
zanieb authored Aug 23, 2023
1 parent 3bb6388 commit 0688883
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 8 deletions.
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
# coding=utf8""" # empty comment

"""
Invalid coding declaration since it is nested inside a docstring
The following empty comment tests for false positives as our implementation visits comments
"""
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")

"""
Invalid coding declaration due to a statement before the comment
"""
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

"""
Invalid coding declaration due to continuation on preceding line
"""
5 changes: 5 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,11 @@ 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::UTF8EncodingDeclaration, Path::new("UP009_10.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 @@ -5,6 +5,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_index::Indexer;
use ruff_source_file::Locator;
use ruff_text_size::TextRange;

use crate::registry::AsRule;
use crate::settings::Settings;
Expand Down Expand Up @@ -55,20 +56,45 @@ 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);
if CODING_COMMENT_REGEX.is_match(line) {
for comment_range in indexer.comment_ranges().iter().take(2) {
// If leading content is not whitespace then it's not a valid coding comment e.g.
// ```
// print(x) # coding=utf8
// ```
let line_range = locator.full_line_range(comment_range.start());
if !locator
.slice(TextRange::new(line_range.start(), comment_range.start()))
.trim()
.is_empty()
{
continue;
}

// If the line is after a continuation then it's not a valid coding comment e.g.
// ```
// x = 1 \
// # coding=utf8
// x = 2
// ```
if indexer
.preceded_by_continuations(line_range.start(), locator)
.is_some()
{
continue;
}

if CODING_COMMENT_REGEX.is_match(locator.slice(line_range)) {
#[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);
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(),
)));
}
diagnostics.push(diagnostic);
Expand Down
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,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
---

0 comments on commit 0688883

Please sign in to comment.