Class: RuboCop::Cop::Style::GuardClause
- Extended by:
- AutoCorrector
- Includes:
- MinBodyLength, RangeHelp, RuboCop::Cop::StatementModifier
- Defined in:
- lib/rubocop/cop/style/guard_clause.rb
Overview
Use a guard clause instead of wrapping the code inside a conditional expression
A condition with an ‘elsif` or `else` branch is allowed unless one of `return`, `break`, `next`, `raise`, or `fail` is used in the body of the conditional expression.
NOTE: Autocorrect works in most cases except with if-else statements
that contain logical operators such as `foo || raise('exception')`
Constant Summary collapse
- MSG =
'Use a guard clause (`%<example>s`) instead of wrapping the ' \ 'code inside a conditional expression.'
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_block(node) ⇒ Object (also: #on_numblock)
- #on_def(node) ⇒ Object (also: #on_defs)
- #on_if(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_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_block(node) ⇒ Object Also known as: on_numblock
132 133 134 135 136 |
# File 'lib/rubocop/cop/style/guard_clause.rb', line 132 def on_block(node) return unless node.method?(:define_method) || node.method?(:define_singleton_method) on_def(node) end |
#on_def(node) ⇒ Object Also known as: on_defs
123 124 125 126 127 128 129 |
# File 'lib/rubocop/cop/style/guard_clause.rb', line 123 def on_def(node) body = node.body return unless body check_ending_body(body) end |
#on_if(node) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rubocop/cop/style/guard_clause.rb', line 139 def on_if(node) return if accepted_form?(node) if (guard_clause = node.if_branch&.guard_clause?) kw = node.loc.keyword.source guard = :if elsif (guard_clause = node.else_branch&.guard_clause?) kw = node.inverse_keyword guard = :else else return end guard = nil if and_or_guard_clause?(guard_clause) register_offense(node, guard_clause_source(guard_clause), kw, guard) end |