From e84cc05f0fe5c21a0e699082468547caa5fb688a Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Thu, 9 Nov 2023 08:20:01 +0100 Subject: [PATCH] Fix false positive with dynamic literals in `Lint/LiteralsComparison` --- spec/ameba/rule/lint/literals_comparison_spec.cr | 6 ++++-- src/ameba/rule/lint/literals_comparison.cr | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/spec/ameba/rule/lint/literals_comparison_spec.cr b/spec/ameba/rule/lint/literals_comparison_spec.cr index c0040d912..ded948e66 100644 --- a/spec/ameba/rule/lint/literals_comparison_spec.cr +++ b/spec/ameba/rule/lint/literals_comparison_spec.cr @@ -6,6 +6,8 @@ module Ameba::Rule::Lint describe LiteralsComparison do it "passes for valid cases" do expect_no_issues subject, <<-CRYSTAL + {start.year, start.month} == {stop.year, stop.month} + ["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 expect_issue subject, <<-CRYSTAL - [foo] === ["foo"] - # ^^^^^^^^^^^^^^^ error: Comparison most likely evaluates to the same + [foo] === [foo] + # ^^^^^^^^^^^^^ error: Comparison most likely evaluates to the same CRYSTAL end diff --git a/src/ameba/rule/lint/literals_comparison.cr b/src/ameba/rule/lint/literals_comparison.cr index 3082af19e..24b7b805b 100644 --- a/src/ameba/rule/lint/literals_comparison.cr +++ b/src/ameba/rule/lint/literals_comparison.cr @@ -36,14 +36,15 @@ module Ameba::Rule::Lint arg_is_literal, arg_is_static = literal_kind?(arg) return unless obj_is_literal && arg_is_literal + return unless obj.to_s == arg.to_s is_dynamic = !obj_is_static || !arg_is_static 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 + when "==" then "true" + when "!=" then "false" end issue_for node, (is_dynamic ? MSG_LIKELY : MSG) % what