Skip to content

Commit a5a6c95

Browse files
committed
Auto merge of #9273 - tabokie:assert_ok_fp, r=flip1995
Move [`assertions_on_result_states`] to restriction Close #9263 This lint causes regression on readability of code and log output. And printing runtime values is not particularly useful for majority of tests which should be reproducible. changelog: Move [`assertions_on_result_states`] to restriction and don't lint it for unit type Signed-off-by: tabokie <[email protected]>
2 parents 7c427f0 + 48ad9d8 commit a5a6c95

7 files changed

+34
-11
lines changed

clippy_lints/src/assertions_on_result_states.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ declare_clippy_lint! {
1919
/// ### Why is this bad?
2020
/// An assertion failure cannot output an useful message of the error.
2121
///
22+
/// ### Known problems
23+
/// The suggested replacement decreases the readability of code and log output.
24+
///
2225
/// ### Example
2326
/// ```rust,ignore
2427
/// # let r = Ok::<_, ()>(());
@@ -28,7 +31,7 @@ declare_clippy_lint! {
2831
/// ```
2932
#[clippy::version = "1.64.0"]
3033
pub ASSERTIONS_ON_RESULT_STATES,
31-
style,
34+
restriction,
3235
"`assert!(r.is_ok())`/`assert!(r.is_err())` gives worse error message than directly calling `r.unwrap()`/`r.unwrap_err()`"
3336
}
3437

@@ -50,13 +53,14 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
5053
if result_type_with_refs != result_type {
5154
return;
5255
} else if let Res::Local(binding_id) = path_res(cx, recv)
53-
&& local_used_after_expr(cx, binding_id, recv) {
56+
&& local_used_after_expr(cx, binding_id, recv)
57+
{
5458
return;
5559
}
5660
}
5761
let mut app = Applicability::MachineApplicable;
5862
match method_segment.ident.as_str() {
59-
"is_ok" if has_debug_impl(cx, substs.type_at(1)) => {
63+
"is_ok" if type_suitable_to_unwrap(cx, substs.type_at(1)) => {
6064
span_lint_and_sugg(
6165
cx,
6266
ASSERTIONS_ON_RESULT_STATES,
@@ -70,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
7074
app,
7175
);
7276
}
73-
"is_err" if has_debug_impl(cx, substs.type_at(0)) => {
77+
"is_err" if type_suitable_to_unwrap(cx, substs.type_at(0)) => {
7478
span_lint_and_sugg(
7579
cx,
7680
ASSERTIONS_ON_RESULT_STATES,
@@ -96,3 +100,7 @@ fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
96100
.get_diagnostic_item(sym::Debug)
97101
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
98102
}
103+
104+
fn type_suitable_to_unwrap<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
105+
has_debug_impl(cx, ty) && !ty.is_unit() && !ty.is_never()
106+
}

clippy_lints/src/lib.register_all.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
66
LintId::of(almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE),
77
LintId::of(approx_const::APPROX_CONSTANT),
88
LintId::of(assertions_on_constants::ASSERTIONS_ON_CONSTANTS),
9-
LintId::of(assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES),
109
LintId::of(async_yields_async::ASYNC_YIELDS_ASYNC),
1110
LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS),
1211
LintId::of(attrs::DEPRECATED_CFG_ATTR),

clippy_lints/src/lib.register_restriction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
77
LintId::of(as_underscore::AS_UNDERSCORE),
88
LintId::of(asm_syntax::INLINE_ASM_X86_ATT_SYNTAX),
99
LintId::of(asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX),
10+
LintId::of(assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES),
1011
LintId::of(attrs::ALLOW_ATTRIBUTES_WITHOUT_REASON),
1112
LintId::of(casts::FN_TO_NUMERIC_CAST_ANY),
1213
LintId::of(create_dir::CREATE_DIR),

clippy_lints/src/lib.register_style.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
store.register_group(true, "clippy::style", Some("clippy_style"), vec![
66
LintId::of(assertions_on_constants::ASSERTIONS_ON_CONSTANTS),
7-
LintId::of(assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES),
87
LintId::of(blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS),
98
LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON),
109
LintId::of(casts::FN_TO_NUMERIC_CAST),

tests/ui/assertions_on_result_states.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ fn main() {
2727
let r: Result<Foo, Foo> = Ok(Foo);
2828
assert!(r.is_ok());
2929

30+
// test ok with some messages
31+
let r: Result<Foo, DebugFoo> = Ok(Foo);
32+
assert!(r.is_ok(), "oops");
33+
34+
// test ok with unit error
35+
let r: Result<Foo, ()> = Ok(Foo);
36+
assert!(r.is_ok());
37+
3038
// test temporary ok
3139
fn get_ok() -> Result<Foo, DebugFoo> {
3240
Ok(Foo)

tests/ui/assertions_on_result_states.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ fn main() {
2727
let r: Result<Foo, Foo> = Ok(Foo);
2828
assert!(r.is_ok());
2929

30+
// test ok with some messages
31+
let r: Result<Foo, DebugFoo> = Ok(Foo);
32+
assert!(r.is_ok(), "oops");
33+
34+
// test ok with unit error
35+
let r: Result<Foo, ()> = Ok(Foo);
36+
assert!(r.is_ok());
37+
3038
// test temporary ok
3139
fn get_ok() -> Result<Foo, DebugFoo> {
3240
Ok(Foo)

tests/ui/assertions_on_result_states.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ LL | assert!(r.is_ok());
77
= note: `-D clippy::assertions-on-result-states` implied by `-D warnings`
88

99
error: called `assert!` with `Result::is_ok`
10-
--> $DIR/assertions_on_result_states.rs:34:5
10+
--> $DIR/assertions_on_result_states.rs:42:5
1111
|
1212
LL | assert!(get_ok().is_ok());
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `get_ok().unwrap()`
1414

1515
error: called `assert!` with `Result::is_ok`
16-
--> $DIR/assertions_on_result_states.rs:37:5
16+
--> $DIR/assertions_on_result_states.rs:45:5
1717
|
1818
LL | assert!(get_ok_macro!().is_ok());
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `get_ok_macro!().unwrap()`
2020

2121
error: called `assert!` with `Result::is_ok`
22-
--> $DIR/assertions_on_result_states.rs:50:5
22+
--> $DIR/assertions_on_result_states.rs:58:5
2323
|
2424
LL | assert!(r.is_ok());
2525
| ^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap()`
2626

2727
error: called `assert!` with `Result::is_ok`
28-
--> $DIR/assertions_on_result_states.rs:56:9
28+
--> $DIR/assertions_on_result_states.rs:64:9
2929
|
3030
LL | assert!(r.is_ok());
3131
| ^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap()`
3232

3333
error: called `assert!` with `Result::is_err`
34-
--> $DIR/assertions_on_result_states.rs:64:5
34+
--> $DIR/assertions_on_result_states.rs:72:5
3535
|
3636
LL | assert!(r.is_err());
3737
| ^^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap_err()`

0 commit comments

Comments
 (0)