-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pylint] Implement unnecessary-lambda (W0108) (#7953)
This is my first PR and I'm new at rust, so feel free to ask me to rewrite everything if needed ;) The rule must be called after deferred lambas have been visited because of the last check (whether the lambda parameters are used in the body of the function that's being called). I didn't know where to do it, so I did what I could to be able to work on the rule itself: - added a `ruff_linter::checkers::ast::analyze::lambda` module - build a vec of visited lambdas in `visit_deferred_lambdas` - call `analyze::lambda` on the vec after they all have been visited Building that vec of visited lambdas was necessary so that bindings could be properly resolved in the case of nested lambdas. Note that there is an open issue in pylint for some false positives, do we need to fix that before merging the rule? pylint-dev/pylint#8192 Also, I did not provide any fixes (yet), maybe we want do avoid merging new rules without fixes? ## Summary Checks for lambdas whose body is a function call on the same arguments as the lambda itself. ### Bad ```python df.apply(lambda x: str(x)) ``` ### Good ```python df.apply(str) ``` ## Test Plan Added unit test and snapshot. Manually compared pylint and ruff output on pylint's test cases. ## References - [pylint documentation](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/unnecessary-lambda.html) - [pylint implementation](https://github.com/pylint-dev/pylint/blob/main/pylint/checkers/base/basic_checker.py#L521-L587) - #970
- Loading branch information
Showing
11 changed files
with
388 additions
and
7 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_lambda.py
This file contains 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,59 @@ | ||
_ = lambda: print() # [unnecessary-lambda] | ||
_ = lambda x, y: min(x, y) # [unnecessary-lambda] | ||
|
||
_ = lambda *args: f(*args) # [unnecessary-lambda] | ||
_ = lambda **kwargs: f(**kwargs) # [unnecessary-lambda] | ||
_ = lambda *args, **kwargs: f(*args, **kwargs) # [unnecessary-lambda] | ||
_ = lambda x, y, z, *args, **kwargs: f(x, y, z, *args, **kwargs) # [unnecessary-lambda] | ||
|
||
_ = lambda x: f(lambda x: x)(x) # [unnecessary-lambda] | ||
_ = lambda x, y: f(lambda x, y: x + y)(x, y) # [unnecessary-lambda] | ||
|
||
# default value in lambda parameters | ||
_ = lambda x=42: print(x) | ||
|
||
# lambda body is not a call | ||
_ = lambda x: x | ||
|
||
# ignore chained calls | ||
_ = lambda: chained().call() | ||
|
||
# lambda has kwargs but not the call | ||
_ = lambda **kwargs: f() | ||
|
||
# lambda has kwargs and the call has kwargs named differently | ||
_ = lambda **kwargs: f(**dict([('forty-two', 42)])) | ||
|
||
# lambda has kwargs and the call has unnamed kwargs | ||
_ = lambda **kwargs: f(**{1: 2}) | ||
|
||
# lambda has starred parameters but not the call | ||
_ = lambda *args: f() | ||
|
||
# lambda has starred parameters and the call has starargs named differently | ||
_ = lambda *args: f(*list([3, 4])) | ||
# lambda has starred parameters and the call has unnamed starargs | ||
_ = lambda *args: f(*[3, 4]) | ||
|
||
# lambda has parameters but not the call | ||
_ = lambda x: f() | ||
_ = lambda *x: f() | ||
_ = lambda **x: f() | ||
|
||
# lambda parameters and call args are not the same length | ||
_ = lambda x, y: f(x) | ||
|
||
# lambda parameters and call args are not in the same order | ||
_ = lambda x, y: f(y, x) | ||
|
||
# lambda parameters and call args are not the same | ||
_ = lambda x: f(y) | ||
|
||
# the call uses the lambda parameters | ||
_ = lambda x: x(x) | ||
_ = lambda x, y: x(x, y) | ||
_ = lambda x: z(lambda y: x + y)(x) | ||
|
||
# lambda uses an additional keyword | ||
_ = lambda *args: f(*args, y=1) | ||
_ = lambda *args: f(*args, y=x) |
23 changes: 23 additions & 0 deletions
23
crates/ruff_linter/src/checkers/ast/analyze/deferred_lambdas.rs
This file contains 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,23 @@ | ||
use ruff_python_ast::Expr; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::codes::Rule; | ||
use crate::rules::pylint; | ||
|
||
/// Run lint rules over all deferred lambdas in the [`SemanticModel`]. | ||
pub(crate) fn deferred_lambdas(checker: &mut Checker) { | ||
while !checker.deferred.lambdas.is_empty() { | ||
let lambdas = std::mem::take(&mut checker.deferred.lambdas); | ||
for snapshot in lambdas { | ||
checker.semantic.restore(snapshot); | ||
|
||
let Some(Expr::Lambda(lambda)) = checker.semantic.current_expression() else { | ||
unreachable!("Expected Expr::Lambda"); | ||
}; | ||
|
||
if checker.enabled(Rule::UnnecessaryLambda) { | ||
pylint::rules::unnecessary_lambda(checker, lambda); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.