-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Account for new assert!
desugaring in !condition
suggestion
#15453
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
Conversation
rustbot has assigned @samueltardieu. Use |
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.
Could you please add this implementation and those tests?
impl Add for ImplNotTraitWithBool {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
self
}
}
[…]
assert_eq!(!b, true);
//~^ bool_assert_comparison
assert_eq!(!b, false);
//~^ bool_assert_comparison
assert_eq!(b + b, true);
//~^ bool_assert_comparison
assert_eq!(b + b, false);
//~^ bool_assert_comparison
Right now, the assert_eq!(b + b, true)
will fail once fixed because it would be transformed into assert!(!!b + b)
. The proposed change fixes that.
assert_eq!(!b, false);
will get an extra pair of parentheses once fixed assert!(!(!b))
but this will be rare and can be ignored as it is only an aesthetic issue.
if let ty::Bool = non_lit_ty.kind() { | ||
if bool_value ^ eq_macro { | ||
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else { | ||
return; | ||
}; | ||
suggestions.push((non_lit_expr.span, (!sugg).to_string())); | ||
} | ||
} else { | ||
// If we have a `value` that is *not* `bool` but that `!value` *is*, we suggest | ||
// `!!value`. | ||
suggestions.push(( | ||
non_lit_expr.span.shrink_to_lo(), | ||
if bool_value ^ eq_macro { | ||
"!".to_string() | ||
} else { | ||
"!!".to_string() | ||
}, | ||
)); | ||
} |
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.
if let ty::Bool = non_lit_ty.kind() { | |
if bool_value ^ eq_macro { | |
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else { | |
return; | |
}; | |
suggestions.push((non_lit_expr.span, (!sugg).to_string())); | |
} | |
} else { | |
// If we have a `value` that is *not* `bool` but that `!value` *is*, we suggest | |
// `!!value`. | |
suggestions.push(( | |
non_lit_expr.span.shrink_to_lo(), | |
if bool_value ^ eq_macro { | |
"!".to_string() | |
} else { | |
"!!".to_string() | |
}, | |
)); | |
} | |
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else { | |
return; | |
}; | |
let sugg = if bool_value ^ eq_macro { | |
!sugg.maybe_paren() | |
} else if ty::Bool == *non_lit_ty.kind() { | |
sugg | |
} else { | |
!!sugg.maybe_paren() | |
}; | |
suggestions.push((non_lit_expr.span, sugg.to_string())); |
I would even remove the early return
here with:
if let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) {
let sugg = if bool_value ^ eq_macro {
!sugg.maybe_paren()
} else if ty::Bool == *non_lit_ty.kind() {
sugg
} else {
!!sugg.maybe_paren()
};
suggestions.push((non_lit_expr.span, sugg.to_string()));
diag.multipart_suggestion(
format!("replace it with `{non_eq_mac}!(..)`"),
suggestions,
Applicability::MachineApplicable,
);
}
`rustc` is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.
Addressed and squashed |
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.
LGTM
Concerning the process: if we merge it here, you'll have to wait at least until the next sync PR (which will be submitted on Aug. 21) is merged into the compiler repository, which might not be ideal since it blocks rust-lang/rust#122661.
Since the change is very localized, a possibility would be to resubmit it in r-l/r where I could approve it again through bors. This would supersede this PR and would remove a blocker sooner.
Tell me what you prefer.
@samueltardieu lets do the latter. I'll submit it later. |
Closing in favor of rust-lang/rust#145273 |
Account for new `assert!` desugaring in `!condition` suggestion `rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? ``@samueltardieu``
Account for new `assert!` desugaring in `!condition` suggestion `rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? ```@samueltardieu```
Account for new `assert!` desugaring in `!condition` suggestion `rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? ````@samueltardieu````
Account for new `assert!` desugaring in `!condition` suggestion `rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? `````@samueltardieu`````
Rollup merge of #145273 - estebank:not-not, r=samueltardieu Account for new `assert!` desugaring in `!condition` suggestion `rustc` in #122661 is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? `````@samueltardieu`````
Account for new `assert!` desugaring in `!condition` suggestion `rustc` in rust-lang/rust#122661 is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? `````@samueltardieu`````
rustc
in rust-lang/rust#122661 is going to change the desugaring ofassert!
to bewhich will make the edge-case of
condition
beingimpl Not<Output = bool>
while not beingbool
itself no longer a straightforward suggestion, but!!condition
will coerce the expression to bebool
, so it can be machine applicable.changelog: [
bool_assert
]: Account for newassert!
desugaring in suggestion