Skip to content

Commit da8afc9

Browse files
committed
Auto merge of #9453 - kraktus:a_on_re_state, r=Jarcho
[`assertions_on_result_states`]: Fix suggestion when `assert!` is not in a statement. fix #9450 changelog: [`assertions_on_result_states`]: Fix suggestion when `assert!` is not in a statement.
2 parents 5652ccb + bdb13cd commit da8afc9

7 files changed

+39
-32
lines changed

clippy_lints/src/assertions_on_result_states.rs

+9-13
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;
5-
use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item};
4+
use clippy_utils::ty::{has_debug_impl, 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
);
@@ -94,13 +97,6 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
9497
}
9598
}
9699

97-
/// This checks whether a given type is known to implement Debug.
98-
fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
99-
cx.tcx
100-
.get_diagnostic_item(sym::Debug)
101-
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
102-
}
103-
104100
fn type_suitable_to_unwrap<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
105101
has_debug_impl(cx, ty) && !ty.is_unit() && !ty.is_never()
106102
}

clippy_lints/src/methods/err_expect.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::ERR_EXPECT;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::ty::implements_trait;
3+
use clippy_utils::ty::has_debug_impl;
44
use clippy_utils::{meets_msrv, msrvs, ty::is_type_diagnostic_item};
55
use rustc_errors::Applicability;
66
use rustc_lint::LateContext;
@@ -28,7 +28,7 @@ pub(super) fn check(
2828
// Tests if the T type in a `Result<T, E>` is not None
2929
if let Some(data_type) = get_data_type(cx, result_type);
3030
// Tests if the T type in a `Result<T, E>` implements debug
31-
if has_debug_impl(data_type, cx);
31+
if has_debug_impl(cx, data_type);
3232

3333
then {
3434
span_lint_and_sugg(
@@ -51,10 +51,3 @@ fn get_data_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
5151
_ => None,
5252
}
5353
}
54-
55-
/// Given a type, very if the Debug trait has been impl'd
56-
fn has_debug_impl<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
57-
cx.tcx
58-
.get_diagnostic_item(sym::Debug)
59-
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
60-
}

clippy_lints/src/methods/ok_expect.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
2+
use clippy_utils::ty::{has_debug_impl, is_type_diagnostic_item};
33
use if_chain::if_chain;
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
@@ -15,7 +15,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
1515
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
1616
let result_type = cx.typeck_results().expr_ty(recv);
1717
if let Some(error_type) = get_error_type(cx, result_type);
18-
if has_debug_impl(error_type, cx);
18+
if has_debug_impl(cx, error_type);
1919

2020
then {
2121
span_lint_and_help(
@@ -37,10 +37,3 @@ fn get_error_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
3737
_ => None,
3838
}
3939
}
40-
41-
/// This checks whether a given type is known to implement Debug.
42-
fn has_debug_impl<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
43-
cx.tcx
44-
.get_diagnostic_item(sym::Debug)
45-
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
46-
}

clippy_utils/src/ty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ pub fn is_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
3131
ty.is_copy_modulo_regions(cx.tcx.at(DUMMY_SP), cx.param_env)
3232
}
3333

34+
/// This checks whether a given type is known to implement Debug.
35+
pub fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
36+
cx.tcx
37+
.get_diagnostic_item(sym::Debug)
38+
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
39+
}
40+
3441
/// Checks whether a type can be partially moved.
3542
pub fn can_partially_move_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
3643
if has_drop(cx, ty) || is_copy(cx, ty) {

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)