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
Labels
Duplicate 🐫
Duplicate of an already existing issue
Current problem
I just found and fixed a bug in my code that was roughly down to the following bad logic:
The bug here, in case it's not obvious at first glance, is that
my_string.replace()
doesn't actually modifymy_string
(a new, modified string is returned), and the result ofmy_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 withpointless-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:
str
methods such asstr.capitalize
,str.encode
,str.ljust
, etc.set.union
,set.intersection
,set.difference
,set.symmetric_difference
, etc.sorted()
(notlist.sort
, which does operate by side effect)The text was updated successfully, but these errors were encountered: