Skip to content

Emit pointless-statement (or some new warning) when no variable is assigned to the result of a function with a return type and no side effects #10224

Closed as duplicate of#7935
@jshumphrey

Description

@jshumphrey

Current problem

I just found and fixed a bug in my code that was roughly down to the following bad logic:

my_string = "This string might have bad characters in it!"
for bad_char, good_char in CHARS_TO_REPLACE.items():
    my_string.replace(bad_char, good_char)

The bug here, in case it's not obvious at first glance, is that my_string.replace() doesn't actually modify my_string (a new, modified string is returned), and the result of my_string.replace() was never assigned to a variable, so the character-replacements were never actually performed.

Desired solution

It would be nice if the call to my_string.replace() was flagged with pointless-statement (or some new warning message, as deemed appropriate) in situations like this, where the called function has a return type, is not known to have any side effects, and where the result of the function call is not assigned to a variable.

Additional context

I would imagine that those restrictions (return type, no known side effects) would probably limit the usability of this feature to the standard library (since the APIs of external libraries might change too often to make this maintainable), but even within the standard library, I've been bitten by this exact issue (str.replace) more often than I'd like to admit, and it seems like this is something Pylint would be able to help with.

Even if this check was constrained to a specific enumerated list of functions, I would imagine others may have gotten bitten by the same issue with some of these common standard-library functions:

  • Other similar str methods such as str.capitalize, str.encode, str.ljust, etc.
  • set.union, set.intersection, set.difference, set.symmetric_difference, etc.
  • sorted() (not list.sort, which does operate by side effect)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate 🐫Duplicate of an already existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions