Sha256: b63d2c2bd2124f635c0b884c6e7dcefb377baef9fa1262015e4cd0c547ebb8fa

Contents?: true

Size: 1.64 KB

Versions: 21

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for useless method definitions, specifically: empty constructors
      # and methods just delegating to `super`.
      #
      # @safety
      #   This cop is unsafe as it can register false positives for cases when an empty
      #   constructor just overrides the parent constructor, which is bad anyway.
      #
      # @example
      #   # bad
      #   def initialize
      #     super
      #   end
      #
      #   def method
      #     super
      #   end
      #
      #   # good - with default arguments
      #   def initialize(x = Object.new)
      #     super
      #   end
      #
      #   # good
      #   def initialize
      #     super
      #     initialize_internals
      #   end
      #
      #   def method(*args)
      #     super(:extra_arg, *args)
      #   end
      #
      class UselessMethodDefinition < Base
        extend AutoCorrector

        MSG = 'Useless method definition detected.'

        def on_def(node)
          return if optional_args?(node)
          return unless delegating?(node.body, node)

          add_offense(node) { |corrector| corrector.remove(node) }
        end
        alias on_defs on_def

        private

        def optional_args?(node)
          node.arguments.any? { |arg| arg.optarg_type? || arg.kwoptarg_type? }
        end

        def delegating?(node, def_node)
          if node&.zsuper_type?
            true
          elsif node&.super_type?
            node.arguments.map(&:source) == def_node.arguments.map(&:source)
          else
            false
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/useless_method_definition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.29.1 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.29.0 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.28.2 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.28.1 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.28.0 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.27.0 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.26.1 lib/rubocop/cop/lint/useless_method_definition.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.26.0 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.25.1 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.25.0 lib/rubocop/cop/lint/useless_method_definition.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.24.1 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.24.0 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.23.0 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.22.3 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.22.2 lib/rubocop/cop/lint/useless_method_definition.rb
rubocop-1.22.1 lib/rubocop/cop/lint/useless_method_definition.rb