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

fix(lint): useless assign with method call within a condition #485

Merged
merged 2 commits into from
Oct 14, 2024
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
14 changes: 14 additions & 0 deletions spec/ameba/rule/lint/useless_assign_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,20 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "doesn't report if assignment is referenced within a method call" do
expect_no_issues subject, <<-CRYSTAL
if v = rand
puts(v = 1)
end
v
CRYSTAL

expect_no_issues subject, <<-CRYSTAL
puts v = 1 unless v = rand
v
CRYSTAL
end

it "reports if assignment is useless in the branch" do
expect_issue subject, <<-CRYSTAL
def method(a)
Expand Down
2 changes: 2 additions & 0 deletions src/ameba/ast/branch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ module Ameba::AST
if loop?(node) && (block = node.block)
on_branchable_start node, block.body
end

true
Copy link
Member Author

@veelenga veelenga Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A visitor class that constructs branches ignored all the nodes inside Crystal::Call

end

def end_visit(node : Crystal::Call)
Expand Down