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
Lint/EmptyWhen: false positive with range #10789
Comments
Thank you for your feedback. # frozen_string_literal: true
var = 12
case var
when 10
:a
when 50...
:c
end The above code appears to generate an ArgumentError when executed. bundle exec ruby test.rb
test.rb:8: warning: ... at EOL, should be parenthesized?
test.rb:8:in `<main>': bad value for range (ArgumentError)
ruby --version
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin21] I think the following rewrite would result in the intended behavior. How about it? # frozen_string_literal: true
var = 12
case var
when 10
:a
when (50...)
:c
end |
Yep. This is a range's specification. The following is the behavior when line break is used on the endless range without parentheses. % cat range.rb
p 1...
42
In other words, the reproduction code is interpreted as follows. # frozen_string_literal: true
var = 12
case var
when 10
:a
when (50...
:c)
end So, this issue case should add parentheses to the expected endless range, not false positives. when (50...)
:c |
thanks for your responses. Yes, that code generates an ArgumentError.
I thought this can be another issue after rethinking, so I created #10791 for this. |
First, I've opened #10792 to detect this issue case. |
## Context It emulates the following Ruby warning. ```console % cat example.rb 1... 2 ``` ```consle % ruby example.rb example.rb:1: warning: ... at EOL, should be parenthesized? ``` So, this cop will detect case like rubocop#10789. ## Summary It checks that a range literal is enclosed in parentheses when the end of the range is at a line break. ### example ```ruby # bad - Represents `(1..42)`, not endless range. 1.. 42 # good - It's incompatible, but your intentions when using endless range may be: (1..) 42 # good 1..42 # good (1..42) # good (1.. 42) ``` NOTE: The following is maybe intended for `(42..)`. But, compatible is `42..do_something`. So, this cop does not provide autocorrection because it is left to user. ```ruby case condition when 42.. do_something end ```
## Context It emulates the following Ruby warning. ```console % cat example.rb 1... 2 ``` ```consle % ruby example.rb example.rb:1: warning: ... at EOL, should be parenthesized? ``` So, this cop will detect case like #10789. ## Summary It checks that a range literal is enclosed in parentheses when the end of the range is at a line break. ### example ```ruby # bad - Represents `(1..42)`, not endless range. 1.. 42 # good - It's incompatible, but your intentions when using endless range may be: (1..) 42 # good 1..42 # good (1..42) # good (1.. 42) ``` NOTE: The following is maybe intended for `(42..)`. But, compatible is `42..do_something`. So, this cop does not provide autocorrection because it is left to user. ```ruby case condition when 42.. do_something end ```
Expected behavior
RuboCop does not report an error.
Actual behavior
RuboCop does report an error that seemed to be invalid.
Steps to reproduce the problem
place these files then
bundle install && bundle exec rubocop
RuboCop version
The text was updated successfully, but these errors were encountered: