11use ruff_macros:: { ViolationMetadata , derive_message_formats} ;
2+ use ruff_python_index:: Indexer ;
23use ruff_python_trivia:: { CommentRanges , is_python_whitespace} ;
34use ruff_source_file:: LineRanges ;
45use ruff_text_size:: { TextRange , TextSize } ;
@@ -49,6 +50,7 @@ pub(crate) fn empty_comments(
4950 context : & LintContext ,
5051 comment_ranges : & CommentRanges ,
5152 locator : & Locator ,
53+ indexer : & Indexer ,
5254) {
5355 let block_comments = comment_ranges. block_comments ( locator. contents ( ) ) ;
5456
@@ -59,12 +61,12 @@ pub(crate) fn empty_comments(
5961 }
6062
6163 // If the line contains an empty comment, add a diagnostic.
62- empty_comment ( context, range, locator) ;
64+ empty_comment ( context, range, locator, indexer ) ;
6365 }
6466}
6567
6668/// Return a [`Diagnostic`] if the comment at the given [`TextRange`] is empty.
67- fn empty_comment ( context : & LintContext , range : TextRange , locator : & Locator ) {
69+ fn empty_comment ( context : & LintContext , range : TextRange , locator : & Locator , indexer : & Indexer ) {
6870 // Check: is the comment empty?
6971 if !locator
7072 . slice ( range)
@@ -95,12 +97,20 @@ fn empty_comment(context: &LintContext, range: TextRange, locator: &Locator) {
9597 }
9698 } ) ;
9799
100+ // If there is no character preceding the comment, this comment must be on its own physical line.
101+ // If there is a line preceding the empty comment's line, check if it ends in a line continuation character. (`\`)
102+ let is_on_same_logical_line = indexer
103+ . preceded_by_continuations ( first_hash_col, locator. contents ( ) )
104+ . is_some ( ) ;
105+
98106 if let Some ( mut diagnostic) = context
99107 . report_diagnostic_if_enabled ( EmptyComment , TextRange :: new ( first_hash_col, line. end ( ) ) )
100108 {
101109 diagnostic. set_fix ( Fix :: safe_edit (
102110 if let Some ( deletion_start_col) = deletion_start_col {
103111 Edit :: deletion ( line. start ( ) + deletion_start_col, line. end ( ) )
112+ } else if is_on_same_logical_line {
113+ Edit :: deletion ( first_hash_col, line. end ( ) )
104114 } else {
105115 Edit :: range_deletion ( locator. full_line_range ( first_hash_col) )
106116 } ,
0 commit comments