Class: Rubocop::Cop::EnsureReturn

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

Constant Summary collapse

ERROR_MESSAGE =
'Never return from an ensure block.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name

Constructor Details

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

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/ensure_return.rb', line 8

def inspect(file, source, tokens, sexp)
  in_ensure = false
  ensure_col = nil

  tokens.each_index do |ix|
    t = tokens[ix]
    if ensure_start?(t)
      in_ensure = true
      ensure_col = t.pos.column
    elsif ensure_end?(t, ensure_col)
      in_ensure = false
    elsif in_ensure && t.type == :on_kw && t.text == 'return'
      add_offence(:warning, t.pos.lineno, ERROR_MESSAGE)
    end
  end
end