]> git.lizzy.rs Git - rust.git/commitdiff
`assertions_on_result_states` fix suggestion when `assert!` not in a statement
authorkraktus <kraktus@users.noreply.github.com>
Sat, 10 Sep 2022 08:30:19 +0000 (10:30 +0200)
committerkraktus <kraktus@users.noreply.github.com>
Sat, 10 Sep 2022 08:30:19 +0000 (10:30 +0200)
clippy_lints/src/assertions_on_result_states.rs
tests/ui/assertions_on_result_states.fixed
tests/ui/assertions_on_result_states.rs
tests/ui/assertions_on_result_states.stderr

index 7cd198ace86c0e3ff3f3edf4c9b44dc862b68429..63664d66662b5a103c3c2ae810135e484746e166 100644 (file)
@@ -1,9 +1,9 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
-use clippy_utils::path_res;
 use clippy_utils::source::snippet_with_context;
 use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item};
 use clippy_utils::usage::local_used_after_expr;
+use clippy_utils::{is_expr_final_block_expr, path_res};
 use rustc_errors::Applicability;
 use rustc_hir::def::Res;
 use rustc_hir::{Expr, ExprKind};
@@ -58,6 +58,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
                     return;
                 }
             }
+            let semicolon = if is_expr_final_block_expr(cx.tcx, e) {";"} else {""};
             let mut app = Applicability::MachineApplicable;
             match method_segment.ident.as_str() {
                 "is_ok" if type_suitable_to_unwrap(cx, substs.type_at(1)) => {
@@ -68,8 +69,9 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
                         "called `assert!` with `Result::is_ok`",
                         "replace with",
                         format!(
-                            "{}.unwrap()",
-                            snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
+                            "{}.unwrap(){}",
+                            snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0,
+                            semicolon
                         ),
                         app,
                     );
@@ -82,8 +84,9 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
                         "called `assert!` with `Result::is_err`",
                         "replace with",
                         format!(
-                            "{}.unwrap_err()",
-                            snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
+                            "{}.unwrap_err(){}",
+                            snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0,
+                            semicolon
                         ),
                         app,
                     );
index 795f435f24cd40b3e713959334ff266aaba3cc9f..2bb755290c508b812c6c6ce4a7b53fae5a47f521 100644 (file)
@@ -75,3 +75,9 @@ fn main() {
     let r: Result<Foo, Foo> = Err(Foo);
     assert!(r.is_err());
 }
+
+#[allow(dead_code)]
+fn issue9450() {
+    let res: Result<i32, i32> = Ok(1);
+    res.unwrap_err();
+}
index 1101aec1e1b34d5cfc29a591b1292ff6bc35d3f6..d8a9bd2f1c45576e191d3d02cf71deeb7cd8a8a3 100644 (file)
@@ -75,3 +75,9 @@ fn test_ref_copy_ok(r: &Result<CopyFoo, CopyFoo>) {
     let r: Result<Foo, Foo> = Err(Foo);
     assert!(r.is_err());
 }
+
+#[allow(dead_code)]
+fn issue9450() {
+    let res: Result<i32, i32> = Ok(1);
+    assert!(res.is_err())
+}
index 97a5f3dfca4a65f93f8d49f1f326fea941531c04..298d63c9c34fb38fc0a304b7aeb09c07db15cdcb 100644 (file)
@@ -36,5 +36,11 @@ error: called `assert!` with `Result::is_err`
 LL |     assert!(r.is_err());
    |     ^^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap_err()`
 
-error: aborting due to 6 previous errors
+error: called `assert!` with `Result::is_err`
+  --> $DIR/assertions_on_result_states.rs:82:5
+   |
+LL |     assert!(res.is_err())
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: replace with: `res.unwrap_err();`
+
+error: aborting due to 7 previous errors