Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::IfUnlessModifier

Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configurable.

Public Instance Methods

error_message(keyword) click to toggle source
# File lib/rubocop/cop/style/if_unless_modifier.rb, line 11
def error_message(keyword)
  "Favor modifier `#{keyword}` usage when having a single-line body."            ' Another good alternative is the usage of control flow `&&`/`||`.'
end
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/style/if_unless_modifier.rb, line 16
def investigate(processed_source)
  return unless processed_source.ast
  on_node(:if, processed_source.ast) do |node|
    # discard ternary ops, if/else and modifier if/unless nodes
    next if ternary_op?(node)
    next if modifier_if?(node)
    next if elsif?(node)
    next if if_else?(node)

    if check(node, processed_source.comments)
      add_offense(node, :keyword,
                  error_message(node.loc.keyword.source))
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.