Class: RuboCop::Cop::Style::RedundantSelf
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:
self,
::RuboCop::Cop::Base ,
::RuboCop::Cop::AutocorrectLogic ,
::RuboCop::Cop::IgnoredNode ,
::RuboCop::Util ,
RuboCop::AST::Sexp
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/style/redundant_self.rb |
Overview
Checks for redundant uses of self
.
The usage of self
is only needed when:
-
Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.
-
Calling an attribute writer to prevent a local variable assignment.
Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.
Note we allow uses of self
with operators because it would be awkward
otherwise. Also allows the use of self.it
without arguments in blocks,
as in 0.times { self.it }
, following Lint/ItWithoutArgumentsInBlock
cop.
Constant Summary
-
KERNEL_METHODS =
# File 'lib/rubocop/cop/style/redundant_self.rb', line 49Kernel.methods(false)
-
KEYWORDS =
# File 'lib/rubocop/cop/style/redundant_self.rb', line 50%i[alias and begin break case class def defined? do else elsif end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield __FILE__ __LINE__ __ENCODING__].freeze
-
MSG =
# File 'lib/rubocop/cop/style/redundant_self.rb', line 48'Redundant `self` detected.'
::RuboCop::Cop::Base
- Inherited
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 |
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
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
-
#on_and_asgn(node)
Alias for #on_or_asgn.
- #on_args(node)
- #on_block(node) (also: #on_numblock, #on_itblock)
- #on_blockarg(node)
-
#on_def(node)
(also: #on_defs)
Using self.x to distinguish from local variable x.
-
#on_defs(node)
Alias for #on_def.
- #on_if(node) (also: #on_while, #on_until)
- #on_in_pattern(node)
-
#on_itblock(node)
Alias for #on_block.
- #on_lvasgn(node)
- #on_masgn(node)
-
#on_numblock(node)
Alias for #on_block.
- #on_op_asgn(node)
-
#on_or_asgn(node)
(also: #on_and_asgn)
Assignment of self.x.
- #on_send(node)
-
#on_until(node)
Alias for #on_if.
-
#on_while(node)
Alias for #on_if.
- #add_lhs_to_local_variables_scopes(rhs, lhs) private
- #add_masgn_lhs_variables(rhs, lhs) private
- #add_match_var_scopes(in_pattern_node) private
- #add_scope(node, local_variables = []) private
- #allow_self(node) private
- #allowed_send_node?(node) ⇒ Boolean private
-
#it_method_in_block?(node) ⇒ Boolean
private
Respects
Lint/ItWithoutArgumentsInBlock
cop and the following Ruby 3.3’s warning: - #on_argument(node) private
- #regular_method_call?(node) ⇒ Boolean private
::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 |
#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_gem_version | Returns a gems locked versions (i.e. |
#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
.new(config = nil, options = nil) ⇒ RedundantSelf
# File 'lib/rubocop/cop/style/redundant_self.rb', line 60
def initialize(config = nil, = nil) super @allowed_send_nodes = [] @local_variables_scopes = Hash.new { |hash, key| hash[key] = [] }.compare_by_identity end
Class Method Details
.autocorrect_incompatible_with
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 56
def self.autocorrect_incompatible_with [ColonMethodCall, Layout::DotPosition] end
Instance Method Details
#add_lhs_to_local_variables_scopes(rhs, lhs) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 193
def add_lhs_to_local_variables_scopes(rhs, lhs) if rhs&.send_type? && !rhs.arguments.empty? rhs.arguments.each { |argument| @local_variables_scopes[argument] << lhs } else @local_variables_scopes[rhs] << lhs end end
#add_masgn_lhs_variables(rhs, lhs) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 201
def add_masgn_lhs_variables(rhs, lhs) lhs.children.each do |child| add_lhs_to_local_variables_scopes(rhs, child.to_a.first) end end
#add_match_var_scopes(in_pattern_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 207
def add_match_var_scopes(in_pattern_node) in_pattern_node.each_descendant(:match_var) do |match_var_node| @local_variables_scopes[in_pattern_node] << match_var_node.children.first end end
#add_scope(node, local_variables = []) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 142
def add_scope(node, local_variables = []) node.each_descendant do |child_node| @local_variables_scopes[child_node] = local_variables end end
#allow_self(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 187
def allow_self(node) return unless node.send_type? && node.self_receiver? @allowed_send_nodes << node end
#allowed_send_node?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/redundant_self.rb', line 148
def allowed_send_node?(node) @allowed_send_nodes.include?(node) || @local_variables_scopes[node].include?(node.method_name) || node.each_ancestor.any? do |ancestor| @local_variables_scopes[ancestor].include?(node.method_name) end || KERNEL_METHODS.include?(node.method_name) end
#it_method_in_block?(node) ⇒ Boolean
(private)
Respects Lint/ItWithoutArgumentsInBlock
cop and the following Ruby 3.3’s warning:
$ ruby -e '0.times { begin; it; end }'
-e:1: warning: it
calls without arguments will refer to the first block param in
Ruby 3.4; use it() or self.it
# File 'lib/rubocop/cop/style/redundant_self.rb', line 163
def it_method_in_block?(node) return false unless node.method?(:it) return false unless (block_node = node.each_ancestor(:block).first) return false unless block_node.arguments.empty_and_without_delimiters? node.arguments.empty? && !node.block_literal? end
#on_and_asgn(node)
Alias for #on_or_asgn.
# File 'lib/rubocop/cop/style/redundant_self.rb', line 74
alias on_and_asgn on_or_asgn
#on_args(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 87
def on_args(node) node.children.each { |arg| on_argument(arg) } end
#on_argument(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 179
def on_argument(node) if node.mlhs_type? on_args(node) elsif node.respond_to?(:name) @local_variables_scopes[node] << node.name end end
#on_block(node) Also known as: #on_numblock, #on_itblock
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 119
def on_block(node) add_scope(node, @local_variables_scopes[node]) end
#on_blockarg(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 91
def on_blockarg(node) on_argument(node) end
#on_def(node) Also known as: #on_defs
Using self.x to distinguish from local variable x
# File 'lib/rubocop/cop/style/redundant_self.rb', line 82
def on_def(node) add_scope(node) end
#on_defs(node)
Alias for #on_def.
# File 'lib/rubocop/cop/style/redundant_self.rb', line 85
alias on_defs on_def
#on_if(node) Also known as: #on_while, #on_until
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 126
def on_if(node) # Allow conditional nodes to use `self` in the condition if that variable # name is used in an `lvasgn` or `masgn` within the `if`. node.each_descendant(:lvasgn, :masgn) do |descendant_node| if descendant_node.lvasgn_type? add_lhs_to_local_variables_scopes(node.condition, descendant_node.lhs) else add_masgn_lhs_variables(node.condition, descendant_node.lhs) end end end
#on_in_pattern(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 103
def on_in_pattern(node) add_match_var_scopes(node) end
#on_itblock(node)
Alias for #on_block.
# File 'lib/rubocop/cop/style/redundant_self.rb', line 124
alias on_itblock on_block
#on_lvasgn(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 99
def on_lvasgn(node) add_lhs_to_local_variables_scopes(node.rhs, node.lhs) end
#on_masgn(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 95
def on_masgn(node) add_masgn_lhs_variables(node.rhs, node.lhs) end
#on_numblock(node)
Alias for #on_block.
# File 'lib/rubocop/cop/style/redundant_self.rb', line 123
alias on_numblock on_block
#on_op_asgn(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 76
def on_op_asgn(node) allow_self(node.lhs) end
#on_or_asgn(node) Also known as: #on_and_asgn
Assignment of self.x
# File 'lib/rubocop/cop/style/redundant_self.rb', line 68
def on_or_asgn(node) allow_self(node.lhs) lhs_name = node.lhs.lvasgn_type? ? node.lhs.name : node.lhs add_lhs_to_local_variables_scopes(node.rhs, lhs_name) end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_self.rb', line 107
def on_send(node) return unless node.self_receiver? && regular_method_call?(node) return if node.parent&.mlhs_type? return if allowed_send_node?(node) return if it_method_in_block?(node) add_offense(node.receiver) do |corrector| corrector.remove(node.receiver) corrector.remove(node.loc.dot) end end
#on_until(node)
Alias for #on_if.
# File 'lib/rubocop/cop/style/redundant_self.rb', line 138
alias on_until on_if
#on_while(node)
Alias for #on_if.
# File 'lib/rubocop/cop/style/redundant_self.rb', line 137
alias on_while on_if
#regular_method_call?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/redundant_self.rb', line 171
def regular_method_call?(node) !(node.operator_method? || KEYWORDS.include?(node.method_name) || node.camel_case_method? || node.setter_method? || node.implicit_call?) end