Skip to content

Commit e146ead

Browse files
committed
chore: use multipart_suggestions for manual_assert, include comments in suggestion
1 parent 09589ff commit e146ead

7 files changed

+36
-188
lines changed

clippy_lints/src/manual_assert.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,21 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
6363
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par();
6464
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
6565
let base_sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
66-
// we show to the user the suggestion without the comments, but when applying the fix, include the
67-
// comments in the block
6866
span_lint_and_then(
6967
cx,
7068
MANUAL_ASSERT,
7169
expr.span,
7270
"only a `panic!` in `if`-then statement",
7371
|diag| {
74-
// If we have comments, use a tool_only suggestion to add them back.
75-
// Comments can be noisy, and this will hide them from the user's output.
76-
if !comments.is_empty() {
77-
diag.tool_only_span_suggestion(
78-
expr.span.shrink_to_lo(),
79-
"add comments back",
80-
comments,
81-
applicability,
82-
);
83-
}
72+
// If we have comments to retain, include them in the final suggestion, if
73+
// not, don't.
74+
let suggestions = if comments.is_empty() {
75+
vec![(expr.span, base_sugg.clone())]
76+
} else {
77+
vec![(expr.span.shrink_to_lo(), comments), (expr.span, base_sugg.clone())]
78+
};
8479

85-
// And setup a multipart suggestion for the user-facing part.
86-
diag.multipart_suggestion(
87-
"replace `if`-then-`panic!` with `assert!`",
88-
vec![(expr.span, base_sugg.clone())],
89-
applicability,
90-
);
80+
diag.multipart_suggestion("replace `if`-then-`panic!` with `assert!`", suggestions, applicability);
9181
},
9282
);
9383
}

tests/ui/manual_assert.edition2018.1.fixed

-83
This file was deleted.

tests/ui/manual_assert.edition2018.2.fixed renamed to tests/ui/manual_assert.edition2018.fixed

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ fn main() {
5858

5959
fn issue7730(a: u8) {
6060
// Suggestion should preserve comment
61-
assert!(!(a > 2), "panic with comment");
61+
// comment
62+
/* this is a
63+
multiline
64+
comment */
65+
/// Doc comment
66+
// comment after `panic!`
67+
assert!(!(a > 2), "panic with comment");
6268
}
6369

6470
fn issue12505() {

tests/ui/manual_assert.edition2018.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ LL | | }
7979
|
8080
help: replace `if`-then-`panic!` with `assert!`
8181
|
82-
LL | assert!(!(a > 2), "panic with comment");
82+
LL ~ // comment
83+
LL + /* this is a
84+
LL + multiline
85+
LL + comment */
86+
LL + /// Doc comment
87+
LL + // comment after `panic!`
88+
LL ~ assert!(!(a > 2), "panic with comment");
8389
|
8490

8591
error: only a `panic!` in `if`-then statement

tests/ui/manual_assert.edition2021.1.fixed

-83
This file was deleted.

tests/ui/manual_assert.edition2021.2.fixed renamed to tests/ui/manual_assert.edition2021.fixed

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ fn main() {
5858

5959
fn issue7730(a: u8) {
6060
// Suggestion should preserve comment
61-
assert!(!(a > 2), "panic with comment");
61+
// comment
62+
/* this is a
63+
multiline
64+
comment */
65+
/// Doc comment
66+
// comment after `panic!`
67+
assert!(!(a > 2), "panic with comment");
6268
}
6369

6470
fn issue12505() {

tests/ui/manual_assert.edition2021.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ LL | | }
7979
|
8080
help: replace `if`-then-`panic!` with `assert!`
8181
|
82-
LL | assert!(!(a > 2), "panic with comment");
82+
LL ~ // comment
83+
LL + /* this is a
84+
LL + multiline
85+
LL + comment */
86+
LL + /// Doc comment
87+
LL + // comment after `panic!`
88+
LL ~ assert!(!(a > 2), "panic with comment");
8389
|
8490

8591
error: only a `panic!` in `if`-then statement

0 commit comments

Comments
 (0)