Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST::ImplicitReturnVisitor implicit return fixes for Crystal::ExceptionHandler #549

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions spec/ameba/rule/lint/unused_literal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -245,32 +245,49 @@ module Ameba::Rule::Lint

it "fails if unused literals in rescue/ensure/else block" do
expect_issue subject, <<-CRYSTAL
a = begin
1234
# ^^^^ error: Literal value is not used
1234_f32
rescue ASDF
"hello world"
# ^^^^^^^^^^^^^ error: Literal value is not used
"interp \#{string}"
rescue QWERTY
[1, 2, 3, 4, 5]
# ^^^^^^^^^^^^^^^ error: Literal value is not used
{"hello" => "world"}
else
'\t'
# ^^^ error: Literal value is not used
<<-HEREDOC
# ^^^^^^^^^^ error: Literal value is not used
this is a heredoc
HEREDOC
1..2
ensure
{goodnight: moon}
# ^^^^^^^^^^^^^^^^^ error: Literal value is not used
{1, 2, 3}
end
CRYSTAL
a = begin
1234
# ^^^^ error: Literal value is not used
1234_f32
# ^^^^^^^^ error: Literal value is not used
rescue ASDF
"hello world"
# ^^^^^^^^^^^^^ error: Literal value is not used
"interp \#{string}"
rescue QWERTY
[1, 2, 3, 4, 5]
# ^^^^^^^^^^^^^^^ error: Literal value is not used
{"hello" => "world"}
else
'\t'
# ^^^ error: Literal value is not used
<<-HEREDOC
# ^^^^^^^^^^ error: Literal value is not used
this is a heredoc
HEREDOC
1..2
ensure
{goodnight: moon}
# ^^^^^^^^^^^^^^^^^ error: Literal value is not used
{1, 2, 3}
# ^^^^^^^^^ error: Literal value is not used
end

b = begin
1234
# ^^^^ error: Literal value is not used
1234_f32
rescue ASDF
"hello world"
# ^^^^^^^^^^^^^ error: Literal value is not used
"interp \#{string}"
ensure
{goodnight: moon}
# ^^^^^^^^^^^^^^^^^ error: Literal value is not used
{1, 2, 3}
# ^^^^^^^^^ error: Literal value is not used
end
CRYSTAL
end

# Locations for Regex literals were added in Crystal v1.15.0
Expand Down
12 changes: 10 additions & 2 deletions src/ameba/ast/visitors/implicit_return_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,18 @@ module Ameba::AST
def visit(node : Crystal::ExceptionHandler) : Bool
@rule.test(@source, node, @stack > 0)

node.body.accept(self)
if node.else
# Last line of body isn't implicitly returned if there's an else
swap_stack { node.body.try &.accept(self) }
else
node.body.accept(self)
end

node.rescues.try &.each &.accept(self)
node.else.try &.accept(self)
node.ensure.try &.accept(self)

# Last line of ensure isn't implicitly returned
swap_stack { node.ensure.try &.accept(self) }

false
end
Expand Down
Loading