-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add option to only enforce map/block correction of Perfomance/Sum #204
Comments
@dgollahon Could you please give a code example for this. |
There are three general cases i've run across:
Here is a quick example: [[1, 2].to_set, [1,3].to_set].reduce(:+) # This gets flagged by Performance/Sum # => #<Set: {1, 2, 3}>
# But I can't do...
[[1, 2].to_set, [1,3].to_set].sum # => TypeError: Set can't be coerced into Integer I could do: [[1, 2].to_set, [1,3].to_set].sum(Set.new) of course, but I don't find that to be more readable and I would expect it to be less performant (though I have not benchmarked it). Essentially, I only care about the "obvious" cases where we can have a higher confidence that it actually makes sense to use |
It turns out that the documentation of said cop actually discusses this case: rubocop-performance/lib/rubocop/cop/performance/sum.rb Lines 11 to 28 in b59c260
So, the intention of the author is to set Nevertheless, I have a different solution to offer: The issues arise because the method
Therefore, the Ruby way ™️ to solve this is "more telling aliases": # offenses
['a', 'b'].reduce(:+)
[[1, 2].to_set, [1,3].to_set].reduce(:+)
# no offense
['a', 'b'].reduce(:concat)
[[1, 2].to_set, [1,3].to_set].reduce(:union) |
I did notice that documentation today, although I don't recall it discussing it when I opened this issue. I wonder if it was there then too? Sure, that's definitely an option and I think those example cases are a bit more readable--that said it still feels wrong/noisy for this cop to be complaining and I would personally prefer it to be more conservative. I also still have a few custom classes where That said, this is not a huge issue, it's just an annoyance. I think that it should be viewed as a false positive regardless of whether or not there is a neater way to express what I want to do though. |
Well, I think you do have a point there. To sum it up: When > [].reduce(:+)
# => nil
> ['a'].reduce(:+)
# => "a"
> [].sum
# => 0
> ['a'].sum
# => in `+': String can't be coerced into Integer (TypeError) And we found:
|
…lse positives
…lse positives Some codebases may contain non-numeric classes which implement a `:+` method. If `a`, `b` and `c` are objects of such a class, then certain corrections do not apply: ```ruby [a, b].reduce(c, :+) # is equivalent to [a, b].sum(c) [a, b].map(&:to_i).sum # is equivalent to [a, b].sum(&:to_i) [a, b].reduce(:+) # works [a, b].sum # raises TypeError ``` For users who wish to only register offenses where auto-correction is unproblematic, we add the option `OnlySumOrWithInitialValue` to the `Performance/Sum` cop. The option is disabled by default, so standard behavior is preserved, and can be activated by setting `OnlySumOrWithInitialValue: true`.
Yup, good summary! |
[Fix #204] Add `Performance/Sum` option to ignore potential false positives
@dgollahon Documentation will be extended in #270. Option |
Is your feature request related to a problem? Please describe.
I have found the
Performance/Sum
cop useful in several cases but the false positive rate is too high for me because there are various cases in my codebase(s) that usereduce(:+)
on objects wheresum
is not applicable. In cases where I'm already using#sum
, however, there were several opportunities to use the block form which is simpler and faster. I would like to be able to have these detected without enabling the entire cop.Describe the solution you'd like
An option that just enforces using the block form of
#sum
when it can be detected:Describe alternatives you've considered
This may be too niche of an option but I think it would be helpful since I will likely disable this cop otherwise because the false positive rate for me is too high. There are also a couple of cases where I might be able to use the init parameter to
#sum
but there are cases I have where I do not prefer writing the code that way as it is not necessary or helpful AFAICT.Additional context
N/A
The text was updated successfully, but these errors were encountered: