Skip to content

Lint unused variable assignment / dead store (false negative unused-variable?) #5838

Open
@richardebeling

Description

@richardebeling

(Issue altered after the discussions up to #5838 (comment) to better reflect intention)

Problem description

Pylint will give unused-variable / W0612 when code assigns a variable that is never used, but this message will not appear if there was any usage of the variable in the past. However, in code, I'd consider it a code smell to assign an unused value to a variable, even if the variable was used before. Consider this code:

#! /usr/bin/env python3
# pylint: disable=missing-docstring

def some_func():
    var = 3
    print(var)
    var = 4
    # var is never used again. The last assignment thus could be removed.

In c++ code, clang-tidy would warn about the dead store.

Expected behavior

Initially, I had considered this to be a case for unused-variable. As @jacobtylerwalls pointed out, the variable is technically used, so maybe it should be a different message.

************* Module test
test.py:6:4: W0612: Unused variable 'var' (unused-variable)

-------------------------------------------------------------------
Your code has been rated at 6.67/10 (previous run: 10.00/10, -3.33)

Pylint version

pylint 2.13.0-dev0
astroid 2.9.3
Python 3.9.7 (default, Sep 10 2021, 14:59:43) [GCC 11.2.0]

(installed from main at 6622d10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: unused-variableEnhancement ✨Improvement to a componentHigh effort 🏋Difficult solution or problem to solveNeeds specification 🔐Accepted as a potential improvement, and needs to specify edge cases, message names, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions