123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Lint::Void

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/lint/void.rb

Overview

Checks for operators, variables, literals, lambda, proc and nonmutating methods used in void context.

each blocks are allowed to prevent false positives. For example, the expression inside the each block below. It’s not void, especially when the receiver is an Enumerator:

enumerator = [1, 2, 3].filter
enumerator.each { |item| item >= 2 } #=> [2, 3]

Examples:

CheckForMethodsWithNoSideEffects: false (default)

# bad
def some_method
  some_num * 10
  do_something
end

def some_method(some_var)
  some_var
  do_something
end

CheckForMethodsWithNoSideEffects: true

# bad
def some_method(some_array)
  some_array.sort
  do_something(some_array)
end

# good
def some_method
  do_something
  some_num * 10
end

def some_method(some_var)
  do_something
  some_var
end

def some_method(some_array)
  some_array.sort!
  do_something(some_array)
end

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RangeHelp - Included

#add_range, #column_offset_between,
#contents_range

A range containing only the contents of a literal with delimiters (e.g.

#directions,
#effective_column

Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value.

#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#all_keys_entirely_literal?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 259

def all_keys_entirely_literal?(node)
  node.each_key.all? { |key| entirely_literal?(key) }
end

#all_values_entirely_literal?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 263

def all_values_entirely_literal?(node)
  node.each_value.all? { |value| entirely_literal?(value) }
end

#autocorrect_nonmutating_send(corrector, node, suggestion) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 237

def autocorrect_nonmutating_send(corrector, node, suggestion)
  send_node = if node.send_type?
                node
              else
                node.send_node
              end
  corrector.replace(send_node.loc.selector, suggestion)
end

#autocorrect_void_expression(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 231

def autocorrect_void_expression(corrector, node)
  return if node.parent.if_type? && node.parent.modifier_form?

  corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
end

#autocorrect_void_op(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 218

def autocorrect_void_op(corrector, node)
  if node.arguments.empty?
    corrector.replace(node, node.receiver.source)
  else
    corrector.remove(node.loc.dot) if node.loc.dot
    corrector.replace(
      range_with_surrounding_space(range: node.loc.selector, side: :both,
                                   newlines: false),
      "\n"
    )
  end
end

#check_begin(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 101

def check_begin(node)
  expressions = *node
  expressions.pop unless in_void_context?(node)
  expressions.each do |expr|
    check_void_op(expr) do
      block_node = node.each_ancestor(:block).first

      block_node&.method?(:each)
    end

    check_expression(expr)
  end
end

#check_ensure(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 198

def check_ensure(node)
  return unless (body = node.branch)
  # NOTE: the `begin` node case is already handled via `on_begin`
  return if body.begin_type?

  check_void_op(body) do
    block_node = node.each_ancestor(:block).first
    block_node&.method?(:each)
  end

  check_expression(body)
end

#check_expression(expr) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 115

def check_expression(expr)
  expr = expr.body if expr.if_type? && expr.modifier_form?

  check_literal(expr)
  check_var(expr)
  check_self(expr)
  check_void_expression(expr)
  return unless cop_config['CheckForMethodsWithNoSideEffects']

  check_nonmutating(expr)
end

#check_literal(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 156

def check_literal(node)
  return if !entirely_literal?(node) || node.xstr_type? || node.range_type?

  add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
    autocorrect_void_expression(corrector, node)
  end
end

#check_nonmutating(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 180

def check_nonmutating(node)
  return if !node.send_type? && !node.block_type? && !node.numblock_type?

  method_name = node.method_name
  return unless NONMUTATING_METHODS.include?(method_name)

  suggestion = if METHODS_REPLACEABLE_BY_EACH.include?(method_name)
                 'each'
               else
                 "#{method_name}!"
               end
  add_offense(node,
              message: format(NONMUTATING_MSG, method: method_name,
                                               suggest: suggestion)) do |corrector|
    autocorrect_nonmutating_send(corrector, node, suggestion)
  end
end

#check_self(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 164

def check_self(node)
  return unless node.self_type?

  add_offense(node, message: SELF_MSG) do |corrector|
    autocorrect_void_expression(corrector, node)
  end
end

#check_var(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 138

def check_var(node)
  return unless node.variable? || node.const_type?

  if node.const_type?
    template = node.special_keyword? ? VAR_MSG : CONST_MSG

    offense_range = node
    message = format(template, var: node.source)
  else
    offense_range = node.loc.name
    message = format(VAR_MSG, var: node.loc.name.source)
  end

  add_offense(offense_range, message: message) do |corrector|
    autocorrect_void_expression(corrector, node)
  end
end

#check_void_expression(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 172

def check_void_expression(node)
  return unless node.defined_type? || node.lambda_or_proc?

  add_offense(node, message: format(EXPRESSION_MSG, expression: node.source)) do |corrector|
    autocorrect_void_expression(corrector, node)
  end
end

#check_void_op(node, &block) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 127

def check_void_op(node, &block)
  node = node.children.first while node.begin_type?
  return unless node.call_type? && OPERATORS.include?(node.method_name)
  return if block && yield(node)

  add_offense(node.loc.selector,
              message: format(OP_MSG, op: node.method_name)) do |corrector|
    autocorrect_void_op(corrector, node)
  end
end

#entirely_literal?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 246

def entirely_literal?(node)
  case node.type
  when :array
    all_values_entirely_literal?(node)
  when :hash
    all_keys_entirely_literal?(node) && all_values_entirely_literal?(node)
  when :send, :csend
    node.method?(:freeze) && node.receiver && entirely_literal?(node.receiver)
  else
    node.literal?
  end
end

#in_void_context?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 211

def in_void_context?(node)
  parent = node.parent
  return false unless parent && parent.children.last == node

  parent.respond_to?(:void_context?) && parent.void_context?
end

#on_begin(node) Also known as: #on_kwbegin

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 90

def on_begin(node)
  check_begin(node)
end

#on_block(node) Also known as: #on_numblock

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 81

def on_block(node)
  return unless node.body && !node.body.begin_type?
  return unless in_void_context?(node.body)

  check_void_op(node.body) { node.method?(:each) }
  check_expression(node.body)
end

#on_ensure(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 95

def on_ensure(node)
  check_ensure(node)
end

#on_kwbegin(node)

Alias for #on_begin.

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 93

alias on_kwbegin on_begin

#on_numblock(node)

Alias for #on_block.

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/void.rb', line 88

alias on_numblock on_block