The rewritten line on executing using "-a" removes the parenthesis which changes the meaning of the condition.
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".
lol.rb:
result = [1, 2, 3].include? 1 ? 'omg' : 'lol'
puts result
The result of this evaluation is 'false'.
lol.rb:result = ([1, 2, 3].include? 1) ? 'omg' : 'lol'
puts result
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
rubocop -a lol.rb$ 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)
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.