Rubocop: Rubocop "-a" changes the meaning of the ternary condition

Created on 13 Apr 2018  路  5Comments  路  Source: rubocop-hq/rubocop

The rewritten line on executing using "-a" removes the parenthesis which changes the meaning of the condition.


Expected behavior

lol.rb:

result = [1, 2, 3].include?(1) ? 'omg' : 'lol'
OR
result = ([1, 2, 3].include? 1) ? 'omg' : 'lol'
puts result

The result of this evaluation is "omg".

Actual behavior

lol.rb:

result = [1, 2, 3].include? 1 ? 'omg' : 'lol'
puts result

The result of this evaluation is 'false'.

Steps to reproduce the problem

  1. Save this as lol.rb:
result = ([1, 2, 3].include? 1) ? 'omg' : 'lol'
puts result
  1. Use this rubocop.yml:
Style/Documentation:
  Enabled: false

Style/StringLiterals:
  Enabled: false

Style/ClassAndModuleChildren:
  Enabled: false

Style/FrozenStringLiteralComment:
  Enabled: false

Metrics/LineLength:
  Max: 120

Metrics/MethodLength:
  Max: 20

Metrics/ClassLength:
  Max: 200

Metrics/ModuleLength:
  Max: 200

Layout/AlignParameters:
  Enabled: false

Metrics/AbcSize:
  Enabled: false

AllCops:
  TargetRubyVersion: 2.3+
  Exclude:
    - '**/*.gemspec'
    - '**/Rakefile'
    - 'lib/sequel/adapters/mariadb.rb'
    - 'Guardfile'

Style/TrailingCommaInArrayLiteral:
  EnforcedStyleForMultiline: comma

Style/TrailingCommaInHashLiteral:
  EnforcedStyleForMultiline: comma

Style/MutableConstant:
  Enabled: false
  1. Run rubocop -a lol.rb

RuboCop version

$ rubocop -V
0.54.0 (using Parser 2.5.1.0, running on ruby 2.4.0 x86_64-darwin16)

(But I had this issue on jruby too)

bug duplicate

All 5 comments

Hm. I fixed this once, in autumn 2016, but changes have been made to the cop since, so I guess it was inadvertently re-introduced.

I've started working on this issue, and made some progress fixing it, but the changes affect the behavior of Style/TernaryParentheses. So before I continue, I'd like to know if that's acceptable. Currently, Style/TernaryParentheses offenses are overlapping with Style/RedundantParentheses. Here's two examples where both cops find an offense:

foo = (bar?) ? a : b
foo = (bar.baz?) ? a : b

On my branch, these offenses are no longer detected by Style/TernaryParentheses. Instead, it leaves finding and correcting them to Style/RedundantParentheses. So both examples remain offenses and are still autocorrectable.

Is that ok? Or should I attempt to preserve current behavior in the bugfix?

@svendittmer I think parentheses are unnecessary for method calls without arguments. The following behaviors are the same.

% ruby -e "p (nil.nil?) ? 'hi' : 'bye'" # => "hi"
% ruby -e "p nil.nil? ? 'hi' : 'bye'" # => "hi"

In this case without parentheses, offenses are not registered with Style/TernaryParentheses cop and Style/RedundantParentheses cop.

This issue duplicates #4460.
Please note the test case of https://github.com/rubocop-hq/rubocop/issues/4460#issuecomment-362282999. This case is not a method call but a conditional expression.

Closing as duplicate. Please follow #4460 for updates.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benoittgt picture benoittgt  路  3Comments

mikegee picture mikegee  路  3Comments

lepieru picture lepieru  路  3Comments

bbatsov picture bbatsov  路  3Comments

herwinw picture herwinw  路  3Comments