pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.32k stars 1.14k forks source link

Branching mistake leading to unused variable: to be detected #3297

Open mhooreman opened 4 years ago

mhooreman commented 4 years ago

Hello,

The following code

  1. defines a filter which depends of an if condition
  2. should apply the filter on a data set
  3. ... but only applies the filter on one branch of the if, instead of the common branch:
#!/usr/bin/env python
"""lorem ipsum"""

import numpy as np

def get(greater):
    """lorem ipsum"""
    ret = np.arange(10)
    if greater:
        f = ret > 5
        ret = ret[f].copy()  # The bug is here ...
    else:
        f = ret <= 5  # f is not used - to be detected => pylint feature request (W0602?)
    # ... the line of the bug shoud be here instead
    return ret

if __name__ == "__main__":
    print(get(True))
    print(get(False))

It would be great to have a warning issued in such case. I think that unused-variable (W0612) would be accurate.

Thanks a lot for your help and support.

PCManticore commented 4 years ago

Thanks for the report! Indeed we should be able to catch this unused variable.

mhooreman commented 4 years ago

Thanks a lot @PCManticore

mhooreman commented 1 month ago

Hello,

I'm sorting out my pending issues. Any update on this?

Pierre-Sassoulas commented 1 month ago

No one worked on it yet, merge request welcome :)