Skip to content

Commit

Permalink
Redundant begin: do not report if there is an inner handler
Browse files Browse the repository at this point in the history
fixes #56
  • Loading branch information
veelenga committed May 25, 2018
1 parent 3e099e9 commit 3887da1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions spec/ameba/rule/redundant_begin_spec.cr
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/ameba/rule/redundant_begin.cr
Expand Up @@ -88,14 +88,18 @@ 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
rescue
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)
Expand Down

0 comments on commit 3887da1

Please sign in to comment.