Skip to content

Commit

Permalink
Merge pull request #7913 from koic/false_positive_for_lint_literal_as…
Browse files Browse the repository at this point in the history
…_condition

Fix a false positive for `Lint/LiteralAsCondition`
  • Loading branch information
koic committed May 9, 2020
2 parents 6baec8f + 4244b24 commit db6d1d0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
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

0 comments on commit db6d1d0

Please sign in to comment.