mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00

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? https://github.com/pylint-dev/pylint/issues/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) - https://github.com/astral-sh/ruff/issues/970
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
_ = 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)
|