Class: Rubocop::Cop::Style::TrivialAccessors

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

Overview

This cop looks for trivial reader/writer methods, that could have been created with the attr_* family of functions automatically.

Constant Summary collapse

MSG =
'Use attr_%s to define trivial %s methods.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

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

Constructor Details

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

Instance Method Details

#on_def(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 11

def on_def(node)
  method_name, args, body = *node

  kind = if body && body.type == :ivar
           'reader'
         elsif args.children.size == 1 &&
               body && body.type == :ivasgn &&
               body.children[1] && body.children[1].type == :lvar &&
               method_name != :initialize
           'writer'
         end
  if kind
    add_offence(:convention, node.loc.keyword,
                sprintf(MSG, kind, kind))
  end

  super
end