Skip to content

no-else-return / R1705 instructs users to use error-prone construction #9274

Open
@aes

Description

@aes

Bug description

This triggers warnings no-else-return and inconsistent-return-statements:

    def fuuu(thing):
        """Demo function"""
        if thing == "some":
            result = handle_the_something_case(...)
            return result
        elif thing == "boring":
            happen = "boring"
        elif thing in other:
            happen = set_us_up_the_bomb(...)
            return happen
        else:
            raise TypeError("Unknown case")

and after "fixing" it, this gives no warnings:

    def fuuu(thing):
        """Demo function"""
        if thing == "some":
            result = handle_the_something_case(...)
            return result
        if thing == "boring":
            happen = "boring"
        if thing in other:
            happen = set_us_up_the_bomb(...)
            return happen
    
        raise TypeError("Unknown case")

There are some problems with this. One is that it is no longer obvious that the if-statement should be total. (That is, that it should cover all cases.) Another is the (potentially unexpected) 'other' to 'boring' case fall-through.

Note that pylint explicitly tells users to express their code in the latter, DANGEROUS, way.

Configuration

No response

Command used

pylint demo.py

Pylint output

************* Module demo
demo.py:8:4: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
demo.py:6:0: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)

------------------------------------------------------------------
Your code has been rated at 9.13/10 (previous run: 8.46/10, +0.67)

Expected behavior

I would expect pylint to direct users towards safer ways of expressing their code.

Pylint version

pylint 2.15.6
astroid 2.13.5
Python 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0]

OS / Environment

Debian GNU/Linux trixie/sid

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Documentation 📗Help wanted 🙏Outside help would be appreciated, good for new contributorsNeeds 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