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 a false positive for Lint/LiteralAsCondition #7913

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.md
Expand Up @@ -22,6 +22,7 @@
* [#7899](https://github.com/rubocop-hq/rubocop/issues/7899): Fix an infinite loop error for `Layout/SpaceAroundOperators` with `Layout/ExtraSpacing` when using `ForceEqualSignAlignment: true`. ([@koic][])
* [#7885](https://github.com/rubocop-hq/rubocop/issues/7885): Fix `Style/IfUnlessModifier` logic when tabs are used for indentation. ([@jonas054][])
* [#7909](https://github.com/rubocop-hq/rubocop/pull/7909): Fix a false positive for `Lint/ParenthesesAsGroupedExpression` when using an intended grouped parentheses. ([@koic][])
* [#7913](https://github.com/rubocop-hq/rubocop/pull/7913): Fix a false positive for `Lint/LiteralAsCondition` when using `true` literal in `while` and similar cases. ([@koic][])

### Changes

Expand Down
23 changes: 10 additions & 13 deletions lib/rubocop/cop/lint/literal_as_condition.rb
Expand Up @@ -10,26 +10,25 @@ module Lint
# @example
#
# # bad
#
# if 20
# do_something
# end
#
# @example
#
# # bad
#
# if some_var && true
# do_something
# end
#
# @example
#
# # good
#
# if some_var && some_condition
# do_something
# end
#
# # good
# # When using a boolean value for an infinite loop.
# while true
# break if condition
# end
class LiteralAsCondition < Cop
MSG = 'Literal `%<literal>s` appeared as a condition.'

Expand All @@ -38,20 +37,18 @@ def on_if(node)
end

def on_while(node)
check_for_literal(node)
end
return if condition(node).true_type?

def on_while_post(node)
check_for_literal(node)
end
alias on_while_post on_while

def on_until(node)
check_for_literal(node)
end
return if condition(node).false_type?

def on_until_post(node)
check_for_literal(node)
end
alias on_until_post on_until

def on_case(case_node)
if case_node.condition
Expand Down
15 changes: 8 additions & 7 deletions manual/cops_lint.md
Expand Up @@ -993,24 +993,25 @@ if/while/until.

```ruby
# bad

if 20
do_something
end
```
```ruby
# bad

# bad
if some_var && true
do_something
end
```
```ruby
# good

# good
if some_var && some_condition
do_something
end

# good
# When using a boolean value for an infinite loop.
while true
break if condition
end
```

## Lint/LiteralInInterpolation
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/lint/literal_as_condition_spec.rb
Expand Up @@ -176,4 +176,36 @@
end
RUBY
end

it 'accepts `true` literal in `while`' do
expect_no_offenses(<<~RUBY)
while true
break if condition
end
RUBY
end

it 'accepts `true` literal in post-loop `while`' do
expect_no_offenses(<<~RUBY)
begin
break if condition
end while true
RUBY
end

it 'accepts `false` literal in `until`' do
expect_no_offenses(<<~RUBY)
until false
break if condition
end
RUBY
end

it 'accepts `false` literal in post-loop `until`' do
expect_no_offenses(<<~RUBY)
begin
break if condition
end until false
RUBY
end
end