Class: RuboCop::Cop::Kaizo::ExplicitBegin
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Kaizo::ExplicitBegin
- Extended by:
- AutoCorrector
- Includes:
- Alignment, RangeHelp, RescueNode
- Defined in:
- lib/rubocop/cop/kaizo/explicit_begin.rb
Overview
Requires an explicit begin...end block when a method body has a
rescue or ensure clause, instead of attaching the clause directly
to the method definition (an "implicit begin"). This is the inverse
of Style/RedundantBegin, which kaizo's default configuration
disables so the two cops do not fight each other.
An explicit begin names the unit of work being guarded, keeps the
rescue/ensure visually bound to it, and leaves room for the method to
grow other statements around the guarded region without silently
widening what the rescue covers.
Modifier rescue expressions (foo rescue nil) and endless method
definitions are not flagged.
Constant Summary collapse
- MSG =
"Use an explicit `begin` block for `rescue`/`ensure` in a method body.".freeze
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
Instance Method Details
#on_def(node) ⇒ Object Also known as: on_defs
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rubocop/cop/kaizo/explicit_begin.rb', line 50 def on_def(node) return if node.endless? body = node.body return unless implicit_begin?(body) range = clause_keyword(body) if safe_to_correct?(node, body) add_offense(range) { |corrector| wrap_in_begin(corrector, node, body) } else add_offense(range) end end |