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

Avoid false positives with literal-compare #1315

Merged
merged 1 commit into from Feb 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions playbook.yml
@@ -0,0 +1,5 @@
- hosts: localhost
tasks:
- shell:
cmd: |
if [ $FOO != false ]; then
12 changes: 9 additions & 3 deletions src/ansiblelint/rules/ComparisonToLiteralBoolRule.py
@@ -1,9 +1,11 @@
# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018, Ansible Project
# Copyright (c) 2018-2021, Ansible Project

import re
from typing import Any, Dict, Union

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.utils import nested_items


class ComparisonToLiteralBoolRule(AnsibleLintRule):
Expand All @@ -19,5 +21,9 @@ class ComparisonToLiteralBoolRule(AnsibleLintRule):

literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)")

def match(self, line: str) -> bool:
return bool(self.literal_bool_compare.search(line))
def matchtask(self, task: Dict[str, Any]) -> Union[bool, str]:
for k, v in nested_items(task):
if k == 'when':
if self.literal_bool_compare.search(v):
return True
return False