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/CaseLikeIf false positive #8891
Comments
|
irb(main):002:0> 1.is_a?(Numeric)
=> true
irb(main):003:0> 1.is_a?(Integer)
=> true So if you have: case var
when is_a?(Numeric)
# command
when is_a?(Integer)
# command
end And you pass I don't exactly understand why but when I switch from if/elsif to switch-case it broke all my unit test when it was using |
Take an example like here https://gitlab.com/noraj/nvd_api/-/blob/master/lib/nvd_feed_api.rb#L94-100 and change if arg_feeds[0].is_a?(String)
elsif arg_feeds[0].is_a?(Array) into case arg_feeds[0]
when is_a?(String)
whenis_a?(Array) And run |
When auto-correction ( diff --git a/lib/nvd_feed_api.rb b/lib/nvd_feed_api.rb
index 0c145f5..addbe2e 100644
--- a/lib/nvd_feed_api.rb
+++ b/lib/nvd_feed_api.rb
@@ -91,12 +91,13 @@ class NVDFeedScraper
if arg_feeds.empty?
return_value = @feeds
elsif arg_feeds.length == 1
- if arg_feeds[0].is_a?(String)
+ case arg_feeds[0]
+ when String
@feeds.each do |feed| # feed is an object
return_value = feed if arg_feeds.include?(feed.name)
end
# if nothing found return nil
- elsif arg_feeds[0].is_a?(Array)
+ when Array
raise 'one of the provided arguments is not a String' unless arg_feeds[0].all? { |x| x.is_a?(String) }
# Sorting CVE can allow us to parse quicker
@@ -165,7 +166,8 @@ class NVDFeedScraper
raise 'no argument provided, 1 or more expected' if arg_cve.empty?
if arg_cve.length == 1
- if arg_cve[0].is_a?(String)
+ case arg_cve[0]
+ when String
raise 'bad CVE name' unless /^CVE-[0-9]{4}-[0-9]{4,}$/i.match?(arg_cve[0])
year = /^CVE-([0-9]{4})-[0-9]{4,}$/i.match(arg_cve[0]).captures[0]
@@ -186,7 +188,7 @@ class NVDFeedScraper
f = feeds(matched_feed)
f.json_pull
return_value = f.cve(arg_cve[0])
- elsif arg_cve[0].is_a?(Array)
+ when Array
(snip) So, the expected syntax is: case var
when Numeric
# command
when Integer
# command
end How about it? |
Correction: case var
when Numeric
# command
when Integer
# command
end
|
So the idea is that |
is equivalent to |
Related issues
#8541, #7736 (comment), and mostly #8508
Expected behavior
No offense when there is a something else than a comparison with a string/int, etc.
This is the same as #8508 where there was a comparison with a class but here it's
is_a?
method rather than the equal operator.Actual behavior
False positive when using a method, eg. is_a? with a class.
Steps to reproduce the problem
RuboCop version
The text was updated successfully, but these errors were encountered: