Skip to content

Commit cd69d86

Browse files
committed
assertions_on_result_states fix suggestion when assert! not in a statement
1 parent 5652ccb commit cd69d86

4 files changed

+27
-6
lines changed

clippy_lints/src/assertions_on_result_states.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
3-
use clippy_utils::path_res;
43
use clippy_utils::source::snippet_with_context;
54
use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item};
65
use clippy_utils::usage::local_used_after_expr;
6+
use clippy_utils::{is_expr_final_block_expr, path_res};
77
use rustc_errors::Applicability;
88
use rustc_hir::def::Res;
99
use rustc_hir::{Expr, ExprKind};
@@ -58,6 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
5858
return;
5959
}
6060
}
61+
let semicolon = if is_expr_final_block_expr(cx.tcx, e) {";"} else {""};
6162
let mut app = Applicability::MachineApplicable;
6263
match method_segment.ident.as_str() {
6364
"is_ok" if type_suitable_to_unwrap(cx, substs.type_at(1)) => {
@@ -68,8 +69,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
6869
"called `assert!` with `Result::is_ok`",
6970
"replace with",
7071
format!(
71-
"{}.unwrap()",
72-
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
72+
"{}.unwrap(){}",
73+
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0,
74+
semicolon
7375
),
7476
app,
7577
);
@@ -82,8 +84,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
8284
"called `assert!` with `Result::is_err`",
8385
"replace with",
8486
format!(
85-
"{}.unwrap_err()",
86-
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
87+
"{}.unwrap_err(){}",
88+
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0,
89+
semicolon
8790
),
8891
app,
8992
);

tests/ui/assertions_on_result_states.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,9 @@ fn main() {
7575
let r: Result<Foo, Foo> = Err(Foo);
7676
assert!(r.is_err());
7777
}
78+
79+
#[allow(dead_code)]
80+
fn issue9450() {
81+
let res: Result<i32, i32> = Ok(1);
82+
res.unwrap_err();
83+
}

tests/ui/assertions_on_result_states.rs

+6
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,9 @@ fn main() {
7575
let r: Result<Foo, Foo> = Err(Foo);
7676
assert!(r.is_err());
7777
}
78+
79+
#[allow(dead_code)]
80+
fn issue9450() {
81+
let res: Result<i32, i32> = Ok(1);
82+
assert!(res.is_err())
83+
}

tests/ui/assertions_on_result_states.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,11 @@ error: called `assert!` with `Result::is_err`
3636
LL | assert!(r.is_err());
3737
| ^^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap_err()`
3838

39-
error: aborting due to 6 previous errors
39+
error: called `assert!` with `Result::is_err`
40+
--> $DIR/assertions_on_result_states.rs:82:5
41+
|
42+
LL | assert!(res.is_err())
43+
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with: `res.unwrap_err();`
44+
45+
error: aborting due to 7 previous errors
4046

0 commit comments

Comments
 (0)