Skip to content
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

PLR1711 (useless-return) does not check return statements if they are indented. #11841

Open
yuji38kwmt opened this issue Jun 12, 2024 · 1 comment
Labels
rule Implementing or modifying a lint rule

Comments

@yuji38kwmt
Copy link

yuji38kwmt commented Jun 12, 2024

Description

PLR1711 (useless-return) does not check return statements if they are indented.

But with Pylint, R1711 checks.

I expect Ruff to behave in the same way as Pylint.

sample.py

def foo(
    value_list: list[str],
) -> None:
    if len(value_list) == 0:
        print(f"a")
    else:
        print("b")
        return

Checks with Ruff

$ ruff --version
ruff 0.4.8

$ ruff check --select PLR1711 sample.py
All checks passed!

Checks with Pylint

$ pylint --version
pylint 3.2.3
astroid 3.2.2
Python 3.12.1 (main, Feb  7 2024, 10:25:21) [GCC 11.4.0]

$ pylint sample.py --enable R1711 --disable C,W
************* Module sample
sample.py:1:0: R1711: Useless return at end of function or method (useless-return)

------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 6.00/10, +2.00)

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Jun 12, 2024
@dhruvmanila
Copy link
Member

We could possibly use AnyNodeRef::from(node).last_child_in_body

/// The last child of the last branch, if the node has multiple branches.
pub fn last_child_in_body(&self) -> Option<AnyNodeRef<'a>> {
to get the last statement although I think there might be other nuances which we need to consider to match Pylint's behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants