|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 | 2 | use clippy_utils::higher::PanicExpn;
|
3 |
| -use clippy_utils::is_expn_of; |
4 | 3 | use clippy_utils::source::snippet_with_applicability;
|
| 4 | +use clippy_utils::{is_expn_of, sugg}; |
5 | 5 | use rustc_errors::Applicability;
|
6 |
| -use rustc_hir::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp}; |
| 6 | +use rustc_hir::{Block, Expr, ExprKind, StmtKind, UnOp}; |
7 | 7 | use rustc_lint::{LateContext, LateLintPass};
|
8 | 8 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
9 | 9 |
|
@@ -74,40 +74,14 @@ impl LateLintPass<'_> for IfThenPanic {
|
74 | 74 | };
|
75 | 75 | let mut applicability = Applicability::MachineApplicable;
|
76 | 76 | let sugg = snippet_with_applicability(cx, span, "..", &mut applicability);
|
77 |
| - //let mut cond_sugg = format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability)); |
78 | 77 | let cond_sugg = if let ExprKind::DropTemps(e, ..) = cond.kind {
|
79 | 78 | if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = e {
|
80 |
| - snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string() |
81 |
| - } else if let Expr{kind: ExprKind::Binary(op, left, right), ..} = e {//BinOp{BinOpKind::And, ..} |
82 |
| - match op.node { |
83 |
| - BinOpKind::And | BinOpKind::Or => { |
84 |
| - let left_span = { |
85 |
| - if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = left { |
86 |
| - snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string() |
87 |
| - } else { |
88 |
| - format!("!{}", snippet_with_applicability(cx, left.span, "..", &mut applicability)) |
89 |
| - } |
90 |
| - }; |
91 |
| - let right_span = { |
92 |
| - if let Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..} = right { |
93 |
| - snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string() |
94 |
| - } else { |
95 |
| - format!("!{}", snippet_with_applicability(cx, right.span, "..", &mut applicability)) |
96 |
| - } |
97 |
| - }; |
98 |
| - if op.node == BinOpKind::And { |
99 |
| - format!("{} || {}", left_span, right_span) |
100 |
| - } else { |
101 |
| - format!("{} && {}", left_span, right_span) |
102 |
| - } |
103 |
| - } |
104 |
| - _ => format!("!({})", snippet_with_applicability(cx, cond.span, "..", &mut applicability)) |
105 |
| - } |
| 79 | + sugg::Sugg::hir_with_applicability(cx, not_expr, "..", &mut applicability).maybe_par().to_string() |
106 | 80 | } else {
|
107 |
| - format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability)) |
| 81 | + format!("!{}", sugg::Sugg::hir_with_applicability(cx, e, "..", &mut applicability).maybe_par().to_string()) |
108 | 82 | }
|
109 | 83 | } else {
|
110 |
| - format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability)) |
| 84 | + format!("!{}", sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par().to_string()) |
111 | 85 | };
|
112 | 86 |
|
113 | 87 | span_lint_and_sugg(
|
|
0 commit comments