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

[Bug]: FURB126 shouldn't catch matches on complex expressions #336

Open
2 of 3 tasks
mscheifer opened this issue Apr 26, 2024 · 0 comments
Open
2 of 3 tasks

[Bug]: FURB126 shouldn't catch matches on complex expressions #336

mscheifer opened this issue Apr 26, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@mscheifer
Copy link

Has your issue already been fixed?

  • Have you checked to see if your issue still exists on the master branch? See the docs for instructions on how to setup a local build of Refurb.
  • Have you looked at the open/closed issues to see if anyone has already reported your issue?
  • If reporting a false positive/incorrect suggestion, have you double checked that the suggested fix changes the code semantics?

The Bug

The following code:

def expensive_func_or_complex_expression(a):
    # <snip logic...>
    return a


def foo(bar):
    match expensive_func_or_complex_expression(bar):
        case "a":
            return 123
        case result:
            return result

Emits the following error:

$ refurb testcase.py
testcase.py:11:13 [FURB126]: Replace `case _: return x` with `return x`

Run `refurb --explain ERR` to further explain an error. Use `--quiet` to silence this message

But it should not be emitting an error instance because the match is on an expression that I don't want to repeat.

The most straightforward interpretation of the suggested fix would be doing:

def foo(bar):
    match expensive_func_or_complex_expression(bar):
        case "a":
            return 123
    return expensive_func_or_complex_expression(bar)

but that's bad.

Doing

def foo(bar):
    result = expensive_func_or_complex_expression(bar)
    match result:
        case "a":
            return 123
    return result

is not too bad but I prefer the original form. This also isn't a super obvious change to make based on the error suggestion as it mentions case _ but I'm not using the underscore pattern.

Version Info

Refurb: v2.0.0
Mypy: v1.9.0

Python Version

Python 3.10.12

Config File

# N/A

Extra Info

None

@mscheifer mscheifer added the bug Something isn't working label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants