-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Update Style/RedundantCondition
cop to detect conditional expressions where the true branch is true
and suggest replacing them with a logical OR
#13729
Conversation
Actually, this is the responsibility of |
c95081c
to
f8f2916
Compare
Style/PreferLogicalOr
copStyle/RedundantCondition
cop to detect ternary expressions where the true branch is true
and suggest replacing them with a logical OR
@koic I’ve extended the |
f8f2916
to
dc6028b
Compare
f57a688
to
d502655
Compare
d502655
to
429ca8f
Compare
# if a.nil? | ||
# true | ||
# else | ||
# a | ||
# end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# if a.nil? | |
# true | |
# else | |
# a | |
# end | |
# if a.nil? | |
# true | |
# else | |
# a | |
# end |
_condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring | ||
|
||
return false unless if_branch && else_branch | ||
|
||
if_branch.true_type? && !else_branch.true_type? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can likely be simplified as follows.
_condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring | |
return false unless if_branch && else_branch | |
if_branch.true_type? && !else_branch.true_type? | |
node.if_branch&.true_type? && node.else_branch && !node.else_branch.true_type? |
RUBY | ||
end | ||
|
||
context 'does not register an offense' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This text belongs in it
, not context
. Can you update the tests added in this PR by referring to the existing tests?
b | ||
end | ||
RUBY | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for cases where each branch does not exist.
expect_no_offenses(<<~RUBY)
if a.zero?
true
else
end
RUBY
and
expect_no_offenses(<<~RUBY)
if a.zero?
else
a
end
RUBY
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @koic, I updated the PR
7cfd6d4
to
d733137
Compare
@@ -0,0 +1 @@ | |||
* [#13729](https://github.com/rubocop/rubocop/pull/13729): Update `Style/RedundantCondition` cop to detect ternary expressions where the true branch is `true` and suggest replacing them with a logical OR. ([@datpmt][]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ternary
-> conditional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbatsov please help me review the PR, thank you
a272b29
to
e117648
Compare
…ns where the true branch is `true` and suggest replacing them with a logical OR
e117648
to
941054e
Compare
This PR updates `EnforcedStyle` of `RSpec/ExampleWithoutDescription` from `always_allow` (default) to `single_line_only`. This repository typically does not use the following style. ```ruby it do ... end ``` In this case, descriptions are always written in `it`. Follow up #13729 (comment).
This PR updates `EnforcedStyle` of `RSpec/ExampleWithoutDescription` from `always_allow` (default) to `single_line_only` for development. This repository typically does not use the following style. ```ruby it do ... end ``` In this case, descriptions are always written in `it`. Follow up rubocop#13729 (comment).
This PR updates `EnforcedStyle` of `RSpec/ExampleWithoutDescription` from `always_allow` (default) to `single_line_only` for development. This repository typically does not use the following style. ```ruby it do ... end ``` In this case, descriptions are always written in `it`. Follow up rubocop#13729 (comment).
This PR updates `EnforcedStyle` of `RSpec/ExampleWithoutDescription` from `always_allow` (default) to `single_line_only` for development. This repository typically does not use the following style. ```ruby it do ... end ``` In this case, descriptions are always written in `it`. Follow up rubocop#13729 (comment).
This PR updates `EnforcedStyle` of `RSpec/ExampleWithoutDescription` from `always_allow` (default) to `single_line_only` for development. This repository typically does not use the following style. ```ruby it do ... end ``` In this case, descriptions are always written in `it`. Follow up rubocop#13729 (comment).
This PR updates `EnforcedStyle` of `RSpec/ExampleWithoutDescription` from `always_allow` (default) to `single_line_only` for development. This repository typically does not use the following style. ```ruby it do ... end ``` In this case, descriptions are always written in `it`. Follow up rubocop#13729 (comment).
Style/RedundantCondition
cop to detect ternary expressions where the true branch is true
and suggest replacing them with a logical ORStyle/RedundantCondition
cop to detect conditional expressions where the true branch is true
and suggest replacing them with a logical OR
This cop started firing on Rails master (our fault rubocop wasn't locked in Gemfile) and I must say I'm a bit puzzled by some of its suggestions, maybe the cop isn't quite restrictive enough: e.g. activerecord/lib/active_record/suppressor.rb:56:44: C: [Correctable] Style/RedundantCondition: Use double pipes || instead.
Suppressor.registry[self.class.name] ? true : super
^^^^^^^^ It I use the auto-corrector it changes it for:
Which isn't equivalent, because it used to return |
@byroot thank you, I realize that |
It seems fine for the condition to be a predicate method without safe navigation. The cases you mentionend are already handled via |
@byroot Can you please file a ticket about this? We'll address it soon. |
Ref: #13913 |
Checks for ternary expressions that use
true
as the true branch and suggest replacing them with a logical OR (||
) expression.For example, a ternary like
condition ? true : variable
is considered redundant because it can be simplified tocondition || variable
. The cop promotes the use of more concise and readable code by encouraging the use of logical OR in such cases, improving both clarity and maintainability.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.