Skip to content

Commit

Permalink
Remove support for ===, =~, and !~ operators in Lint/UnusedComp…
Browse files Browse the repository at this point in the history
…arison

These comparison operators can have side-effects depending on the obj/args,
so there's no way to know if these are unused without semantic analysis.
  • Loading branch information
nobodywasishere committed Dec 11, 2024
1 parent 5ea3545 commit ca09eba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
17 changes: 5 additions & 12 deletions spec/ameba/rule/lint/unused_comparison_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "passes if a regex literal is part of a comparison" do
it "passes for unused comparisons with `===`, `=~`, and `!~`" do
expect_no_issues subject, <<-CRYSTAL
/f(o+)(bar?)/ === "fooba"
puts $~
"foobar" =~ /(o+)ba(r?)/
puts $1
CRYSTAL
/foo(bar)?/ =~ baz
"foobar" !~ /(o+)ba(r?)/
"hello" === world
CRYSTAL
end

it "fails for all comparison operators" do
Expand All @@ -52,12 +51,6 @@ module Ameba::Rule::Lint
# ^^^^^^ error: Comparison operation is unused
x != 2
# ^^^^^^ error: Comparison operation is unused
x =~ 2
# ^^^^^^ error: Comparison operation is unused
x !~ 2
# ^^^^^^ error: Comparison operation is unused
x === 2
# ^^^^^^^ error: Comparison operation is unused
x < 2
# ^^^^^ error: Comparison operation is unused
x <= 2
Expand Down
8 changes: 1 addition & 7 deletions src/ameba/rule/lint/unused_comparison.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,14 @@ module Ameba::Rule::Lint

MSG = "Comparison operation is unused"

COMPARISON_OPERATORS = %w[
== != =~ !~ ===
< <= > >= <=>
]
COMPARISON_OPERATORS = %w[== != < <= > >= <=>]

def test(source : Source)
AST::ImplicitReturnVisitor.new(self, source)
end

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.obj.is_a?(Crystal::RegexLiteral) ||
node.args.first.is_a?(Crystal::RegexLiteral)

issue_for node, MSG
end
end
Expand Down

0 comments on commit ca09eba

Please sign in to comment.