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

Style/TernaryParentheses still not necessarily safe autocorrect for defined? keyword #8782

Closed
OmriSama opened this issue Sep 24, 2020 · 1 comment · Fixed by #8797
Closed

Comments

@OmriSama
Copy link

OmriSama commented Sep 24, 2020

I have a project with this line in it:

val = (defined? inc.name) ? inc.name : inc

rubocop -a autocorrects this to:

val = defined? inc.name || inc

Which is clearly not the same thing because one returns a Boolean and another returns the name.

The correct way to fix this, IMO, to make Rubocop happy:

val = defined?(inc.name) ? inc.name : inc

I thought this issue was common enough that I would've been able to find someone who had it before, and I did find:

#3505
#3450

But it seems like this problem has persisted...

Thoughts?

RuboCop version

$ bundle exec rubocop -V
0.91.1 (using Parser 2.7.1.4, rubocop-ast 0.4.2, running on ruby 2.7.1 x86_64-darwin19)
@marcandre
Copy link
Contributor

Thanks for the bug report.

I presume there's another cop involved in the final auto-correction; by itself it corrects to val = defined? inc.name ? inc.name : inc, which is incorrect.

dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Oct 8, 2020
…correction if it is not parenthesized.

Without parens, `defined?` will apply to the entire ternary instead of its previous argument, which makes this autocorrection incorrect.
marcandre pushed a commit that referenced this issue Oct 8, 2020
…ion if it is not parenthesized.

Without parens, `defined?` will apply to the entire ternary instead of its previous argument, which makes this autocorrection incorrect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants