You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wrong replacement of a block with 2 actual arguments by SymbolProc causes an error with wrong number of arguments
slice_when{ |x| x.nil? } => slice_when(&:nil?)
=> nil? ArgumentError (wrong number of arguments (given 1, expected 0))
Expected behavior
slice_when{ |x| x.nil? } is incorrectly used because of two block variable should be specified here instead of one. Good behavior is to give recommendation that other method like slice_after should be used.
Actual behavior
C: Style/SymbolProc: Pass &:nil? as an argument to slice_when instead of a block.
[1, nil, 2, 3, nil, 4, 5, 6].slice_when{|x| x.nil?}.map(&:compact)
^^^^^^^^^^^^
Auto-correct produces unworkable code:
[1,nil,2,3,nil,4,5,6].slice_when(&:nil?).map(&:compact)#…#>> ArgumentError (wrong number of arguments (given 1, expected 0))
The root cause of this problem is that the original code does not properly reveal the yielded arguments, e.g.:
slice_when{ |x,_| x.nil?}
Because the second argument is implicitly discarded, RuboCop gets the signature wrong. Unfortunately it's not realistic to keep a lookup table of the block arity of all standard library methods. Even if we did, the above would still be a false positive for user implemented methods.
What we can do is mark the auto-correct of Style/SymbolProc as unsafe, which will make it disabled by default.
Wrong replacement of a block with 2 actual arguments by SymbolProc causes an error with wrong number of arguments
Expected behavior
slice_when{ |x| x.nil? }
is incorrectly used because of two block variable should be specified here instead of one. Good behavior is to give recommendation that other method likeslice_after
should be used.Actual behavior
Auto-correct produces unworkable code:
Steps to reproduce the problem
RuboCop version
The text was updated successfully, but these errors were encountered: