Skip to content

Commit

Permalink
Merge pull request #485 from crystal-ameba/fix/call-within-branch
Browse files Browse the repository at this point in the history
fix(lint): useless assign with method call within a condition
  • Loading branch information
Sija authored Oct 14, 2024
2 parents e5c0715 + 7a7d862 commit 1d34b07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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
end

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

0 comments on commit 1d34b07

Please sign in to comment.