Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update equals is useful for always false #3855

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
Loading