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
The cop Style/CollectionCompact has unsafe autocorrect which changes calls to Hash#delete_if to Hash#compact.
This is incorrect because Hash#delete_if modifies the object and Hash#compact does not.
The correct replacement would be Hash#compact! (the ! version).
The same applies to Array#delete_if erroneously being converted to Array#compact.
My reproduction below only shows the Hash case. I have not gone looking for other Array / Hash methods that modify the receiver.
Warning: Array#delete_if returns self after modifying the object. Array#compact! sometimes returns nil:
`
$ ri --no-pager Array#compact!
Array#compact!
(from ruby core)
array.compact! -> self or nil
Removes all nil elements from self.
Returns self if any elements removed, otherwise nil. So maybe it is not as simple as just changingcompacttocompact!` in the replacement.
Expected behavior
I expect a call to Hash#delete_if to be replaced with Hash#compact! (the ! version)
Actual behavior
Hash#delete_if is replaced with Hash#compact (the non ! version)
The cop Style/CollectionCompact has unsafe autocorrect which changes calls to
Hash#delete_if
toHash#compact
.This is incorrect because
Hash#delete_if
modifies the object andHash#compact
does not.The correct replacement would be
Hash#compact!
(the ! version).The same applies to
Array#delete_if
erroneously being converted toArray#compact
.My reproduction below only shows the Hash case. I have not gone looking for other Array / Hash methods that modify the receiver.
Warning:
Array#delete_if
returnsself
after modifying the object.Array#compact!
sometimes returns nil:`
$ ri --no-pager Array#compact!
Array#compact!
(from ruby core)
array.compact! -> self or nil
Removes all nil elements from self.
Returns self if any elements removed, otherwise nil.
So maybe it is not as simple as just changing
compactto
compact!` in the replacement.Expected behavior
I expect a call to
Hash#delete_if
to be replaced withHash#compact!
(the ! version)Actual behavior
Hash#delete_if
is replaced withHash#compact
(the non ! version)Steps to reproduce the problem
Example input:
This prints the following:
After letting rubocop correct the source file, the output has changed:
RuboCop version
The text was updated successfully, but these errors were encountered: