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

False positive simplify-boolean-expression when boolean variables are modified after their intialization in a for loop #7626

Closed
finite-state-machine opened this issue Oct 16, 2022 · 0 comments · Fixed by #7627
Labels
Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@finite-state-machine
Copy link

finite-state-machine commented Oct 16, 2022

Bug description

For the following code:

# pylint: disable=missing-module-docstring

def some_function() -> bool:
    '''a function

    Returns:
        True
    '''
    flag_a = False
    flag_b = False
    for word in ['alfa', 'bravo']:
        if 'b' not in word:
            flag_a = True
        else:
            flag_b = True
    multiple = (flag_a and flag_b) or returns_false()  # <- error here
    return multiple

def returns_false() -> bool:
    '''a different function

    Returns:
        False, but pylint probably doesn't know that
    '''
    return False


def main() -> None:
    '''entry point
    '''
    print(repr(some_function()))  # True

if __name__ == '__main__':
    main()

...pylint emits:

R1709: Boolean expression may be simplified to returns_false() (simplify-boolean-expression)

This statement is incorrect: some_function() always returns True.

Note: removing ...or returns_false() causes the error to disappear.

Configuration

No response

Command used

pylint --rcfile=/dev/null thecodeabove.py

Pylint output

************* Module thecodeabove
thecodeabove.py:17:4: R1709: Boolean expression may be simplified to returns_false() (simplify-boolean-expression)

------------------------------------------------------------------
Your code has been rated at 9.38/10 (previous run: 9.38/10, +0.00)

Expected behavior

Pylint should not emit R1709/simplify-boolean-expression, since it is mistaken.

Pylint version

pylint 2.15.4
astroid 2.12.11
Python 3.8.12 (default, Jan 17 2022, 13:29:00)
[Clang 10.0.1 (clang-1001.0.46.4)]

OS / Environment

macOS 10.14

Additional dependencies

No response

@finite-state-machine finite-state-machine added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 16, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 16, 2022
@Pierre-Sassoulas Pierre-Sassoulas changed the title "Boolean expression may be simplified to" (simplify-boolean-expression) doesn't account for loops False positive simplify-boolean-expression when boolean variables are modified after their intialization in a for loop Oct 16, 2022
@jacobtylerwalls jacobtylerwalls self-assigned this Oct 16, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.5 milestone Oct 16, 2022
@jacobtylerwalls jacobtylerwalls removed their assignment Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
3 participants