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

Make if_then_panic handle situation of BinOpKind::And || BinOpKind::Or #7741

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions clippy_lints/src/if_then_panic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::higher::PanicExpn;
use clippy_utils::is_expn_of;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{is_expn_of, sugg};
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, StmtKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -74,12 +74,14 @@ impl LateLintPass<'_> for IfThenPanic {
};
let mut applicability = Applicability::MachineApplicable;
let sugg = snippet_with_applicability(cx, span, "..", &mut applicability);

let cond_sugg =
if let ExprKind::DropTemps(Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..}) = cond.kind {
snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string()
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())
Comment on lines +77 to +84
Copy link
Member

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.

Copy link
Contributor Author

@surechen surechen Oct 1, 2021

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.

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.

Copy link
Member

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.

};

span_lint_and_sugg(
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/if_then_panic.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ fn main() {
} else {
println!("qwq");
}
let b = vec![1, 2, 3];
assert!(!b.is_empty(), "panic1");
assert!(!(b.is_empty() && a.is_empty()), "panic2");
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
assert!(!(b.is_empty() || a.is_empty()), "panic4");
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
}
16 changes: 16 additions & 0 deletions tests/ui/if_then_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ fn main() {
} else {
println!("qwq");
}
let b = vec![1, 2, 3];
if b.is_empty() {
panic!("panic1");
}
if b.is_empty() && a.is_empty() {
panic!("panic2");
}
if a.is_empty() && !b.is_empty() {
panic!("panic3");
}
if b.is_empty() || a.is_empty() {
panic!("panic4");
}
if a.is_empty() || !b.is_empty() {
panic!("panic5");
}
}
42 changes: 41 additions & 1 deletion tests/ui/if_then_panic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,45 @@ LL | | panic!("qwqwq");
LL | | }
| |_____^ help: try: `assert!(a.is_empty(), "qwqwq");`

error: aborting due to 2 previous errors
error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:39:5
|
LL | / if b.is_empty() {
LL | | panic!("panic1");
LL | | }
| |_____^ help: try: `assert!(!b.is_empty(), "panic1");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:42:5
|
LL | / if b.is_empty() && a.is_empty() {
LL | | panic!("panic2");
LL | | }
| |_____^ help: try: `assert!(!(b.is_empty() && a.is_empty()), "panic2");`

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");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:48:5
|
LL | / if b.is_empty() || a.is_empty() {
LL | | panic!("panic4");
LL | | }
| |_____^ help: try: `assert!(!(b.is_empty() || a.is_empty()), "panic4");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:51:5
|
LL | / if a.is_empty() || !b.is_empty() {
LL | | panic!("panic5");
LL | | }
| |_____^ help: try: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`

error: aborting due to 7 previous errors