-
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
Make if_then_panic handle situation of BinOpKind::And || BinOpKind::Or #7741
Conversation
Make if_then_else handle situation of cond.kind = ExprKind::DropTemps(ExprKind::Binary(BinOpKind::And || BinOpKind::Or, left, right), ..) =
That's weird. I just self-assign it to me :) |
I think your solution is more difficult than it has to be. The easiest fix for this would be to put the |
This would also handle operators like Edit: there doesn't seem to be a simplification for |
Hi, Done. clippy_utils::Sugg is very cool. Thank you very much. |
Agree, the various possibilities here are very complicated. Using sugg is better than trying to cover various possible situations in this lint. |
let cond_sugg = if let ExprKind::DropTemps(e, ..) = cond.kind { | ||
if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = e { | ||
sugg::Sugg::hir_with_applicability(cx, not_expr, "..", &mut applicability).maybe_par().to_string() | ||
} else { | ||
format!("!{}", sugg::Sugg::hir_with_applicability(cx, e, "..", &mut applicability).maybe_par().to_string()) | ||
} | ||
} else { | ||
format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability)) | ||
format!("!{}", sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par().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.
cond
and e
should have the same Span
, so the two format!
calls are pretty much equivalent. I think you only have to change the else
case and not the ExprKind::Unary
case.
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.
cond
ande
should have the sameSpan
, so the twoformat!
calls are pretty much equivalent. I think you only have to change theelse
case and not theExprKind::Unary
case.
Hi, I thought the same at the beginning, but if I only modify the else block:
let cond_sugg = if let ExprKind::DropTemps(Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..}, ..) = cond.kind {
sugg::Sugg::hir_with_applicability(cx, not_expr, "..", &mut applicability).maybe_par().to_string()
} else {
format!("!{}", sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par().to_string())
};
result will be like this:
error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:45:5
|
LL | / if a.is_empty() && !b.is_empty() {
LL | | panic!("panic3");
LL | | }
| |_____^ help: try: `assert!(!a.is_empty() && !b.is_empty(), "panic3");`
So I think "a.is_empty() && !b.is_empty()" is ExprKind::DropTemps{Expr{kind: ExprKind::Binary(BinOpKind::And, left, right), ..}} here.
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.
Hm, that's weird. I also just tested it locally. In that case, let's merge it as it is.
@teor2345 There is the |
@bors r+ Thanks! |
📌 Commit 41e2c68 has been approved by |
Make if_then_panic handle situation of BinOpKind::And || BinOpKind::Or fixes #7731 Make if_then_panic handle situation of cond.kind = ExprKind::DropTemps(ExprKind::Binary(BinOpKind::And || BinOpKind::Or, left, right), ..) =
💔 Test failed - checks-action_test |
@bors retry (added changelog) |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes #7731
Make if_then_panic handle situation of cond.kind = ExprKind::DropTemps(ExprKind::Binary(BinOpKind::And || BinOpKind::Or, left, right), ..) =
changelog: [
if_then_panic
] Fix suggestion for more complex conditions