Skip to content

Commit

Permalink
use sugg::Sugg::maybe_par
Browse files Browse the repository at this point in the history
  • Loading branch information
surechen committed Oct 1, 2021
1 parent 4babfae commit 41e2c68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
36 changes: 5 additions & 31 deletions clippy_lints/src/if_then_panic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
use rustc_hir::{Block, Expr, ExprKind, StmtKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down Expand Up @@ -74,40 +74,14 @@ impl LateLintPass<'_> for IfThenPanic {
};
let mut applicability = Applicability::MachineApplicable;
let sugg = snippet_with_applicability(cx, span, "..", &mut applicability);
//let mut cond_sugg = format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability));
let cond_sugg = if let ExprKind::DropTemps(e, ..) = cond.kind {
if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = e {
snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string()
} else if let Expr{kind: ExprKind::Binary(op, left, right), ..} = e {//BinOp{BinOpKind::And, ..}
match op.node {
BinOpKind::And | BinOpKind::Or => {
let left_span = {
if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = left {
snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string()
} else {
format!("!{}", snippet_with_applicability(cx, left.span, "..", &mut applicability))
}
};
let right_span = {
if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = right {
snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string()
} else {
format!("!{}", snippet_with_applicability(cx, right.span, "..", &mut applicability))
}
};
if op.node == BinOpKind::And {
format!("{} || {}", left_span, right_span)
} else {
format!("{} && {}", left_span, right_span)
}
}
_ => format!("!({})", snippet_with_applicability(cx, cond.span, "..", &mut applicability))
}
sugg::Sugg::hir_with_applicability(cx, not_expr, "..", &mut applicability).maybe_par().to_string()
} else {
format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability))
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())
};

span_lint_and_sugg(
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/if_then_panic.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ fn main() {
}
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");
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");
}
8 changes: 4 additions & 4 deletions tests/ui/if_then_panic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ error: only a `panic!` in `if`-then statement
LL | / if b.is_empty() && a.is_empty() {
LL | | panic!("panic2");
LL | | }
| |_____^ help: try: `assert!(!b.is_empty() || !a.is_empty(), "panic2");`
| |_____^ 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");`
| |_____^ 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");`
| |_____^ 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");`
| |_____^ help: try: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`

error: aborting due to 7 previous errors

0 comments on commit 41e2c68

Please sign in to comment.