Update lint description and add help section

Co-authored-by: Weihang Lo <me@weihanglo.tw>
This commit is contained in:
unexge 2023-02-18 19:09:31 +00:00
parent 099d610640
commit 4eb6ccc973
2 changed files with 46 additions and 9 deletions

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint_and_help;
use rustc_ast::ast; use rustc_ast::ast;
use rustc_ast::{ use rustc_ast::{
token::{Token, TokenKind}, token::{Token, TokenKind},
@ -13,19 +13,23 @@ declare_clippy_lint! {
/// Checks assertions without a custom panic message. /// Checks assertions without a custom panic message.
/// ///
/// ### Why is this bad? /// ### Why is this bad?
/// If the assertion fails, the custom message may make it easier to understand what went wrong. /// Without a good custom message, it'd be hard to understand what went wrong when the assertion fails.
/// A good custom message should be more about why the failure of the assertion is problematic
/// and not what is failed because the assertion already conveys that.
/// ///
/// ### Example /// ### Example
/// ```rust /// ```rust
/// let threshold = 50; /// # struct Service { ready: bool }
/// let num = 42; /// fn call(service: Service) {
/// assert!(num < threshold); /// assert!(service.ready);
/// }
/// ``` /// ```
/// Use instead: /// Use instead:
/// ```rust /// ```rust
/// let threshold = 50; /// # struct Service { ready: bool }
/// let num = 42; /// fn call(service: Service) {
/// assert!(num < threshold, "{num} is lower than threshold ({threshold})"); /// assert!(service.ready, "`service.poll_ready()` must be called first to ensure that service is ready to receive requests");
/// }
/// ``` /// ```
#[clippy::version = "1.69.0"] #[clippy::version = "1.69.0"]
pub MISSING_ASSERT_MESSAGE, pub MISSING_ASSERT_MESSAGE,
@ -56,11 +60,13 @@ impl EarlyLintPass for MissingAssertMessage {
let num_separators = num_commas_on_arguments(mac_call); let num_separators = num_commas_on_arguments(mac_call);
if num_separators < num_separators_needed { if num_separators < num_separators_needed {
span_lint( span_lint_and_help(
cx, cx,
MISSING_ASSERT_MESSAGE, MISSING_ASSERT_MESSAGE,
mac_call.span(), mac_call.span(),
"assert without any message", "assert without any message",
None,
"consider describing why the failing assert is problematic",
); );
} }
} }

View File

@ -4,6 +4,7 @@ error: assert without any message
LL | assert!(foo()); LL | assert!(foo());
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
| |
= help: consider describing why the failing assert is problematic
= note: `-D clippy::missing-assert-message` implied by `-D warnings` = note: `-D clippy::missing-assert-message` implied by `-D warnings`
error: assert without any message error: assert without any message
@ -11,90 +12,120 @@ error: assert without any message
| |
LL | assert_eq!(foo(), foo()); LL | assert_eq!(foo(), foo());
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:16:5 --> $DIR/missing_assert_message.rs:16:5
| |
LL | assert_ne!(foo(), foo()); LL | assert_ne!(foo(), foo());
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:17:5 --> $DIR/missing_assert_message.rs:17:5
| |
LL | debug_assert!(foo()); LL | debug_assert!(foo());
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:18:5 --> $DIR/missing_assert_message.rs:18:5
| |
LL | debug_assert_eq!(foo(), foo()); LL | debug_assert_eq!(foo(), foo());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:19:5 --> $DIR/missing_assert_message.rs:19:5
| |
LL | debug_assert_ne!(foo(), foo()); LL | debug_assert_ne!(foo(), foo());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:24:5 --> $DIR/missing_assert_message.rs:24:5
| |
LL | assert!(bar!(true)); LL | assert!(bar!(true));
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:25:5 --> $DIR/missing_assert_message.rs:25:5
| |
LL | assert!(bar!(true, false)); LL | assert!(bar!(true, false));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:26:5 --> $DIR/missing_assert_message.rs:26:5
| |
LL | assert_eq!(bar!(true), foo()); LL | assert_eq!(bar!(true), foo());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:27:5 --> $DIR/missing_assert_message.rs:27:5
| |
LL | assert_ne!(bar!(true, true), bar!(true)); LL | assert_ne!(bar!(true, true), bar!(true));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:32:5 --> $DIR/missing_assert_message.rs:32:5
| |
LL | assert!(foo(),); LL | assert!(foo(),);
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:33:5 --> $DIR/missing_assert_message.rs:33:5
| |
LL | assert_eq!(foo(), foo(),); LL | assert_eq!(foo(), foo(),);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:34:5 --> $DIR/missing_assert_message.rs:34:5
| |
LL | assert_ne!(foo(), foo(),); LL | assert_ne!(foo(), foo(),);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:35:5 --> $DIR/missing_assert_message.rs:35:5
| |
LL | debug_assert!(foo(),); LL | debug_assert!(foo(),);
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:36:5 --> $DIR/missing_assert_message.rs:36:5
| |
LL | debug_assert_eq!(foo(), foo(),); LL | debug_assert_eq!(foo(), foo(),);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: assert without any message error: assert without any message
--> $DIR/missing_assert_message.rs:37:5 --> $DIR/missing_assert_message.rs:37:5
| |
LL | debug_assert_ne!(foo(), foo(),); LL | debug_assert_ne!(foo(), foo(),);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider describing why the failing assert is problematic
error: aborting due to 16 previous errors error: aborting due to 16 previous errors