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

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

Closed
jshumphrey opened this issue Feb 7, 2025 · 1 comment
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@jshumphrey
Copy link

jshumphrey commented Feb 7, 2025

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)
@jshumphrey jshumphrey added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 7, 2025
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue. This is a great idea and a duplicate of #7935, (I just had to take 10 minutes to find it and I knew it existed so don't worry about the dup 😄 )

@Pierre-Sassoulas Pierre-Sassoulas added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants