Class: RuboCop::Cop::Style::RedundantReturn
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::RangeHelp ,
::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_return.rb |
Overview
Checks for redundant return
expressions.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/style/redundant_return.rb', line 59'Redundant `return` detected.'
-
MULTI_RETURN_MSG =
# File 'lib/rubocop/cop/style/redundant_return.rb', line 60'To return multiple values, use an array.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/style/redundant_return.rb', line 61%i[define_method define_singleton_method lambda].freeze
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp
- Included
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
- #allow_multiple_return_values? ⇒ Boolean readonly private
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_def(node) (also: #on_defs)
-
#on_defs(node)
Alias for #on_def.
- #on_send(node)
- #add_braces(corrector, node) private
- #add_brackets(corrector, node) private
- #check_begin_node(node) private
- #check_branch(node) private
- #check_case_match_node(node) private
- #check_case_node(node) private
- #check_ensure_node(node) private
- #check_if_node(node) private
- #check_resbody_node(node) private
- #check_rescue_node(node) private
- #check_return_node(node) private
- #correct_with_arguments(return_node, corrector) private
- #correct_without_arguments(return_node, corrector) private
- #hash_without_braces?(node) ⇒ Boolean private
- #message(node) private
::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 |
#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
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #heredoc_range, #max_line_length, #multiline_ranges, #multiline_string?, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation?, #surrounding_heredoc?, #surrounding_percent_array? |
::RuboCop::Cop::IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Attribute Details
#allow_multiple_return_values? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/redundant_return.rb', line 175
def allow_multiple_return_values? cop_config['AllowMultipleReturnValues'] || false end
Instance Method Details
#add_braces(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 104
def add_braces(corrector, node) corrector.insert_before(node.children.first, '{') corrector.insert_after(node.children.last, '}') end
#add_brackets(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 99
def add_brackets(corrector, node) corrector.insert_before(node.children.first, '[') corrector.insert_after(node.children.last, ']') end
#check_begin_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 170
def check_begin_node(node) last_expr = node.children.last check_branch(last_expr) end
#check_branch(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 110
def check_branch(node) return unless node case node.type when :return then check_return_node(node) when :case then check_case_node(node) when :case_match then check_case_match_node(node) when :if then check_if_node(node) when :rescue then check_rescue_node(node) when :resbody then check_resbody_node(node) when :ensure then check_ensure_node(node) when :begin, :kwbegin check_begin_node(node) end end
#check_case_match_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 144
def check_case_match_node(node) node.in_pattern_branches.each { |in_pattern_node| check_branch(in_pattern_node.body) } check_branch(node.else_branch) end
#check_case_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 139
def check_case_node(node) node.when_branches.each { |when_node| check_branch(when_node.body) } check_branch(node.else_branch) end
#check_ensure_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 165
def check_ensure_node(node) rescue_node = node.node_parts[0] check_branch(rescue_node) end
#check_if_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 149
def check_if_node(node) return if node.ternary? check_branch(node.if_branch) check_branch(node.else_branch) end
#check_resbody_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 161
def check_resbody_node(node) check_branch(node.body) end
#check_rescue_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 156
def check_rescue_node(node) node.branches.each { |branch| check_branch(branch) } check_branch(node.body) unless node.else? end
#check_return_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 127
def check_return_node(node) return if cop_config['AllowMultipleReturnValues'] && node.children.size > 1 add_offense(node.loc.keyword, message: (node)) do |corrector| if node.arguments? correct_with_arguments(node, corrector) else correct_without_arguments(node, corrector) end end end
#correct_with_arguments(return_node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 80
def correct_with_arguments(return_node, corrector) if return_node.children.size > 1 add_brackets(corrector, return_node) elsif hash_without_braces?(return_node.first_argument) add_braces(corrector, return_node.first_argument) end if return_node.splat_argument? first_argument = return_node.first_argument corrector.replace(first_argument, first_argument.source.delete_prefix('*')) end keyword = range_with_surrounding_space(return_node.loc.keyword, side: :right) corrector.remove(keyword) end
#correct_without_arguments(return_node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 76
def correct_without_arguments(return_node, corrector) corrector.replace(return_node, 'nil') end
#hash_without_braces?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/redundant_return.rb', line 95
def hash_without_braces?(node) node.hash_type? && !node.braces? end
#message(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 179
def (node) if !allow_multiple_return_values? && node.children.size > 1 "#{MSG} #{MULTI_RETURN_MSG}" else MSG end end
#on_def(node) Also known as: #on_defs
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 69
def on_def(node) check_branch(node.body) end
#on_defs(node)
Alias for #on_def.
# File 'lib/rubocop/cop/style/redundant_return.rb', line 72
alias on_defs on_def
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/style/redundant_return.rb', line 63
def on_send(node) return unless node.block_literal? check_branch(node.parent.body) end