Sha256: 5236b3b20c96b508ace1ba974a38c811fc001bb519087eac196f7f4ff5440c6d

Contents?: true

Size: 1.27 KB

Versions: 181

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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 < Base
        MSG = 'Do not return a value in `%<method>s`.'

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

          context_node = non_void_context(return_node)

          return unless context_node&.def_type?
          return unless context_node&.void_context?

          add_offense(
            return_node.loc.keyword,
            message: format(message, method: context_node.method_name)
          )
        end

        private

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

Version data entries

181 entries across 174 versions & 17 rubygems

Version Path
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rubocop-1.59.0/lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.59.0 lib/rubocop/cop/lint/return_in_void_context.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/return_in_void_context.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/return_in_void_context.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/return_in_void_context.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/return_in_void_context.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/return_in_void_context.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.58.0 lib/rubocop/cop/lint/return_in_void_context.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/return_in_void_context.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/return_in_void_context.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/return_in_void_context.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.57.2 lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.57.1 lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.57.0 lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.56.4 lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.56.3 lib/rubocop/cop/lint/return_in_void_context.rb
rubocop-1.56.2 lib/rubocop/cop/lint/return_in_void_context.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/return_in_void_context.rb