Skip to content

Commit

Permalink
Add unused literal spec for with ... yield
Browse files Browse the repository at this point in the history
Don't catch unused comparisons with regex literals, regardless of operator
  • Loading branch information
nobodywasishere committed Dec 11, 2024
1 parent 6e87204 commit 5ea3545
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/unused_comparison_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "passes if a regex literal is part of a === or =~ comparison" do
it "passes if a regex literal is part of a comparison" do
expect_no_issues subject, <<-CRYSTAL
/f(o+)(bar?)/ === "fooba"
puts $~
Expand Down
7 changes: 7 additions & 0 deletions spec/ameba/rule/lint/unused_literal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "passes if a literal is passed to with or yield" do
expect_no_issues subject, <<-CRYSTAL
yield 1
with "2" yield :three
CRYSTAL
end

it "fails if literals are top-level" do
expect_issue subject, <<-CRYSTAL
1234
Expand Down
4 changes: 2 additions & 2 deletions src/ameba/rule/lint/unused_comparison.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ module Ameba::Rule::Lint

def test(source, node : Crystal::Call, last_is_used : Bool)
if !last_is_used && node.name.in?(COMPARISON_OPERATORS) && node.args.size == 1
return if node.name.in?("===", "=~") &&
(node.obj.is_a?(Crystal::RegexLiteral) || node.args.first.is_a?(Crystal::RegexLiteral))
return if node.obj.is_a?(Crystal::RegexLiteral) ||
node.args.first.is_a?(Crystal::RegexLiteral)

issue_for node, MSG
end
Expand Down

0 comments on commit 5ea3545

Please sign in to comment.