Extend StaticComparison w/ support of === operator

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-01 03:35:20 +01:00
parent 849be52618
commit ea42911c3c
2 changed files with 17 additions and 9 deletions

View file

@ -6,13 +6,21 @@ module Ameba::Rule::Lint
describe StaticComparison do
it "passes for valid cases" do
expect_no_issues subject, <<-CRYSTAL
/foo/ === "foo"
"foo" == foo
"foo" != foo
foo == "foo"
foo != "foo"
CRYSTAL
end
it "reports if there is a static comparison evaluating to true" do
it "reports if there is a static comparison evaluating to the same" do
expect_issue subject, <<-CRYSTAL
"foo" === "foo"
# ^^^^^^^^^^^^^ error: Comparison always evaluates to the same
CRYSTAL
end
it "reports if there is a static comparison evaluating to true (2)" do
expect_issue subject, <<-CRYSTAL
"foo" == "foo"
# ^^^^^^^^^^^^ error: Comparison always evaluates to true

View file

@ -21,7 +21,7 @@ module Ameba::Rule::Lint
description "Identifies static comparisons"
end
OP_NAMES = %w(== !=)
OP_NAMES = %w(=== == !=)
MSG = "Comparison always evaluates to %s"
PRIMITIVES = {
@ -45,12 +45,12 @@ module Ameba::Rule::Lint
return unless (obj = node.obj) && (arg = node.args.first?)
return unless obj.class.in?(PRIMITIVES) && arg.class.in?(PRIMITIVES)
case node.name
when "=="
what = (obj.to_s == arg.to_s).to_s
when "!="
what = (obj.to_s != arg.to_s).to_s
end
what =
case node.name
when "===" then "the same"
when "==" then (obj.to_s == arg.to_s).to_s
when "!=" then (obj.to_s != arg.to_s).to_s
end
issue_for node, MSG % what
end