Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Naming/ConstantName #11865

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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