-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add required parentheses around method receiver #12851
Conversation
clippy_lints/src/needless_bool.rs
Outdated
if condition_needs_parentheses(cond) && is_parent_stmt(cx, e.hir_id) { | ||
if (condition_needs_parentheses(cond) && is_parent_stmt(cx, e.hir_id)) | ||
|| is_receiver_of_method_call(cx, e) | ||
{ | ||
snip = snip.maybe_par(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most lints just unconditionally call maybe_par
since that can already figure out on its own if it needs parentheses, I wonder why we aren't just doing this here? Would this otherwise add too many unnecessary parens?
FWIW, method call receivers are not the only thing where we'd need parentheses. &&
has very low precedence so they are needed for almost any parent expression (e.g. any binary expression other than ||
).
let (a, b) = Default::default();
_ = if a && b { true } else { false } as u8; // should also suggest `(a && b) as u8`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most lints just unconditionally call
maybe_par
since that can already figure out on its own if it needs parentheses, I wonder why we aren't just doing this here? Would this otherwise add too many unnecessary parens?
Yup.
FWIW, method call receivers are not the only thing where we'd need parentheses.
&&
has very low precedence so they are needed for almost any parent expression (e.g. any binary expression other than||
).let (a, b) = Default::default(); _ = if a && b { true } else { false } as u8; // should also suggest `(a && b) as u8`
I'll check this one as well, I'll put the PR in WIP mode in the meantime.
☔ The latest upstream changes (presumably #12842) made this pull request unmergeable. Please resolve the merge conflicts. |
@y21 I've handled the |
Ok, fair, thanks! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fix #12846
changelog: [
needless_bool
]: Add missing parentheses around method receiver