diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/empty_comment.py b/crates/ruff_linter/resources/test/fixtures/pylint/empty_comment.py index 34e2cc0fb7f60..3ea3f7f2df7ed 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/empty_comment.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/empty_comment.py @@ -51,3 +51,8 @@ def bar(): # ## # + +# This should be removed. + +α = 1 +α# diff --git a/crates/ruff_linter/src/rules/pylint/rules/empty_comment.rs b/crates/ruff_linter/src/rules/pylint/rules/empty_comment.rs index 1581b6de1ef31..5acdf8779794d 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/empty_comment.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/empty_comment.rs @@ -88,10 +88,13 @@ fn empty_comment(range: TextRange, locator: &Locator) -> Option { .slice(TextRange::new(line.start(), first_hash_col)) .char_indices() .rev() - .find(|&(_, c)| !is_python_whitespace(c) && c != '#') - .map(|(last_non_whitespace_non_comment_col, _)| { - // SAFETY: (last_non_whitespace_non_comment_col + 1) <= first_hash_col - TextSize::try_from(last_non_whitespace_non_comment_col).unwrap() + TextSize::new(1) + .find_map(|(index, char)| { + if is_python_whitespace(char) || char == '#' { + None + } else { + // SAFETY: <= first_hash_col + Some(TextSize::try_from(index + char.len_utf8()).unwrap()) + } }); Some( diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2044_empty_comment.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2044_empty_comment.py.snap index 4339810f22c21..94bb9acab3d1c 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2044_empty_comment.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2044_empty_comment.py.snap @@ -115,4 +115,19 @@ empty_comment.py:45:1: PLR2044 [*] Line with empty comment 47 46 | # These should also be removed. 48 47 | +empty_comment.py:58:2: PLR2044 [*] Line with empty comment + | +57 | α = 1 +58 | α# + | ^ PLR2044 + | + = help: Delete the empty comment + +ℹ Safe fix +55 55 | # This should be removed. +56 56 | +57 57 | α = 1 +58 |-α# + 58 |+α +