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

False positive with Style/IfWithBooleanLiteralBranches and nonzero? #9459

Closed
erwanlr opened this issue Jan 29, 2021 · 0 comments · Fixed by #9461
Closed

False positive with Style/IfWithBooleanLiteralBranches and nonzero? #9459

erwanlr opened this issue Jan 29, 2021 · 0 comments · Fixed by #9461

Comments

@erwanlr
Copy link

erwanlr commented Jan 29, 2021

The nonzero? method returns self if num is not zero, nil otherwise, and not a boolean. Causing false positive with the Style/IfWithBooleanLiteralBranches cop with code such as val.nonzero? ? true : false


Expected behavior

The cop should not flag statements such as val.nonzero? ? true : false

Actual behavior

 Style/IfWithBooleanLiteralBranches: Remove redundant ternary operator with boolean literal branches.
      NS::Browser.get(url(path)).code.nonzero? ? true : false

Steps to reproduce the problem

Run the cop against code like val.nonzero? ? true : false , for example https://github.com/wpscanteam/CMSScanner/blob/master/lib/cms_scanner/web_site.rb#L74

RuboCop version

1.9.0 (using Parser 3.0.0.0, rubocop-ast 1.4.1, running on ruby 2.7.2 x86_64-linux)
  - rubocop-performance 1.9.2
koic added a commit to koic/rubocop that referenced this issue Jan 29, 2021
…nLiteralBranches`

Fixes rubocop#9459.

This PR adds `AllowedMethods` option to `Style/IfWithBooleanLiteralBranches` and
set `nonzero?` as default value.
bbatsov pushed a commit that referenced this issue Jan 30, 2021
…lBranches`

Fixes #9459.

This PR adds `AllowedMethods` option to `Style/IfWithBooleanLiteralBranches` and
set `nonzero?` as default value.
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 a pull request may close this issue.

1 participant