]> git.lizzy.rs Git - rust.git/commitdiff
Don't lint `manual_assert` in `else if`
authorJason Newcomb <jsnewcomb@pm.me>
Thu, 1 Dec 2022 17:30:19 +0000 (12:30 -0500)
committerJason Newcomb <jsnewcomb@pm.me>
Thu, 1 Dec 2022 22:37:18 +0000 (17:37 -0500)
clippy_lints/src/manual_assert.rs
tests/ui/manual_assert.edition2018.fixed
tests/ui/manual_assert.edition2021.fixed
tests/ui/manual_assert.edition2021.stderr
tests/ui/manual_assert.rs

index b8ed9b9ec18f718b0cc56160b0966a9e0cf220a5..4277455a3a21c8e096e0c36e588724d065372d99 100644 (file)
@@ -2,7 +2,7 @@
 use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
 use clippy_utils::source::snippet_with_applicability;
-use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg};
+use clippy_utils::{is_else_clause, peel_blocks_with_stmt, span_extract_comment, sugg};
 use rustc_errors::Applicability;
 use rustc_hir::{Expr, ExprKind, UnOp};
 use rustc_lint::{LateContext, LateLintPass};
@@ -47,6 +47,10 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
             if cx.tcx.item_name(macro_call.def_id) == sym::panic;
             if !cx.tcx.sess.source_map().is_multiline(cond.span);
             if let Some(format_args) = FormatArgsExpn::find_nested(cx, then, macro_call.expn);
+            // Don't change `else if foo { panic!(..) }` to `else { assert!(foo, ..) }` as it just
+            // shuffles the condition around.
+            // Should this have a config value?
+            if !is_else_clause(cx.tcx, expr);
             then {
                 let mut applicability = Applicability::MachineApplicable;
                 let format_args_snip = snippet_with_applicability(cx, format_args.inputs_span(), "..", &mut applicability);
index c9a819ba535449a1b8d90b2324204f74b318a585..638320dd6eec439a76ea745736dd2b7fcfd1672f 100644 (file)
@@ -62,6 +62,11 @@ fn main() {
         panic!("panic5");
     }
     assert!(!a.is_empty(), "with expansion {}", one!());
+    if a.is_empty() {
+        let _ = 0;
+    } else if a.len() == 1 {
+        panic!("panic6");
+    }
 }
 
 fn issue7730(a: u8) {
index 2f62de51cadcd948a98c0b75668aa2f13c6b4a92..8c7e919bf62a10d52bc6a343454b438f89c8857b 100644 (file)
@@ -50,6 +50,11 @@ fn main() {
     assert!(!(b.is_empty() || a.is_empty()), "panic4");
     assert!(!(a.is_empty() || !b.is_empty()), "panic5");
     assert!(!a.is_empty(), "with expansion {}", one!());
+    if a.is_empty() {
+        let _ = 0;
+    } else if a.len() == 1 {
+        panic!("panic6");
+    }
 }
 
 fn issue7730(a: u8) {
index 237638ee1344c60274bb31c1accbcf7f7456cee3..3555ac29243a1cb0d819a368d8bb22bf02aea2de 100644 (file)
@@ -65,7 +65,7 @@ LL | |     }
    | |_____^ help: try instead: `assert!(!a.is_empty(), "with expansion {}", one!());`
 
 error: only a `panic!` in `if`-then statement
-  --> $DIR/manual_assert.rs:73:5
+  --> $DIR/manual_assert.rs:78:5
    |
 LL | /     if a > 2 {
 LL | |         // comment
index 6a4cc2468d4193815b6b43299db873b95d8fb33d..f037c5b8405c721cc6755b1874cf5d47f229206b 100644 (file)
@@ -66,6 +66,11 @@ fn main() {
     if a.is_empty() {
         panic!("with expansion {}", one!())
     }
+    if a.is_empty() {
+        let _ = 0;
+    } else if a.len() == 1 {
+        panic!("panic6");
+    }
 }
 
 fn issue7730(a: u8) {