Redundant begin: do not report if there is an inner handler

fixes #56
This commit is contained in:
Vitalii Elenhaupt 2018-05-24 22:58:58 +03:00 committed by V. Elenhaupt
parent 3e099e9afc
commit 3887da1438
2 changed files with 7 additions and 3 deletions

View file

@ -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

View file

@ -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)