Sha256: 0aa7abff5560c2371d96f970a88f63527738071daf0dcc57e2a9a37a41fe6d76

Contents?: true

Size: 1.75 KB

Versions: 6789

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for the use of a return with a value in a context
      # where the value will be ignored. (initialize and setter methods)
      #
      # @example
      #
      #   # bad
      #   def initialize
      #     foo
      #     return :qux if bar?
      #     baz
      #   end
      #
      #   def foo=(bar)
      #     return 42
      #   end
      #
      # @example
      #
      #   # good
      #   def initialize
      #     foo
      #     return if bar?
      #     baz
      #   end
      #
      #   def foo=(bar)
      #     return
      #   end
      class ReturnInVoidContext < Cop
        MSG = 'Do not return a value in `%<method>s`.'.freeze

        def on_return(return_node)
          return unless return_node.descendants.any?

          context_node = non_void_context(return_node)

          return unless context_node && context_node.def_type?

          method_name = method_name(context_node)

          return unless method_name && void_context_method?(method_name)

          add_offense(return_node,
                      location: :keyword,
                      message: format(message, method: method_name))
        end

        private

        def non_void_context(return_node)
          return_node.each_ancestor(:block, :def, :defs).first
        end

        def method_name(context_node)
          context_node.children.first
        end

        def void_context_method?(method_name)
          method_name == :initialize || setter_method?(method_name)
        end

        def setter_method?(method_name)
          method_name.to_s.end_with?('=') &&
            !AST::Node::COMPARISON_OPERATORS.include?(method_name)
        end
      end
    end
  end
end

Version data entries

6,789 entries across 6,783 versions & 25 rubygems

Version Path
cybrid_api_organization_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_bank_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_organization_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_bank_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_organization_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_bank_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_organization_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_organization_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_bank_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_organization_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_bank_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_bank_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_id_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb
cybrid_api_organization_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/return_in_void_context.rb