Skip to content

Commit 5b93b75

Browse files
unexgeweihanglo
andcommitted
Update lint description and add help section
Co-authored-by: Weihang Lo <me@weihanglo.tw>
1 parent 0adbee1 commit 5b93b75

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

clippy_lints/src/missing_assert_message.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_help;
22
use rustc_ast::ast;
33
use rustc_ast::{
44
token::{Token, TokenKind},
@@ -13,19 +13,23 @@ declare_clippy_lint! {
1313
/// Checks assertions without a custom panic message.
1414
///
1515
/// ### Why is this bad?
16-
/// If the assertion fails, the custom message may make it easier to understand what went wrong.
16+
/// Without a good custom message, it'd be hard to understand what went wrong when the assertion fails.
17+
/// A good custom message should be more about why the failure of the assertion is problematic
18+
/// and not what is failed because the assertion already conveys that.
1719
///
1820
/// ### Example
1921
/// ```rust
20-
/// let threshold = 50;
21-
/// let num = 42;
22-
/// assert!(num < threshold);
22+
/// # struct Service { ready: bool }
23+
/// fn call(service: Service) {
24+
/// assert!(service.ready);
25+
/// }
2326
/// ```
2427
/// Use instead:
2528
/// ```rust
26-
/// let threshold = 50;
27-
/// let num = 42;
28-
/// assert!(num < threshold, "{num} is lower than threshold ({threshold})");
29+
/// # struct Service { ready: bool }
30+
/// fn call(service: Service) {
31+
/// assert!(service.ready, "`service.poll_ready()` must be called first to ensure that service is ready to receive requests");
32+
/// }
2933
/// ```
3034
#[clippy::version = "1.69.0"]
3135
pub MISSING_ASSERT_MESSAGE,
@@ -56,11 +60,13 @@ impl EarlyLintPass for MissingAssertMessage {
5660
let num_separators = num_commas_on_arguments(mac_call);
5761

5862
if num_separators < num_separators_needed {
59-
span_lint(
63+
span_lint_and_help(
6064
cx,
6165
MISSING_ASSERT_MESSAGE,
6266
mac_call.span(),
6367
"assert without any message",
68+
None,
69+
"consider describing why the failing assert is problematic",
6470
);
6571
}
6672
}

tests/ui/missing_assert_message.stderr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,128 @@ error: assert without any message
44
LL | assert!(foo());
55
| ^^^^^^^^^^^^^^
66
|
7+
= help: consider describing why the failing assert is problematic
78
= note: `-D clippy::missing-assert-message` implied by `-D warnings`
89

910
error: assert without any message
1011
--> $DIR/missing_assert_message.rs:15:5
1112
|
1213
LL | assert_eq!(foo(), foo());
1314
| ^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: consider describing why the failing assert is problematic
1417

1518
error: assert without any message
1619
--> $DIR/missing_assert_message.rs:16:5
1720
|
1821
LL | assert_ne!(foo(), foo());
1922
| ^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: consider describing why the failing assert is problematic
2025

2126
error: assert without any message
2227
--> $DIR/missing_assert_message.rs:17:5
2328
|
2429
LL | debug_assert!(foo());
2530
| ^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= help: consider describing why the failing assert is problematic
2633

2734
error: assert without any message
2835
--> $DIR/missing_assert_message.rs:18:5
2936
|
3037
LL | debug_assert_eq!(foo(), foo());
3138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= help: consider describing why the failing assert is problematic
3241

3342
error: assert without any message
3443
--> $DIR/missing_assert_message.rs:19:5
3544
|
3645
LL | debug_assert_ne!(foo(), foo());
3746
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= help: consider describing why the failing assert is problematic
3849

3950
error: assert without any message
4051
--> $DIR/missing_assert_message.rs:24:5
4152
|
4253
LL | assert!(bar!(true));
4354
| ^^^^^^^^^^^^^^^^^^^
55+
|
56+
= help: consider describing why the failing assert is problematic
4457

4558
error: assert without any message
4659
--> $DIR/missing_assert_message.rs:25:5
4760
|
4861
LL | assert!(bar!(true, false));
4962
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= help: consider describing why the failing assert is problematic
5065

5166
error: assert without any message
5267
--> $DIR/missing_assert_message.rs:26:5
5368
|
5469
LL | assert_eq!(bar!(true), foo());
5570
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
|
72+
= help: consider describing why the failing assert is problematic
5673

5774
error: assert without any message
5875
--> $DIR/missing_assert_message.rs:27:5
5976
|
6077
LL | assert_ne!(bar!(true, true), bar!(true));
6178
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
|
80+
= help: consider describing why the failing assert is problematic
6281

6382
error: assert without any message
6483
--> $DIR/missing_assert_message.rs:32:5
6584
|
6685
LL | assert!(foo(),);
6786
| ^^^^^^^^^^^^^^^
87+
|
88+
= help: consider describing why the failing assert is problematic
6889

6990
error: assert without any message
7091
--> $DIR/missing_assert_message.rs:33:5
7192
|
7293
LL | assert_eq!(foo(), foo(),);
7394
| ^^^^^^^^^^^^^^^^^^^^^^^^^
95+
|
96+
= help: consider describing why the failing assert is problematic
7497

7598
error: assert without any message
7699
--> $DIR/missing_assert_message.rs:34:5
77100
|
78101
LL | assert_ne!(foo(), foo(),);
79102
| ^^^^^^^^^^^^^^^^^^^^^^^^^
103+
|
104+
= help: consider describing why the failing assert is problematic
80105

81106
error: assert without any message
82107
--> $DIR/missing_assert_message.rs:35:5
83108
|
84109
LL | debug_assert!(foo(),);
85110
| ^^^^^^^^^^^^^^^^^^^^^
111+
|
112+
= help: consider describing why the failing assert is problematic
86113

87114
error: assert without any message
88115
--> $DIR/missing_assert_message.rs:36:5
89116
|
90117
LL | debug_assert_eq!(foo(), foo(),);
91118
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119+
|
120+
= help: consider describing why the failing assert is problematic
92121

93122
error: assert without any message
94123
--> $DIR/missing_assert_message.rs:37:5
95124
|
96125
LL | debug_assert_ne!(foo(), foo(),);
97126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127+
|
128+
= help: consider describing why the failing assert is problematic
98129

99130
error: aborting due to 16 previous errors
100131

0 commit comments

Comments
 (0)