diff --git a/spec/ameba/rule/redundant_begin_spec.cr b/spec/ameba/rule/redundant_begin_spec.cr index 4e0f4061..7bdad9a9 100644 --- a/spec/ameba/rule/redundant_begin_spec.cr +++ b/spec/ameba/rule/redundant_begin_spec.cr @@ -153,7 +153,7 @@ module Ameba::Rule subject.catch(s).should_not be_valid end - it "fails if there is an inner redundant block" do + it "doesn't report if there is an inner redundant block" do s = Source.new %q( def method begin @@ -164,7 +164,7 @@ module Ameba::Rule rescue end ) - subject.catch(s).should_not be_valid + subject.catch(s).should be_valid end it "fails if there is a redundant block with yield" do diff --git a/src/ameba/rule/redundant_begin.cr b/src/ameba/rule/redundant_begin.cr index bf5ec4fc..cc7f35e1 100644 --- a/src/ameba/rule/redundant_begin.cr +++ b/src/ameba/rule/redundant_begin.cr @@ -88,7 +88,7 @@ module Ameba::Rule end private def redundant_begin_in_handler?(source, handler, node) - return false if begin_exprs_in_handler?(handler) + return false if begin_exprs_in_handler?(handler) || inner_handler?(handler) code = node_source(node, source.lines).try &.join("\n") def_redundant_begin? code if code @@ -96,6 +96,10 @@ module Ameba::Rule false end + private def inner_handler?(handler) + handler.body.is_a?(Crystal::ExceptionHandler) + end + private def begin_exprs_in_handler?(handler) if (body = handler.body).is_a?(Crystal::Expressions) exception_handler?(body.expressions.first)