Skip to content

[refurb] Fix FURB163 autofix creating a syntax error for yield expressions - #18756

Merged
MichaReiser merged 4 commits into
astral-sh:mainfrom
LaBatata101:fix-FURB163
Jun 23, 2025
Merged

MichaReiser merged 4 commits into
astral-sh:mainfrom
LaBatata101:fix-FURB163

Conversation

@LaBatata101

Copy link
Copy Markdown
Contributor

Summary

Fixes #18747

Test Plan

Add regression test

@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Jun 18, 2025

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! I tested a couple of other potentially-tricky cases like await and if expressions, but I think yield and yield from are the only problematic forms. We just need to resolve the merge conflicts, and then this is good to go.

@LaBatata101

Copy link
Copy Markdown
Contributor Author

Looks good, thanks! I tested a couple of other potentially-tricky cases like await and if expressions, but I think yield and yield from are the only problematic forms. We just need to resolve the merge conflicts, and then this is good to go.

Done!

@LaBatata101
LaBatata101 requested a review from ntBre June 18, 2025 19:51
@github-actions

github-actions Bot commented Jun 18, 2025 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

)?;

let number = checker.locator().slice(arg);
let arg_str = if matches!(arg, Expr::Yield(_) | Expr::YieldFrom(_)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use OperatorPrecedence here instead of hard coding Yield and YieldFrom

@LaBatata101 LaBatata101 Jun 20, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any benefit of using OperatorPrecedence over the matches!(arg, Expr::Yield(_) | Expr::YieldFrom(_))?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would be more general in case something changes in the future and also make it easier to find precedence related checks. I haven't actually used OperatorPrecedence myself, but it looks like Yield has the lowest precedence, so something like OperatorPrecedence::from(arg) <= OperatorPrecedence::Yield should be equivalent to the matches! call?

Is that what you had in mind @MichaReiser?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the fix. I'm also wondering if we could simply preserve any existing parentheses because that would fix it too.

OperatorPrecedence::from(arg) <= OperatorPrecedence::Yield

Roughly. Ideally there would be a , OperatorPrecedence but that doesn't exist. The main advantage is that this rule would be correct if we ever end up introducing a new OperatorPrecedence that binds lower than Yield (and we can check all Yield usages if it binds one level higher than Yield).

@LaBatata101
LaBatata101 requested a review from MichaReiser June 21, 2025 21:33

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@MichaReiser
MichaReiser merged commit 0ce022e into astral-sh:main Jun 23, 2025
@LaBatata101
LaBatata101 deleted the fix-FURB163 branch June 23, 2025 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB163 fix should parenthesize yield

3 participants