-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Unused function call extension #8732
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
Open
LukasKrocek
wants to merge
5
commits into
pylint-dev:main
Choose a base branch
from
LukasKrocek:unassigned-function-call-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ec98be
Unused function call extension
LukasKrocek ec34107
rename from `unused-return-value` -> `function-return-not-assigned`
LukasKrocek 4e0a984
adding changelog
LukasKrocek b502545
adding bad and good example
LukasKrocek 900b7a4
switch to functional tests
LukasKrocek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def return_int(): | ||
return 1 | ||
|
||
|
||
return_int() # [function-return-not-assigned] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def return_int(): | ||
return 1 | ||
|
||
|
||
_ = return_int() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Add ``FunctionReturnNotAssignedChecker`` extension and new ``function-return-not-assigned`` message if return value not used. | ||
|
||
Refs #7935 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE | ||
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt | ||
|
||
"""Looks for unassigned function/method calls that have non-nullable return type.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import astroid | ||
from astroid import nodes | ||
|
||
from pylint.checkers import BaseChecker | ||
from pylint.checkers.typecheck import TypeChecker | ||
from pylint.checkers.utils import only_required_for_messages, safe_infer | ||
|
||
if TYPE_CHECKING: | ||
from pylint.lint import PyLinter | ||
|
||
|
||
class FunctionReturnNotAssignedChecker(BaseChecker): | ||
name = "function_return_not_assigned" | ||
msgs = { | ||
"W5486": ( | ||
"Function returned value which is never used", | ||
"function-return-not-assigned", | ||
"Function returns non-nullable value which is never used. " | ||
"Use explicit `_ = func_call()` if you are not interested in returned value", | ||
) | ||
} | ||
|
||
@only_required_for_messages("function-return-not-assigned") | ||
def visit_call(self, node: nodes.Call) -> None: | ||
result_is_used = not isinstance(node.parent, nodes.Expr) | ||
|
||
if result_is_used: | ||
return | ||
|
||
function_node = safe_infer(node.func) | ||
funcs = (nodes.FunctionDef, astroid.UnboundMethod, astroid.BoundMethod) | ||
|
||
# FIXME: more elegant solution probably exists | ||
# methods called on instances returned by functions in some libraries | ||
# are having function_node None and needs to be handled here | ||
# for example: | ||
# attrs.evolve returned instances | ||
# instances returned by any pyrsistent method (pmap.set, pvector.append, ...) | ||
if function_node is None: | ||
try: | ||
for n in node.func.infer(): | ||
if not isinstance(n, astroid.BoundMethod): | ||
continue | ||
function_node = n | ||
break | ||
except Exception: # pylint:disable=broad-exception-caught | ||
pass | ||
|
||
if not isinstance(function_node, funcs): | ||
return | ||
|
||
# Unwrap to get the actual function node object | ||
if isinstance(function_node, astroid.BoundMethod) and isinstance( | ||
function_node._proxied, astroid.UnboundMethod | ||
): | ||
function_node = function_node._proxied._proxied | ||
|
||
# Make sure that it's a valid function that we can analyze. | ||
# Ordered from less expensive to more expensive checks. | ||
if ( | ||
not function_node.is_function | ||
or function_node.decorators | ||
or TypeChecker._is_ignored_function(function_node) | ||
): | ||
return | ||
|
||
return_nodes = list( | ||
function_node.nodes_of_class(nodes.Return, skip_klass=nodes.FunctionDef) | ||
) | ||
for ret_node in return_nodes: | ||
if not ( | ||
isinstance(ret_node.value, nodes.Const) | ||
and ret_node.value.value is None | ||
or ret_node.value is None | ||
): | ||
self.add_message("function-return-not-assigned", node=node) | ||
return | ||
|
||
|
||
def register(linter: PyLinter) -> None: | ||
linter.register_checker(FunctionReturnNotAssignedChecker(linter)) |
56 changes: 56 additions & 0 deletions
56
tests/functional/ext/function_return_not_assigned/function_return_not_assigned.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# pylint: disable=missing-function-docstring, missing-module-docstring, missing-class-docstring, expression-not-assigned, invalid-name | ||
from dataclasses import dataclass, replace | ||
|
||
|
||
def func_that_returns_something(): | ||
return 1 | ||
|
||
|
||
func_that_returns_something() # [function-return-not-assigned] | ||
|
||
_ = func_that_returns_something() | ||
|
||
if func_that_returns_something(): | ||
pass | ||
|
||
|
||
def func_that_returns_none(): | ||
return None | ||
|
||
|
||
def func_with_no_explicit_return(): | ||
print("I am doing something") | ||
|
||
|
||
func_that_returns_none() | ||
func_with_no_explicit_return() | ||
|
||
some_var = "" | ||
# next line should probably raise? | ||
func_that_returns_something() if some_var else func_that_returns_none() | ||
_ = func_that_returns_something() if some_var else func_that_returns_none() | ||
func_with_no_explicit_return() if some_var else func_that_returns_none() | ||
|
||
|
||
@dataclass | ||
class TestClass: | ||
value: int | ||
|
||
def return_self(self): | ||
return self | ||
|
||
def return_none(self): | ||
pass | ||
|
||
|
||
inst = TestClass(1) | ||
inst.return_self() # [function-return-not-assigned] | ||
inst.return_none() | ||
|
||
replace(inst, value=3) # [function-return-not-assigned] | ||
|
||
inst = replace(inst, value=3) | ||
|
||
inst.return_self() # [function-return-not-assigned] | ||
inst.return_none() | ||
inst = inst.return_self() |
2 changes: 2 additions & 0 deletions
2
tests/functional/ext/function_return_not_assigned/function_return_not_assigned.rc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[MAIN] | ||
load-plugins=pylint.extensions.function_return_not_assigned, |
4 changes: 4 additions & 0 deletions
4
tests/functional/ext/function_return_not_assigned/function_return_not_assigned.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function-return-not-assigned:9:0:9:29::Function returned value which is never used:UNDEFINED | ||
function-return-not-assigned:47:0:47:18::Function returned value which is never used:UNDEFINED | ||
function-return-not-assigned:50:0:50:22::Function returned value which is never used:UNDEFINED | ||
function-return-not-assigned:54:0:54:18::Function returned value which is never used:UNDEFINED |
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parent of all three
func_that_returns_something
some_var
andfunc_that_returns_none
is IfExpr. Ifsome_var
would be function call, I would consider it to be used whilefunc_that_returns_something
func_that_returns_none
not.func_that_returns_something
should raisefunction-return-not-assigned