Skip to content

Commit

Permalink
Avoid false positives with literal-compare (#1315)
Browse files Browse the repository at this point in the history
Refactor rule to only look inside when conditions and avoid
using line-parser.

Fixes: #470
  • Loading branch information
ssbarnea committed Feb 7, 2021
1 parent 33b1ee8 commit 43788ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
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

0 comments on commit 43788ce

Please sign in to comment.