Sha256: 611a60508ae7c2a075025a14d677d6b5e3beed6742c5a61e2ce272ca7221f4a9

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # This cop checks for unused method arguments.
      #
      # @example
      #
      #   def some_method(used, unused, _unused_but_allowed)
      #     puts used
      #   end
      class UnusedMethodArgument < Cop
        def join_force?(force_class)
          force_class == VariableForce
        end

        def after_leaving_scope(scope, _variable_table)
          scope.variables.each_value do |variable|
            check_argument(variable)
          end
        end

        def check_argument(variable)
          return unless variable.method_argument?
          return if variable.name.to_s.start_with?('_')
          return if variable.referenced?

          message = message(variable)
          add_offense(variable.declaration_node, :name, message)
        end

        def message(variable)
          message = "Unused method argument - `#{variable.name}`. " \
                    "If it's necessary, use `_` or `_#{variable.name}` " \
                    "as an argument name to indicate that it won't be used."

          scope = variable.scope
          all_arguments = scope.variables.each_value.select(&:method_argument?)

          if all_arguments.none?(&:referenced?)
            message << " You can also write as `#{scope.name}(*)` " \
                       'if you want the method to accept any arguments ' \
                       "but don't care about them."
          end

          message
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/lint/unused_method_argument.rb