summaryrefslogtreecommitdiffstats
path: root/lib/ansiblelint/rules/ComparisonToEmptyStringRule.py
blob: a43c4f7fa8d50d22ccc482e69a55334121163abe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018, Ansible Project

import re

from ansiblelint.rules import AnsibleLintRule


class ComparisonToEmptyStringRule(AnsibleLintRule):
    id = '602'
    shortdesc = "Don't compare to empty string"
    description = (
        'Use ``when: var|length > 0`` rather than ``when: var != ""`` (or '
        'conversely ``when: var|length == 0`` rather than ``when: var == ""``)'
    )
    severity = 'HIGH'
    tags = ['idiom']
    version_added = 'v4.0.0'

    empty_string_compare = re.compile("[=!]= ?(\"{2}|'{2})")

    def match(self, file, line):
        return self.empty_string_compare.search(line)