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

Update Style/RedundantCondition cop to detect conditional expressions where the true branch is true and suggest replacing them with a logical OR #13729

Merged
merged 1 commit into from
Feb 26, 2025

Conversation

datpmt
Copy link
Contributor

@datpmt datpmt commented Jan 20, 2025

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 to condition || 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.

#   # bad
#   a.nil? ? true : a
#
#   # good
#   a.nil? || a
#
#   # bad
#   if a.nil?
#     true
#   else
#     a
#   end
#
#   # good
#   a.nil? || a

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic
Copy link
Member

koic commented Jan 20, 2025

Actually, this is the responsibility of Style/RedundantCondition cop. Instead of creating a new cop, please consider extending Style/RedundantCondition cop.

@datpmt datpmt force-pushed the prefer_logical_or branch from c95081c to f8f2916 Compare January 21, 2025 02:56
@datpmt datpmt changed the title Add new Style/PreferLogicalOr cop Update Style/RedundantCondition cop to detect ternary expressions where the true branch is true and suggest replacing them with a logical OR Jan 21, 2025
@datpmt
Copy link
Contributor Author

datpmt commented Jan 21, 2025

@koic I’ve extended the Style/RedundantCondition cop as suggested. The PR is now updated. Please let me know if anything else needs adjustment.

@datpmt datpmt force-pushed the prefer_logical_or branch from f8f2916 to dc6028b Compare January 21, 2025 03:02
@datpmt datpmt force-pushed the prefer_logical_or branch 2 times, most recently from f57a688 to d502655 Compare January 21, 2025 15:49
@datpmt datpmt force-pushed the prefer_logical_or branch from d502655 to 429ca8f Compare January 21, 2025 16:10
Comment on lines 150 to 154
# if a.nil?
# true
# else
# a
# end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# if a.nil?
# true
# else
# a
# end
# if a.nil?
# true
# else
# a
# end

Comment on lines 178 to 182
_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?
Copy link
Member

@koic koic Jan 22, 2025

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.

Suggested change
_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
Copy link
Member

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
Copy link
Member

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

Copy link
Contributor Author

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

@datpmt datpmt force-pushed the prefer_logical_or branch 2 times, most recently from 7cfd6d4 to d733137 Compare January 23, 2025 04:12
@@ -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][])
Copy link
Collaborator

@bbatsov bbatsov Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternary -> conditional

Copy link
Contributor Author

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

@datpmt datpmt force-pushed the prefer_logical_or branch 2 times, most recently from a272b29 to e117648 Compare January 24, 2025 02:06
…ns where the true branch is `true` and suggest replacing them with a logical OR
@datpmt datpmt force-pushed the prefer_logical_or branch from e117648 to 941054e Compare January 24, 2025 02:10
koic added a commit that referenced this pull request Jan 26, 2025
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).
koic added a commit to koic/rubocop that referenced this pull request Jan 27, 2025
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).
koic added a commit to koic/rubocop that referenced this pull request Jan 27, 2025
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).
koic added a commit to koic/rubocop that referenced this pull request Jan 27, 2025
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).
koic added a commit to koic/rubocop that referenced this pull request Jan 27, 2025
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).
koic added a commit to koic/rubocop that referenced this pull request Jan 27, 2025
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).
@datpmt datpmt changed the title Update Style/RedundantCondition cop to detect ternary expressions where the true branch is true and suggest replacing them with a logical OR Update Style/RedundantCondition cop to detect conditional expressions where the true branch is true and suggest replacing them with a logical OR Jan 27, 2025
@bbatsov bbatsov merged commit d506bd6 into rubocop:master Feb 26, 2025
22 of 23 checks passed
@byroot
Copy link
Contributor

byroot commented Feb 26, 2025

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:

     def save(**) # :nodoc:
-      Suppressor.registry[self.class.name] ? true : super
+      Suppressor.registry[self.class.name] || super
     end

Which isn't equivalent, because it used to return true and now might return all sorts of "truthy" object. This is often fine, but may very well not be.

@datpmt
Copy link
Contributor Author

datpmt commented Feb 26, 2025

@byroot thank you,

I realize that condition must be nil or true or false. I missed a step to check this. Please let me know if I missed something.

@Earlopain
Copy link
Contributor

It seems fine for the condition to be a predicate method without safe navigation. The cases you mentionend are already handled via Lint/LiteralAsCondition

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 26, 2025

@byroot Can you please file a ticket about this? We'll address it soon.

@byroot
Copy link
Contributor

byroot commented Feb 26, 2025

Ref: #13913

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants