mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Merge pull request #417 from crystal-ameba/fix-issue-400
Fix false positive with dynamic literals in `Lint/LiteralsComparison`
This commit is contained in:
commit
3f1e925e07
2 changed files with 7 additions and 4 deletions
|
@ -6,6 +6,8 @@ module Ameba::Rule::Lint
|
||||||
describe LiteralsComparison do
|
describe LiteralsComparison do
|
||||||
it "passes for valid cases" do
|
it "passes for valid cases" do
|
||||||
expect_no_issues subject, <<-CRYSTAL
|
expect_no_issues subject, <<-CRYSTAL
|
||||||
|
{start.year, start.month} == {stop.year, stop.month}
|
||||||
|
["foo"] === [foo]
|
||||||
"foo" == foo
|
"foo" == foo
|
||||||
"foo" != foo
|
"foo" != foo
|
||||||
foo == "foo"
|
foo == "foo"
|
||||||
|
@ -15,8 +17,8 @@ module Ameba::Rule::Lint
|
||||||
|
|
||||||
it "reports if there is a dynamic comparison possibly evaluating to the same" do
|
it "reports if there is a dynamic comparison possibly evaluating to the same" do
|
||||||
expect_issue subject, <<-CRYSTAL
|
expect_issue subject, <<-CRYSTAL
|
||||||
[foo] === ["foo"]
|
[foo] === [foo]
|
||||||
# ^^^^^^^^^^^^^^^ error: Comparison most likely evaluates to the same
|
# ^^^^^^^^^^^^^ error: Comparison most likely evaluates to the same
|
||||||
CRYSTAL
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -36,14 +36,15 @@ module Ameba::Rule::Lint
|
||||||
arg_is_literal, arg_is_static = literal_kind?(arg)
|
arg_is_literal, arg_is_static = literal_kind?(arg)
|
||||||
|
|
||||||
return unless obj_is_literal && arg_is_literal
|
return unless obj_is_literal && arg_is_literal
|
||||||
|
return unless obj.to_s == arg.to_s
|
||||||
|
|
||||||
is_dynamic = !obj_is_static || !arg_is_static
|
is_dynamic = !obj_is_static || !arg_is_static
|
||||||
|
|
||||||
what =
|
what =
|
||||||
case node.name
|
case node.name
|
||||||
when "===" then "the same"
|
when "===" then "the same"
|
||||||
when "==" then (obj.to_s == arg.to_s).to_s
|
when "==" then "true"
|
||||||
when "!=" then (obj.to_s != arg.to_s).to_s
|
when "!=" then "false"
|
||||||
end
|
end
|
||||||
|
|
||||||
issue_for node, (is_dynamic ? MSG_LIKELY : MSG) % what
|
issue_for node, (is_dynamic ? MSG_LIKELY : MSG) % what
|
||||||
|
|
Loading…
Reference in a new issue