diff --git a/changelog/new_support_allow_comments_for_empty_else.md b/changelog/new_support_allow_comments_for_empty_else.md new file mode 100644 index 00000000000..888a9f2aca0 --- /dev/null +++ b/changelog/new_support_allow_comments_for_empty_else.md @@ -0,0 +1 @@ +* [#10790](https://github.com/rubocop/rubocop/pull/10790): Support `AllowComments` option for `Style/EmptyElse`. ([@ydah][]) diff --git a/config/default.yml b/config/default.yml index 931d9a89731..a430621397b 100644 --- a/config/default.yml +++ b/config/default.yml @@ -3512,6 +3512,7 @@ Style/EmptyElse: - empty - nil - both + AllowComments: false Style/EmptyLambdaParameter: Description: 'Omit parens for empty lambda parameters.' diff --git a/lib/rubocop/cop/style/empty_else.rb b/lib/rubocop/cop/style/empty_else.rb index 0c9674338e5..0a8d157c33c 100644 --- a/lib/rubocop/cop/style/empty_else.rb +++ b/lib/rubocop/cop/style/empty_else.rb @@ -89,6 +89,41 @@ module Style # if condition # statement # end + # + # @example AllowComments: false (default) + # + # # bad + # if condition + # statement + # else + # # something comment + # nil + # end + # + # # bad + # if condition + # statement + # else + # # something comment + # end + # + # @example AllowComments: true + # + # # good + # if condition + # statement + # else + # # something comment + # nil + # end + # + # # good + # if condition + # statement + # else + # # something comment + # end + # class EmptyElse < Base include OnNormalIfUnless include ConfigurableEnforcedStyle @@ -108,6 +143,8 @@ def on_case(node) private def check(node) + return if cop_config['AllowComments'] && comment_in_else?(node.loc) + empty_check(node) if empty_style? nil_check(node) if nil_style? end diff --git a/spec/rubocop/cop/style/empty_else_spec.rb b/spec/rubocop/cop/style/empty_else_spec.rb index 5456e0d965e..35ce8748e74 100644 --- a/spec/rubocop/cop/style/empty_else_spec.rb +++ b/spec/rubocop/cop/style/empty_else_spec.rb @@ -530,6 +530,174 @@ end end + context 'when `AllowComments: true`' do + let(:config) do + RuboCop::Config.new('Style/EmptyElse' => { + 'AllowComments' => true, + 'EnforcedStyle' => 'both', + 'SupportedStyles' => %w[empty nil both] + }, + 'Style/MissingElse' => missing_else_config) + end + + context 'given an if-statement' do + context 'with not comment and empty else-clause' do + it 'registers an offense' do + expect_offense(<<~RUBY) + if condition + statement + else + ^^^^ Redundant `else`-clause. + end + RUBY + end + end + + context 'with not comment and nil else-clause' do + it 'registers an offense' do + expect_offense(<<~RUBY) + if condition + statement + else + ^^^^ Redundant `else`-clause. + nil + end + RUBY + end + end + + context 'with comment and empty else-clause' do + it "doesn't register an offense" do + expect_no_offenses(<<~RUBY) + if condition + statement + else + # some comment + end + RUBY + end + end + + context 'with comment and nil else-clause' do + it "doesn't register an offense" do + expect_no_offenses(<<~RUBY) + if condition + statement + else + nil # some comment + end + RUBY + end + end + end + + context 'given an unless-statement' do + context 'with not comment and empty else-clause' do + it 'registers an offense' do + expect_offense(<<~RUBY) + unless condition + statement + else + ^^^^ Redundant `else`-clause. + end + RUBY + end + end + + context 'with not comment and nil else-clause' do + it 'registers an offense' do + expect_offense(<<~RUBY) + unless condition + statement + else + ^^^^ Redundant `else`-clause. + nil + end + RUBY + end + end + + context 'with comment and empty else-clause' do + it "doesn't register an offense" do + expect_no_offenses(<<~RUBY) + unless condition + statement + else + # some comment + end + RUBY + end + end + + context 'with comment and nil else-clause' do + it "doesn't register an offense" do + expect_no_offenses(<<~RUBY) + unless condition + statement + else + nil # some comment + end + RUBY + end + end + end + + context 'given a case statement' do + context 'with not comment and empty else-clause' do + it 'registers an offense' do + expect_offense(<<~RUBY) + case a + when condition + statement + else + ^^^^ Redundant `else`-clause. + end + RUBY + end + end + + context 'with not comment and nil else-clause' do + it 'registers an offense' do + expect_offense(<<~RUBY) + case a + when condition + statement + else + ^^^^ Redundant `else`-clause. + nil + end + RUBY + end + end + + context 'with comment and empty else-clause' do + it "doesn't register an offense" do + expect_no_offenses(<<~RUBY) + case a + when condition + statement + else + # some comment + end + RUBY + end + end + + context 'with comment and nil else-clause' do + it "doesn't register an offense" do + expect_no_offenses(<<~RUBY) + case a + when condition + statement + else + nil # some comment + end + RUBY + end + end + end + end + context 'with nested if and case statement' do let(:config) do RuboCop::Config.new('Style/EmptyElse' => {