pylint icon indicating copy to clipboard operation
pylint copied to clipboard

False-positive unreachable code

Open dolik-rce opened this issue 1 year ago • 3 comments

Bug description

import signal
import sys

def handle_signal():
    signal.signal(signal.SIGINT, lambda sig, frame: [print("Aborted using CTRL+C"), sys.exit(1)])
    print("Signal handler installed.")

Configuration

No response

Command used

python3 -m pylint a.py

Pylint output

************* Module test
test.py:6:4: W0101: Unreachable code (unreachable)

Expected behavior

No warning should be emitted, because the "unreachable" code is actually called when executed.

Pylint version

pylint 2.17.2
astroid 2.15.2
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]

It did work before I upgraded, at least with this version: 

pylint 2.15.4
astroid 2.12.11
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110]

OS / Environment

Debian 11.3, bash 5.1.4

Additional dependencies

No response

dolik-rce avatar Apr 12 '23 11:04 dolik-rce

Thanks! Simpler repro:

import sys
lambda code: [sys.exit(code)]
print('hello')

jacobtylerwalls avatar Apr 12 '23 12:04 jacobtylerwalls

Regression in bed42badfe6be6317e6dc6406433218287ded3b9.

jacobtylerwalls avatar Apr 12 '23 12:04 jacobtylerwalls

This line shouldn't assume direct parentage by lambda, we need to go higher up (in the bug report, a list intervenes first): https://github.com/pylint-dev/pylint/blob/bed42badfe6be6317e6dc6406433218287ded3b9/pylint/checkers/base/basic_checker.py#L666

cc / @clavedeluna

jacobtylerwalls avatar Apr 12 '23 12:04 jacobtylerwalls