Sha256: 1e0feab71c85a5d3182b71ed43f6ee689208385ed7cdc9da2a645ef403ac4fc9

Contents?: true

Size: 1.04 KB

Versions: 37

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for unintended or-assignment to a constant.
      #
      # Constants should always be assigned in the same location. And its value
      # should always be the same. If constants are assigned in multiple
      # locations, the result may vary depending on the order of `require`.
      #
      # @safety
      #   This cop is unsafe because code that is already conditionally
      #   assigning a constant may have its behavior changed by autocorrection.
      #
      # @example
      #
      #   # bad
      #   CONST ||= 1
      #
      #   # good
      #   CONST = 1
      #
      class OrAssignmentToConstant < Base
        extend AutoCorrector

        MSG = 'Avoid using or-assignment with constants.'

        def on_or_asgn(node)
          lhs, _rhs = *node
          return unless lhs&.casgn_type?

          add_offense(node.loc.operator) do |corrector|
            corrector.replace(node.loc.operator, '=')
          end
        end
      end
    end
  end
end

Version data entries

37 entries across 33 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/or_assignment_to_constant.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/or_assignment_to_constant.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/or_assignment_to_constant.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/or_assignment_to_constant.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/or_assignment_to_constant.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/or_assignment_to_constant.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/or_assignment_to_constant.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/or_assignment_to_constant.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.47.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.46.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.45.1 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.45.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.44.1 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.44.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.43.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.42.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.41.1 lib/rubocop/cop/lint/or_assignment_to_constant.rb
rubocop-1.41.0 lib/rubocop/cop/lint/or_assignment_to_constant.rb