Skip to content

Commit

Permalink
Optimize and simplify MutableConstant Cop
Browse files Browse the repository at this point in the history
In case of `CONST ||= expr`, the analysis was done twice, once with the correct value, but another time with `nil`.
Moreover, checks for the `nil` case were done where they didn't belong.
Finally, all `@memo ||= expr` or similar would trigger a callback for nothing.
  • Loading branch information
marcandre authored and mergify[bot] committed Jul 14, 2021
1 parent d241f43 commit 3e1cc1a
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/rubocop/cop/style/mutable_constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ class MutableConstant < Base

def on_casgn(node)
_scope, _const_name, value = *node
on_assignment(value)
end
if value.nil? # This is only the case for `CONST += ...` or similarg66
parent = node.parent
return unless parent.or_asgn_type? # We only care about `CONST ||= ...`

def on_or_asgn(node)
lhs, value = *node

return unless lhs&.casgn_type?
value = parent.children.last
end

on_assignment(value)
end
Expand Down Expand Up @@ -118,14 +117,13 @@ def autocorrect(corrector, node)
end

def mutable_literal?(value)
return false if value.nil?
return false if frozen_regexp_or_range_literals?(value)

value.mutable_literal?
end

def immutable_literal?(node)
node.nil? || frozen_regexp_or_range_literals?(node) || node.immutable_literal?
frozen_regexp_or_range_literals?(node) || node.immutable_literal?
end

def frozen_string_literal?(node)
Expand Down

0 comments on commit 3e1cc1a

Please sign in to comment.