Skip to content

Commit dac7efc

Browse files
committed
Simplify boolean expression in manual_assert
1 parent 59cd37c commit dac7efc

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::{is_panic, root_macro_call};
3-
use clippy_utils::{is_else_clause, is_parent_stmt, peel_blocks_with_stmt, span_extract_comment, sugg};
3+
use clippy_utils::{higher, is_else_clause, is_parent_stmt, peel_blocks_with_stmt, span_extract_comment, sugg};
44
use rustc_errors::Applicability;
5-
use rustc_hir::{Expr, ExprKind, UnOp};
5+
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass, LintContext};
77
use rustc_session::declare_lint_pass;
88

@@ -35,7 +35,7 @@ declare_lint_pass!(ManualAssert => [MANUAL_ASSERT]);
3535

3636
impl<'tcx> LateLintPass<'tcx> for ManualAssert {
3737
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
38-
if let ExprKind::If(cond, then, None) = expr.kind
38+
if let Some(higher::If { cond, then, r#else: None }) = higher::If::hir(expr)
3939
&& !matches!(cond.kind, ExprKind::Let(_))
4040
&& !expr.span.from_expansion()
4141
&& let then = peel_blocks_with_stmt(then)
@@ -51,19 +51,13 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
5151
&& !is_else_clause(cx.tcx, expr)
5252
{
5353
let mut applicability = Applicability::MachineApplicable;
54-
let cond = cond.peel_drop_temps();
5554
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
5655
if !comments.is_empty() {
5756
comments += "\n";
5857
}
59-
let (cond, not) = match cond.kind {
60-
ExprKind::Unary(UnOp::Not, e) => (e, ""),
61-
_ => (cond, "!"),
62-
};
63-
let cond_sugg =
64-
sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability).maybe_paren();
58+
let cond_sugg = !sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability);
6559
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
66-
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
60+
let sugg = format!("assert!({cond_sugg}, {format_args_snip}){semicolon}");
6761
// we show to the user the suggestion without the comments, but when applying the fix, include the
6862
// comments in the block
6963
span_lint_and_then(

tests/ui/manual_assert.edition2018.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ LL - comment */
167167
LL - /// Doc comment
168168
LL - panic!("panic with comment") // comment after `panic!`
169169
LL - }
170-
LL + assert!(!(a > 2), "panic with comment");
170+
LL + assert!(a <= 2, "panic with comment");
171171
|
172172

173173
error: only a `panic!` in `if`-then statement
@@ -186,7 +186,7 @@ LL - const BAR: () = if N == 0 {
186186
LL -
187187
LL - panic!()
188188
LL - };
189-
LL + const BAR: () = assert!(!(N == 0), );
189+
LL + const BAR: () = assert!(N != 0, );
190190
|
191191

192192
error: only a `panic!` in `if`-then statement

tests/ui/manual_assert.edition2021.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ LL - comment */
167167
LL - /// Doc comment
168168
LL - panic!("panic with comment") // comment after `panic!`
169169
LL - }
170-
LL + assert!(!(a > 2), "panic with comment");
170+
LL + assert!(a <= 2, "panic with comment");
171171
|
172172

173173
error: only a `panic!` in `if`-then statement
@@ -186,7 +186,7 @@ LL - const BAR: () = if N == 0 {
186186
LL -
187187
LL - panic!()
188188
LL - };
189-
LL + const BAR: () = assert!(!(N == 0), );
189+
LL + const BAR: () = assert!(N != 0, );
190190
|
191191

192192
error: only a `panic!` in `if`-then statement

0 commit comments

Comments
 (0)