class RuboCop::Cop::Security::CompoundHash
end
[@foo, @bar].hash
def hash
# good
end
@foo ^ @bar
def hash
# bad
@example
value, however this is inadvisable anyway.
This cop may be unsafe if the application logic depends on the hash
@safety
depending on the use case.
Delegating to ‘Array#hash` is clearer and safer, although it might be slower
performance or security concerns if they are prone to collisions.
when there are many values. Poor implementations may also introduce
Manually combining hashes is error prone and hard to follow, especially
values using custom logic instead of delegating to `Array#hash`.
Checks for implementations of the `hash` method which combine
def contained_in_hash_method?(node, &block)
def contained_in_hash_method?(node, &block) node.each_ancestor.any? do |ancestor| hash_method_definition?(ancestor, &block) end end
def on_send(node)
def on_send(node) outer_bad_hash_combinator?(node) do contained_in_hash_method?(node) do add_offense(node, message: COMBINATOR_IN_HASH_MSG) end end monuple_hash?(node) do add_offense(node, message: MONUPLE_HASH_MSG) end redundant_hash?(node) do add_offense(node, message: REDUNDANT_HASH_MSG) end end
def outer_bad_hash_combinator?(node)
def outer_bad_hash_combinator?(node) bad_hash_combinator?(node) do yield true if node.each_ancestor.none? { |ancestor| bad_hash_combinator?(ancestor) } end end