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 macro expression and control support #553

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions spec/ameba/rule/lint/unused_literal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,26 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "fails if unused literals in macro expressions" do
expect_issue subject, <<-CRYSTAL
{{ "hello world" }}
{% "hello world" %}
# ^^^^^^^^^^^^^ error: Literal value is not used

{%
if var == 2
"2"
# ^^^ error: Literal value is not used
end
%}

macro name(foo)
{% "bar" %}
# ^^^^^ error: Literal value is not used
end
CRYSTAL
end

# Locations for Regex literals were added in Crystal v1.15.0
{% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %}
it "fails if a regex literal is unused" do
Expand Down
47 changes: 45 additions & 2 deletions src/ameba/ast/visitors/implicit_return_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ module Ameba::AST
false
end

def visit(node : Crystal::Macro) : Bool
@rule.test(@source, node, @stack.positive?)

incr_stack do
node.args.each &.accept(self)
node.double_splat.try &.accept(self)
node.block_arg.try &.accept(self)
node.body.accept(self)
end

false
end

def visit(node : Crystal::ClassDef | Crystal::ModuleDef) : Bool
@rule.test(@source, node, @stack.positive?)

Expand Down Expand Up @@ -313,13 +326,43 @@ module Ameba::AST
false
end

def visit(node : Crystal::Generic | Crystal::Path | Crystal::Union) : Bool
def visit(node : Crystal::MacroExpression) : Bool
@rule.test(@source, node, @stack.positive?)

if node.output?
incr_stack { node.exp.accept(self) }
else
node.exp.accept(self)
end

false
end

def visit(node : Crystal::MacroIf) : Bool
@rule.test(@source, node, @stack.positive?)

incr_stack { node.cond.accept(self) }
node.then.accept(self)
node.else.accept(self)

false
end

def visit(node : Crystal::Macro | Crystal::MacroIf | Crystal::MacroFor) : Bool
def visit(node : Crystal::MacroFor) : Bool
@rule.test(@source, node, @stack.positive?)

node.body.accept(self)

false
end

def visit(node : Crystal::MacroVar)
@rule.test(@source, node, @stack.positive?)

false
end

def visit(node : Crystal::Generic | Crystal::Path | Crystal::Union) : Bool
@rule.test(@source, node, @stack.positive?)

false
Expand Down
Loading