Skip to content

Commit

Permalink
Update equals is useful for always false (#3855)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Dec 5, 2024
1 parent 58dc21c commit 19192ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/cfnlint/rules/conditions/EqualsIsUseful.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ def equals_is_useful(self, validator, s, instance, schema):
first = "true" if first else "false"
if str(first) == str(second):
yield ValidationError(
f"{instance!r} will always return {True!r} or {False!r}",
f"{instance!r} will always return {True!r}",
rule=self,
)
if isinstance(instance[0], (str, float, int, bool)) and isinstance(
instance[1], (str, float, int, bool)
):
if str(first) != str(second):
yield ValidationError(
f"{instance!r} will always return {False!r}",
rule=self,
)
except: # noqa: E722
pass
8 changes: 5 additions & 3 deletions test/unit/rules/conditions/test_equals_is_useful.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
("Equal string and integer", [1, "1"], 1),
("Equal string and boolean", [True, "true"], 1),
("Equal string and number", [1.0, "1.0"], 1),
("Not equal string and integer", [1, "1.1"], 0),
("Not equal string and boolean", [True, "True"], 0),
("Not equal string and integer", [1, "1.1"], 1),
("Not equal string and boolean", [True, "True"], 1),
("No error on bad type", {"true": True}, 0),
("No error on bad length", ["a", "a", "a"], 0),
("No with string and account id", ["A", {"Ref": "AWS::AccountId"}], 0),
("No with string and account id", [{"Ref": "AWS::AccountId"}, "A"], 0),
],
)
def test_names(name, instance, num_of_errors):
rule = EqualsIsUseful()
validator = CfnTemplateValidator({})
assert (
len(list(rule.equals_is_useful(validator, {}, instance, {}))) == num_of_errors
), f"Expected {num_of_errors} errors for {name}"
), f"Expected {num_of_errors} errors for {name} and {instance}"

0 comments on commit 19192ef

Please sign in to comment.