Class: Rubocop::Cop::Style::FavorSprintf

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/favor_sprintf.rb

Overview

This cop checks for uses of String#%.

It cannot be implemented in a reliable manner for all cases, so only two scenarios are considered - if the first argument is a string literal and if the second argument is an array literal.

Constant Summary collapse

MSG =
'Favor sprintf over String#%.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_send(node) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/style/favor_sprintf.rb', line 14

def on_send(node)
  receiver_node, method_name, *arg_nodes = *node

  if method_name == :% &&
      ([:str, :dstr].include?(receiver_node.type) ||
       arg_nodes[0].type == :array)
    add_offence(:convention, node.loc.selector, MSG)
  end
end