Skip to content

Nonsensical returns are not reported as useless-return if they are indented #9449

@e-gebes

Description

@e-gebes

Bug description

# pylint: disable=missing-module-docstring,missing-function-docstring


def function1(parameter):
    parameter.do()
    return  # this is reported as `useless-return` as expected


def function2(parameter):
    if parameter:
        return
    return  # this is not reported as `useless-return`


def function3(parameter):
    if parameter:
        pass
    else:
        return  # this is not reported as `useless-return`


def function4(parameter):
    if parameter:
        return
    else:  # this is reported as `no-else-return` as expected
        return  # this is not reported as `useless-return`


def function5(parameter):
    try:
        parameter.do()
    except RuntimeError:
        parameter.other()
        return  # this is not reported as `useless-return`


def function6(parameter):
    try:
        parameter.do()
    except RuntimeError:
        return  # maybe even this should be reported - `pass` can be used

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
4:0: R1711: Useless return at end of function or method (useless-return)
23:4: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return)

Expected behavior

Maybe more useless-return messages should be reported, see the comments in the code snippet.

Pylint version

pylint 2.17.7
astroid 2.15.8
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions