Skip to content

Commit

Permalink
fix(lint): useless assign with method call within a condition
Browse files Browse the repository at this point in the history
fixes #468
  • Loading branch information
veelenga committed Oct 14, 2024
1 parent 7eb16a5 commit 4e180d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 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,33 @@ 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

expect_no_issues subject, <<-CRYSTAL
hash = {"foo" => "bar"}
def call(v)
end
unless v = hash["foo"]?
call v = "default"
end
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
end

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

0 comments on commit 4e180d4

Please sign in to comment.