Class: RuboCop::Cop::Style::GuardClause

Inherits:
Base
  • Object
show all
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')`

Examples:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something

  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

# bad
if something
  foo || raise('exception')
else
  ok
end

# good
foo || raise('exception') if something
ok

# bad
define_method(:test) do
  if something
    work
  end
end

# good
define_method(:test) do
  return unless something

  work
end

# also good
define_method(:test) do
  work if something
end

AllowConsecutiveConditionals: false (default)

# bad
def test
  if foo?
    work
  end

  if bar?  # <- reports an offense
    work
  end
end

AllowConsecutiveConditionals: true

# good
def test
  if foo?
    work
  end

  if bar?
    work
  end
end

# bad
def test
  if foo?
    work
  end

  do_something

  if bar?  # <- reports an offense
    work
  end
end

Constant Summary collapse

MSG =
'Use a guard clause (`%<example>s`) instead of wrapping the ' \
'code inside a conditional expression.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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

#exclude_limit

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

silence_warnings

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