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

Add Lint/UnusedComparison and Lint/UnusedLiteral #507

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
172 changes: 172 additions & 0 deletions spec/ameba/rule/lint/unused_comparison_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
require "../../../spec_helper"

module Ameba::Rule::Lint
subject = UnusedComparison.new

describe UnusedComparison do
it "passes if comparisons are significant" do
expect_no_issues subject, <<-CRYSTAL
a = 1 == "1"
b = begin
2 == "3"
end

if c == b
puts "meow"
end

def test
1 == 2
end
CRYSTAL
end

it "passes for implicit object comparisons" do
expect_no_issues subject, <<-CRYSTAL
case obj
when .> 1 then true
when .< 0 then false
end
CRYSTAL
end

it "fails for all comparison operators" do
expect_issue subject, <<-CRYSTAL
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
# ^^^^^ 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
puts x
CRYSTAL
end

it "fails for an unused top-level comparison" do
expect_issue subject, <<-CRYSTAL
x = 1
x == 2
# ^^^^^^ error: Comparison operation is unused
puts x
CRYSTAL
end

it "fails for an unused comparison in a begin block" do
expect_issue subject, <<-CRYSTAL
begin
x = 1
x == 2
# ^^^^^^ error: Comparison operation is unused
puts x
end
CRYSTAL
end

it "fails for unused comparisons in if/elsif/else bodies" do
expect_issue subject, <<-CRYSTAL
a = if x = 1
x == 1
# ^^^^^^ error: Comparison operation is unused
x == 2
elsif true
x == 1
# ^^^^^^ error: Comparison operation is unused
x == 2
else
x == 2
# ^^^^^^ error: Comparison operation is unused
x == 1
# ^^^^^^ error: Comparison operation is unused
x == 3
end
CRYSTAL
end

it "fails for unused comparisons in a proc body" do
expect_issue subject, <<-CRYSTAL
a = -> {
x == 1
# ^^^^^^ error: Comparison operation is unused
"meow"
}
CRYSTAL
end

it "fails for unused comparison in if when not assigning" do
expect_issue subject, <<-CRYSTAL
if true
x == 1
# ^^^^^^ error: Comparison operation is unused
else
x == 2
# ^^^^^^ error: Comparison operation is unused
end
CRYSTAL
end

it "fails on useless comparisons" do
expect_issue subject, <<-CRYSTAL
def hello
if x == 3
x < 1
else
x > 1
end
end

def world
if x == 3
x < 1
# ^^^^^ error: Comparison operation is unused
else
x > 1
# ^^^^^ error: Comparison operation is unused
end

return
end

if x == 3
x < 1
# ^^^^^ error: Comparison operation is unused
else
x > 1
# ^^^^^ error: Comparison operation is unused
end

a = if x == 3
x > 1
# ^^^^^ error: Comparison operation is unused
x < 1
else
x > 1
end

a = if begin
x == 1
# ^^^^^^ error: Comparison operation is unused
x == 3
end
x == 4
end

x == 4
# ^^^^^^ error: Comparison operation is unused
CRYSTAL
end
end
end
Loading
Loading