Skip to content

Commit 19192ef

Browse files
authored
Update equals is useful for always false (#3855)
1 parent 58dc21c commit 19192ef

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/cfnlint/rules/conditions/EqualsIsUseful.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,16 @@ def equals_is_useful(self, validator, s, instance, schema):
5050
first = "true" if first else "false"
5151
if str(first) == str(second):
5252
yield ValidationError(
53-
f"{instance!r} will always return {True!r} or {False!r}",
53+
f"{instance!r} will always return {True!r}",
5454
rule=self,
5555
)
56+
if isinstance(instance[0], (str, float, int, bool)) and isinstance(
57+
instance[1], (str, float, int, bool)
58+
):
59+
if str(first) != str(second):
60+
yield ValidationError(
61+
f"{instance!r} will always return {False!r}",
62+
rule=self,
63+
)
5664
except: # noqa: E722
5765
pass

test/unit/rules/conditions/test_equals_is_useful.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
("Equal string and integer", [1, "1"], 1),
1616
("Equal string and boolean", [True, "true"], 1),
1717
("Equal string and number", [1.0, "1.0"], 1),
18-
("Not equal string and integer", [1, "1.1"], 0),
19-
("Not equal string and boolean", [True, "True"], 0),
18+
("Not equal string and integer", [1, "1.1"], 1),
19+
("Not equal string and boolean", [True, "True"], 1),
2020
("No error on bad type", {"true": True}, 0),
2121
("No error on bad length", ["a", "a", "a"], 0),
22+
("No with string and account id", ["A", {"Ref": "AWS::AccountId"}], 0),
23+
("No with string and account id", [{"Ref": "AWS::AccountId"}, "A"], 0),
2224
],
2325
)
2426
def test_names(name, instance, num_of_errors):
2527
rule = EqualsIsUseful()
2628
validator = CfnTemplateValidator({})
2729
assert (
2830
len(list(rule.equals_is_useful(validator, {}, instance, {}))) == num_of_errors
29-
), f"Expected {num_of_errors} errors for {name}"
31+
), f"Expected {num_of_errors} errors for {name} and {instance}"

0 commit comments

Comments
 (0)