Skip to content

Commit 4d63a6a

Browse files
committed
disable [assertions_on_result_states] and do not lint for unit type
Signed-off-by: tabokie <[email protected]>
1 parent a0ed687 commit 4d63a6a

7 files changed

+40
-10
lines changed

clippy_lints/src/assertions_on_result_states.rs

Lines changed: 18 additions & 3 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

@@ -56,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
5659
}
5760
let mut app = Applicability::MachineApplicable;
5861
match method_segment.ident.as_str() {
59-
"is_ok" if has_debug_impl(cx, substs.type_at(1)) => {
62+
"is_ok" if type_suitable_to_unwrap(cx, substs.type_at(1)) => {
6063
span_lint_and_sugg(
6164
cx,
6265
ASSERTIONS_ON_RESULT_STATES,
@@ -70,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
7073
app,
7174
);
7275
}
73-
"is_err" if has_debug_impl(cx, substs.type_at(0)) => {
76+
"is_err" if type_suitable_to_unwrap(cx, substs.type_at(0)) => {
7477
span_lint_and_sugg(
7578
cx,
7679
ASSERTIONS_ON_RESULT_STATES,
@@ -96,3 +99,15 @@ fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
9699
.get_diagnostic_item(sym::Debug)
97100
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
98101
}
102+
103+
fn is_unit_type(ty: Ty<'_>) -> bool {
104+
match ty.kind() {
105+
ty::Tuple(slice) => slice.is_empty(),
106+
ty::Never => true,
107+
_ => false,
108+
}
109+
}
110+
111+
fn type_suitable_to_unwrap<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
112+
has_debug_impl(cx, ty) && !is_unit_type(ty)
113+
}

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)