From d42a1827d841ac9938be228a167b2ae54efb58fd Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 20 May 2024 13:08:29 +0900 Subject: [PATCH] Fix a false negative for `Layout/EmptyComment` This PR fixes a false negative for `Layout/EmptyComment` when using an empty comment next to code after comment line. --- ...x_false_negative_for_layout_empty_comment.md | 1 + lib/rubocop/cop/layout/empty_comment.rb | 4 +++- spec/rubocop/cop/layout/empty_comment_spec.rb | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_negative_for_layout_empty_comment.md diff --git a/changelog/fix_false_negative_for_layout_empty_comment.md b/changelog/fix_false_negative_for_layout_empty_comment.md new file mode 100644 index 000000000000..7d62a2c2310c --- /dev/null +++ b/changelog/fix_false_negative_for_layout_empty_comment.md @@ -0,0 +1 @@ +* [#12914](https://github.com/rubocop/rubocop/pull/12914): Fix a false negative for `Layout/EmptyComment` when using an empty comment next to code after comment line. ([@koic][]) diff --git a/lib/rubocop/cop/layout/empty_comment.rb b/lib/rubocop/cop/layout/empty_comment.rb index 0243f8800ffb..8621de5df305 100644 --- a/lib/rubocop/cop/layout/empty_comment.rb +++ b/lib/rubocop/cop/layout/empty_comment.rb @@ -106,7 +106,9 @@ def autocorrect(corrector, node) end def concat_consecutive_comments(comments) - consecutive_comments = comments.chunk_while { |i, j| i.loc.line.succ == j.loc.line } + consecutive_comments = comments.chunk_while do |i, j| + i.loc.line.succ == j.loc.line && i.loc.column == j.loc.column + end consecutive_comments.map do |chunk| joined_text = chunk.map { |c| comment_text(c) }.join diff --git a/spec/rubocop/cop/layout/empty_comment_spec.rb b/spec/rubocop/cop/layout/empty_comment_spec.rb index c79cda425d8d..e2eab590ecef 100644 --- a/spec/rubocop/cop/layout/empty_comment_spec.rb +++ b/spec/rubocop/cop/layout/empty_comment_spec.rb @@ -38,6 +38,23 @@ def foo RUBY end + it 'registers an offense and corrects when using an empty comment next to code after comment line' do + expect_offense(<<~RUBY) + # comment + def foo # + ^ Source code comment is empty. + something + end + RUBY + + expect_correction(<<~RUBY) + # comment + def foo + something + end + RUBY + end + it 'does not register an offense when using comment text' do expect_no_offenses(<<~RUBY) # Description of `Foo` class.