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]
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
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
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
91 92 93 |
# File 'lib/rubocop/cop/lint/void.rb', line 91 def on_begin(node) check_begin(node) end |
#on_block(node) ⇒ Object Also known as: on_numblock, on_itblock
81 82 83 84 85 86 87 |
# 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) ⇒ Object
96 97 98 |
# File 'lib/rubocop/cop/lint/void.rb', line 96 def on_ensure(node) check_ensure(node) end |