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?] B018 useless-expression does not work #3453

Closed
smackesey opened this issue Mar 11, 2023 · 7 comments · Fixed by #3455
Closed

[bug?] B018 useless-expression does not work #3453

smackesey opened this issue Mar 11, 2023 · 7 comments · Fixed by #3455
Labels
rule Implementing or modifying a lint rule

Comments

@smackesey
Copy link

smackesey commented Mar 11, 2023

None of the following trigger B018 useless-expression for me, when I would expect them all to:

"foo" + "bar"  # should trigger B018

"foo"  # should trigger B018

object().__class__  # should trigger B018

I might be missing something about what this rule is supposed to do though.

@charliermarsh
Copy link
Member

I think this is consistent with flake8-bugbear although I'm not sure why their rule is limited to those cases.

@charliermarsh
Copy link
Member

I'm guessing they got rid of strings to avoid false positives with docstrings (https://github.com/PyCQA/flake8-bugbear/pull/209/files).

I'm guessing they don't detect function calls since it's not really possible (with current information) to know whether a function call is effect-ful or pure.

We could definitely detect binary operations though.

@charliermarsh
Copy link
Member

In general I think we can improve this even if it deviates a bit from bugbear.

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Mar 12, 2023
@smackesey
Copy link
Author

I was actually unable to get this rule to trigger at all-- do you have an example of something that triggers it? Like, 1 + 5 doesn't trigger it either.

I'm guessing they don't detect function calls since it's not really possible (with current information) to know whether a function call is effect-ful or pure.

Yeah, the expected behavior is not to detect a straight function call, but when it's followed by attribute access (as in my example) then I would think it should be detected. FWIW that's pylint's behavior.

@charliermarsh
Copy link
Member

Prior to my open PR, the rule was only enforced in function and class bodies. I think this was consistent with bugbear but has since changed, so it’s reflected in the PR.

@smackesey
Copy link
Author

Ah nice, somehow my eyes glossed over the PR notification. Thx for addressing.

@charliermarsh
Copy link
Member

All good, that PR still doesn't include the object().__class__ case which I may add.

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

Successfully merging a pull request may close this issue.

2 participants