From 3389364c355d3a5c7904ea920b70047a9334b6f3 Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 22 Aug 2023 10:51:39 -0500 Subject: [PATCH 1/8] Fix `uncessary-coding-comment` fix when there is same-line leading whitespace --- .../test/fixtures/pyupgrade/UP009_6.py | 7 +++++++ .../test/fixtures/pyupgrade/UP009_7.py | 7 +++++++ crates/ruff/src/rules/pyupgrade/mod.rs | 2 ++ .../rules/unnecessary_coding_comment.rs | 6 +++--- ...f__rules__pyupgrade__tests__UP009_6.py.snap | 18 ++++++++++++++++++ ...f__rules__pyupgrade__tests__UP009_7.py.snap | 18 ++++++++++++++++++ 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 crates/ruff/resources/test/fixtures/pyupgrade/UP009_6.py create mode 100644 crates/ruff/resources/test/fixtures/pyupgrade/UP009_7.py create mode 100644 crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_6.py.snap create mode 100644 crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_7.py.snap diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_6.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_6.py new file mode 100644 index 0000000000000..d1900e39bbbc9 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_6.py @@ -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. +""" diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_7.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_7.py new file mode 100644 index 0000000000000..5433482dd8912 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_7.py @@ -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. +""" diff --git a/crates/ruff/src/rules/pyupgrade/mod.rs b/crates/ruff/src/rules/pyupgrade/mod.rs index 3e75dc8550c68..1b80bd54aee6d 100644 --- a/crates/ruff/src/rules/pyupgrade/mod.rs +++ b/crates/ruff/src/rules/pyupgrade/mod.rs @@ -67,6 +67,8 @@ 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::UnicodeKindPrefix, Path::new("UP025.py"))] #[test_case(Rule::UnnecessaryBuiltinImport, Path::new("UP029.py"))] #[test_case(Rule::UnnecessaryClassParentheses, Path::new("UP039.py"))] diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index cd0cdf213e8e5..183248be39623 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -56,8 +56,8 @@ 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) { + let comment = locator.slice(*range); + if CODING_COMMENT_REGEX.is_match(comment) { #[allow(deprecated)] let line = locator.compute_line_index(range.start()); if line.to_zero_indexed() > 1 { @@ -67,7 +67,7 @@ pub(crate) fn unnecessary_coding_comment( let mut diagnostic = Diagnostic::new(UTF8EncodingDeclaration, *range); if settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.set_fix(Fix::automatic(Edit::deletion( - range.start(), + locator.line_start(range.start()), locator.full_line_end(range.end()), ))); } diff --git a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_6.py.snap b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_6.py.snap new file mode 100644 index 0000000000000..faf65dd7cc43a --- /dev/null +++ b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_6.py.snap @@ -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 | """ + + diff --git a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_7.py.snap b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_7.py.snap new file mode 100644 index 0000000000000..8e7962330df15 --- /dev/null +++ b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_7.py.snap @@ -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 | """ + + From f0db7a43008e3c50c7b20f5d483cd24989c6c43d Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 22 Aug 2023 12:48:34 -0500 Subject: [PATCH 2/8] Fix handling of cases where the line includes non-comment content --- .../test/fixtures/pyupgrade/UP009_8.py | 6 ++++++ crates/ruff/src/rules/pyupgrade/mod.rs | 1 + .../rules/unnecessary_coding_comment.rs | 17 +++++++++-------- ...ff__rules__pyupgrade__tests__UP009_8.py.snap | 4 ++++ 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py create mode 100644 crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_8.py.snap diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py new file mode 100644 index 0000000000000..3321a35383230 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py @@ -0,0 +1,6 @@ +print("foo") # coding=utf8 +print("Hello world") + +""" +Should not be modified due to leading code on same line +""" diff --git a/crates/ruff/src/rules/pyupgrade/mod.rs b/crates/ruff/src/rules/pyupgrade/mod.rs index 1b80bd54aee6d..89ef60d46ca78 100644 --- a/crates/ruff/src/rules/pyupgrade/mod.rs +++ b/crates/ruff/src/rules/pyupgrade/mod.rs @@ -69,6 +69,7 @@ mod tests { #[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::UnicodeKindPrefix, Path::new("UP025.py"))] #[test_case(Rule::UnnecessaryBuiltinImport, Path::new("UP029.py"))] #[test_case(Rule::UnnecessaryClassParentheses, Path::new("UP039.py"))] diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index 183248be39623..c03d7c9fc0673 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -55,20 +55,21 @@ 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 comment = locator.slice(*range); - if CODING_COMMENT_REGEX.is_match(comment) { + 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); + let mut diagnostic = Diagnostic::new(UTF8EncodingDeclaration, *comment_range); if settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.set_fix(Fix::automatic(Edit::deletion( - locator.line_start(range.start()), - locator.full_line_end(range.end()), + line_range.start(), + line_range.end(), ))); } diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_8.py.snap b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_8.py.snap new file mode 100644 index 0000000000000..870ad3bf5d625 --- /dev/null +++ b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_8.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff/src/rules/pyupgrade/mod.rs +--- + From c9621ea0c1a8fda199fc9a9cf3f3fe7bdd8724c6 Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 22 Aug 2023 13:27:29 -0500 Subject: [PATCH 3/8] Do not raise violations if preceded by a continuation --- .../ruff/resources/test/fixtures/pyupgrade/UP009_9.py | 7 +++++++ crates/ruff/src/rules/pyupgrade/mod.rs | 1 + .../pyupgrade/rules/unnecessary_coding_comment.rs | 11 +++++++++++ .../ruff__rules__pyupgrade__tests__UP009_9.py.snap | 4 ++++ 4 files changed, 23 insertions(+) create mode 100644 crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py create mode 100644 crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_9.py.snap diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py new file mode 100644 index 0000000000000..fbfb852bf4d3c --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py @@ -0,0 +1,7 @@ +x = 1 \ + # coding=utf8 +x = 2 + +""" +Should not be modified due to continuation in preceding line +""" diff --git a/crates/ruff/src/rules/pyupgrade/mod.rs b/crates/ruff/src/rules/pyupgrade/mod.rs index 89ef60d46ca78..e6ff0e751bd37 100644 --- a/crates/ruff/src/rules/pyupgrade/mod.rs +++ b/crates/ruff/src/rules/pyupgrade/mod.rs @@ -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"))] diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index c03d7c9fc0673..1d2b1d195ef0b 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -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( diff --git a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_9.py.snap b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_9.py.snap new file mode 100644 index 0000000000000..870ad3bf5d625 --- /dev/null +++ b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_9.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff/src/rules/pyupgrade/mod.rs +--- + From 8835c5253b3d2b1089fbb97e7d0930a29a0ea1b9 Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 22 Aug 2023 14:08:17 -0500 Subject: [PATCH 4/8] Clippy --- .../src/rules/pyupgrade/rules/unnecessary_coding_comment.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index 1d2b1d195ef0b..f2414452af828 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -72,7 +72,10 @@ pub(crate) fn unnecessary_coding_comment( // # coding=utf8 // x = 2 // ``` - if let Some(_) = indexer.preceded_by_continuations(line_range.start(), locator) { + if indexer + .preceded_by_continuations(line_range.start(), locator) + .is_some() + { continue; } From be1eac13f9055f1a4d01b8abfd1389a1d62eefd5 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 23 Aug 2023 10:25:41 -0500 Subject: [PATCH 5/8] Add failing test case for false positive with docstring --- crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py | 7 +++++++ crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py | 2 +- crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py | 2 +- crates/ruff/src/rules/pyupgrade/mod.rs | 1 + .../ruff__rules__pyupgrade__tests__UP009_10.py.snap | 4 ++++ 5 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py create mode 100644 crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_10.py.snap diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py new file mode 100644 index 0000000000000..bfb26d1717ab9 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py @@ -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 +""" diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py index 3321a35383230..e0e48475bf907 100644 --- a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py @@ -2,5 +2,5 @@ print("Hello world") """ -Should not be modified due to leading code on same line +Invalid coding declaration due to a statement before the comment """ diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py index fbfb852bf4d3c..b88d6cb794c08 100644 --- a/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py @@ -3,5 +3,5 @@ x = 2 """ -Should not be modified due to continuation in preceding line +Invalid coding declaration due to continuation on preceding line """ diff --git a/crates/ruff/src/rules/pyupgrade/mod.rs b/crates/ruff/src/rules/pyupgrade/mod.rs index e6ff0e751bd37..416972454fcc4 100644 --- a/crates/ruff/src/rules/pyupgrade/mod.rs +++ b/crates/ruff/src/rules/pyupgrade/mod.rs @@ -71,6 +71,7 @@ mod tests { #[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"))] diff --git a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_10.py.snap b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_10.py.snap new file mode 100644 index 0000000000000..870ad3bf5d625 --- /dev/null +++ b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP009_10.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff/src/rules/pyupgrade/mod.rs +--- + From 4fb7c287e1dde395d44d4457ff7dd5acaab0c424 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 23 Aug 2023 10:29:19 -0500 Subject: [PATCH 6/8] Apply the regex to the comment and the line --- .../rules/pyupgrade/rules/unnecessary_coding_comment.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index f2414452af828..9d457ea6c82cc 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -58,7 +58,12 @@ pub(crate) fn unnecessary_coding_comment( 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) { + let comment = locator.slice(*comment_range); + + // Both the line and the comment itself must match to prevent false positives + // where the comment is on a line that includes content that looks like a coding comment + // or the coding comment is preceded by non-whitespace + if CODING_COMMENT_REGEX.is_match(line) && CODING_COMMENT_REGEX.is_match(comment) { #[allow(deprecated)] let index = locator.compute_line_index(line_range.start()); if index.to_zero_indexed() > 1 { From c98e5c509e1e98b78228efbf2ed5fa887ca4bb44 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 23 Aug 2023 11:00:37 -0500 Subject: [PATCH 7/8] Check for leading whitespace instead of applying the regex twice --- .../rules/unnecessary_coding_comment.rs | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index 9d457ea6c82cc..0946cbb9f845a 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -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; @@ -56,34 +57,39 @@ 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 comment_range in indexer.comment_ranges().iter().take(2) { + // If leading content is not whitspace then it's not a valid coding comment e.g. + // ``` + // print(x) # coding=utf8 + // ``` let line_range = locator.full_line_range(comment_range.start()); - let line = locator.slice(line_range); - let comment = locator.slice(*comment_range); + 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; + } - // Both the line and the comment itself must match to prevent false positives - // where the comment is on a line that includes content that looks like a coding comment - // or the coding comment is preceded by non-whitespace - if CODING_COMMENT_REGEX.is_match(line) && CODING_COMMENT_REGEX.is_match(comment) { + if CODING_COMMENT_REGEX.is_match(locator.slice(line_range)) { #[allow(deprecated)] let index = locator.compute_line_index(line_range.start()); if index.to_zero_indexed() > 1 { continue; } - // 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( From 5436c892f1bb290f188ddc41e8d471166d9e7ecd Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 23 Aug 2023 11:22:56 -0500 Subject: [PATCH 8/8] Fix typo --- .../src/rules/pyupgrade/rules/unnecessary_coding_comment.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index 0946cbb9f845a..72e55a6b11ab6 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -57,7 +57,7 @@ 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 comment_range in indexer.comment_ranges().iter().take(2) { - // If leading content is not whitspace then it's not a valid coding comment e.g. + // If leading content is not whitespace then it's not a valid coding comment e.g. // ``` // print(x) # coding=utf8 // ```