Sha256: 337c432d55d68604d31f6d2e412d3495abd6c0f4bb9a429a71c19bcf40d2f2f3

Contents?: true

Size: 862 Bytes

Versions: 5

Compression:

Stored size: 862 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class EnsureReturn < Cop
      ERROR_MESSAGE = 'Never return from an ensure block.'

      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

      private

      def ensure_start?(t)
        t.type == :on_kw && t.text == 'ensure'
      end

      def ensure_end?(t, column)
        t.type == :on_kw && t.text == 'end' && t.pos.column == column
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.7.2 lib/rubocop/cop/ensure_return.rb
rubocop-0.7.1 lib/rubocop/cop/ensure_return.rb
rubocop-0.7.0 lib/rubocop/cop/ensure_return.rb
rubocop-0.6.1 lib/rubocop/cop/ensure_return.rb
rubocop-0.6.0 lib/rubocop/cop/ensure_return.rb