Skip to content

Commit

Permalink
Fix an error for Naming/ConstantName
Browse files Browse the repository at this point in the history
Fixes rubocop#8422 (comment).

This PR fixes an error for `Naming/ConstantName`
when assigning a constant from an empty branch of `else`.
  • Loading branch information
koic committed May 9, 2023
1 parent b8005a4 commit fa61700
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_naming_constant_name.md
@@ -0,0 +1 @@
* [#11865](https://github.com/rubocop/rubocop/pull/11865): Fix an error for `Naming/ConstantName` when assigning a constant from an empty branch of `else`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/naming/constant_name.rb
Expand Up @@ -76,7 +76,7 @@ def allowed_conditional_expression_on_rhs?(node)
end

def contains_constant?(node)
node.branches.any?(&:const_type?)
node.branches.compact.any?(&:const_type?)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/naming/constant_name_spec.rb
Expand Up @@ -114,6 +114,15 @@
RUBY
end

it 'does not register an offense when assigning a constant from an empty branch of `else`' do
expect_no_offenses(<<~RUBY)
CONST = if condition
foo
else
end
RUBY
end

context 'when a rhs is a conditional expression' do
context 'when conditional branches contain only constants' do
it 'does not check names' do
Expand Down

0 comments on commit fa61700

Please sign in to comment.