Class: RuboCop::Cop::Lint::Void
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:
[source,ruby]
enumerator = [1, 2, 3].filter
enumerator.each { |item| item >= 2 } #=> [2, 3]
NOTE: The last expression in an assignment method definition such as def foo=(arg)
is not flagged. Ruby discards it (the method returns its argument), but the method can
still be called directly and its return value relied upon, so flagging it would be a
false positive for this lint.
NOTE: A constant used in a void context is flagged but not autocorrected, since referencing a constant can trigger autoloading side effects (e.g. forcing a file to load before a monkey-patch), so removing it may change behavior.
Constant Summary collapse
- OP_MSG =
'Operator `%<op>s` used in void context.'- VAR_MSG =
'Variable `%<var>s` used in void context.'- CONST_MSG =
'Constant `%<var>s` used in void context.'- LIT_MSG =
'Literal `%<lit>s` used in void context.'- SELF_MSG =
'`self` used in void context.'- EXPRESSION_MSG =
'`%<expression>s` used in void context.'- NONMUTATING_MSG =
'Method `#%<method>s` used in void context. Did you mean `#%<suggest>s`?'- BINARY_OPERATORS =
%i[* / % + - == === != < > <= >= <=>].freeze
- UNARY_OPERATORS =
%i[+@ -@ ~ !].freeze
- OPERATORS =
(BINARY_OPERATORS + UNARY_OPERATORS).freeze
- NONMUTATING_METHODS_WITH_BANG_VERSION =
%i[capitalize chomp chop compact delete_prefix delete_suffix downcase encode flatten gsub lstrip merge next reject reverse rotate rstrip scrub select shuffle slice sort sort_by squeeze strip sub succ swapcase tr tr_s transform_values unicode_normalize uniq upcase].freeze
- METHODS_REPLACEABLE_BY_EACH =
%i[collect map].freeze
- NONMUTATING_METHODS =
(NONMUTATING_METHODS_WITH_BANG_VERSION + METHODS_REPLACEABLE_BY_EACH).freeze
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source, #project_index
Instance Method Summary collapse
- #on_begin(node) ⇒ Object (also: #on_kwbegin)
- #on_block(node) ⇒ Object (also: #on_numblock, #on_itblock)
- #on_ensure(node) ⇒ Object
Methods included from AutoCorrector
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
cop_dir_for, #exclude_limit, read_limits
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#on_begin(node) ⇒ Object Also known as: on_kwbegin
101 102 103 |
# File 'lib/rubocop/cop/lint/void.rb', line 101 def on_begin(node) check_begin(node) end |
#on_block(node) ⇒ Object Also known as: on_numblock, on_itblock
90 91 92 93 94 95 96 97 |
# File 'lib/rubocop/cop/lint/void.rb', line 90 def on_block(node) return unless node.body && !node.body.begin_type? return unless in_void_context?(node.body) return if node.method?(:each) check_void_op(node.body) check_expression(node.body) end |
#on_ensure(node) ⇒ Object
106 107 108 |
# File 'lib/rubocop/cop/lint/void.rb', line 106 def on_ensure(node) check_ensure(node) end |