Skip to content

fix(manual_assert): use multipart suggestion instead of simple span_suggestion #15226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions clippy_lints/src/manual_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,22 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_paren();
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
// we show to the user the suggestion without the comments, but when applying the fix, include the
// comments in the block

span_lint_and_then(
cx,
MANUAL_ASSERT,
expr.span,
"only a `panic!` in `if`-then statement",
|diag| {
// comments can be noisy, do not show them to the user
let mut suggestions = vec![];

if !comments.is_empty() {
diag.tool_only_span_suggestion(
expr.span.shrink_to_lo(),
"add comments back",
comments,
applicability,
);
suggestions.push((expr.span.shrink_to_lo(), comments));
}
diag.span_suggestion(expr.span, "try instead", sugg, applicability);

suggestions.push((expr.span, sugg));

diag.multipart_suggestion("try instead", suggestions, applicability);
},
);
}
Expand Down
81 changes: 26 additions & 55 deletions tests/ui/manual_assert.edition2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ LL | | }
= help: to override `-D warnings` add `#[allow(clippy::manual_assert)]`
help: try instead
|
LL - if !a.is_empty() {
LL -
LL - panic!("qaqaq{:?}", a);
LL - }
LL + assert!(a.is_empty(), "qaqaq{:?}", a);
LL ~
LL ~ assert!(a.is_empty(), "qaqaq{:?}", a);
|

error: only a `panic!` in `if`-then statement
Expand All @@ -29,11 +26,8 @@ LL | | }
|
help: try instead
|
LL - if !a.is_empty() {
LL -
LL - panic!("qwqwq");
LL - }
LL + assert!(a.is_empty(), "qwqwq");
LL ~
LL ~ assert!(a.is_empty(), "qwqwq");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -47,11 +41,8 @@ LL | | }
|
help: try instead
|
LL - if b.is_empty() {
LL -
LL - panic!("panic1");
LL - }
LL + assert!(!b.is_empty(), "panic1");
LL ~
LL ~ assert!(!b.is_empty(), "panic1");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -65,11 +56,8 @@ LL | | }
|
help: try instead
|
LL - if b.is_empty() && a.is_empty() {
LL -
LL - panic!("panic2");
LL - }
LL + assert!(!(b.is_empty() && a.is_empty()), "panic2");
LL ~
LL ~ assert!(!(b.is_empty() && a.is_empty()), "panic2");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -83,11 +71,8 @@ LL | | }
|
help: try instead
|
LL - if a.is_empty() && !b.is_empty() {
LL -
LL - panic!("panic3");
LL - }
LL + assert!(!(a.is_empty() && !b.is_empty()), "panic3");
LL ~
LL ~ assert!(!(a.is_empty() && !b.is_empty()), "panic3");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -101,11 +86,8 @@ LL | | }
|
help: try instead
|
LL - if b.is_empty() || a.is_empty() {
LL -
LL - panic!("panic4");
LL - }
LL + assert!(!(b.is_empty() || a.is_empty()), "panic4");
LL ~
LL ~ assert!(!(b.is_empty() || a.is_empty()), "panic4");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -119,11 +101,8 @@ LL | | }
|
help: try instead
|
LL - if a.is_empty() || !b.is_empty() {
LL -
LL - panic!("panic5");
LL - }
LL + assert!(!(a.is_empty() || !b.is_empty()), "panic5");
LL ~
LL ~ assert!(!(a.is_empty() || !b.is_empty()), "panic5");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -137,11 +116,8 @@ LL | | }
|
help: try instead
|
LL - if a.is_empty() {
LL -
LL - panic!("with expansion {}", one!())
LL - }
LL + assert!(!a.is_empty(), "with expansion {}", one!());
LL ~
LL ~ assert!(!a.is_empty(), "with expansion {}", one!());
|

error: only a `panic!` in `if`-then statement
Expand All @@ -158,16 +134,14 @@ LL | | }
|
help: try instead
|
LL - if a > 2 {
LL -
LL - // comment
LL - /* this is a
LL - multiline
LL - comment */
LL - /// Doc comment
LL - panic!("panic with comment") // comment after `panic!`
LL - }
LL + assert!(!(a > 2), "panic with comment");
LL ~
LL + // comment
LL + /* this is a
LL + multiline
LL + comment */
LL + /// Doc comment
LL + // comment after `panic!`
LL ~ assert!(!(a > 2), "panic with comment");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -182,11 +156,8 @@ LL | | };
|
help: try instead
|
LL - const BAR: () = if N == 0 {
LL -
LL - panic!()
LL - };
LL + const BAR: () = assert!(!(N == 0), );
LL ~ const BAR: () =
LL ~ assert!(!(N == 0), );
|

error: aborting due to 10 previous errors
Expand Down
81 changes: 26 additions & 55 deletions tests/ui/manual_assert.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ LL | | }
= help: to override `-D warnings` add `#[allow(clippy::manual_assert)]`
help: try instead
|
LL - if !a.is_empty() {
LL -
LL - panic!("qaqaq{:?}", a);
LL - }
LL + assert!(a.is_empty(), "qaqaq{:?}", a);
LL ~
LL ~ assert!(a.is_empty(), "qaqaq{:?}", a);
|

error: only a `panic!` in `if`-then statement
Expand All @@ -29,11 +26,8 @@ LL | | }
|
help: try instead
|
LL - if !a.is_empty() {
LL -
LL - panic!("qwqwq");
LL - }
LL + assert!(a.is_empty(), "qwqwq");
LL ~
LL ~ assert!(a.is_empty(), "qwqwq");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -47,11 +41,8 @@ LL | | }
|
help: try instead
|
LL - if b.is_empty() {
LL -
LL - panic!("panic1");
LL - }
LL + assert!(!b.is_empty(), "panic1");
LL ~
LL ~ assert!(!b.is_empty(), "panic1");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -65,11 +56,8 @@ LL | | }
|
help: try instead
|
LL - if b.is_empty() && a.is_empty() {
LL -
LL - panic!("panic2");
LL - }
LL + assert!(!(b.is_empty() && a.is_empty()), "panic2");
LL ~
LL ~ assert!(!(b.is_empty() && a.is_empty()), "panic2");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -83,11 +71,8 @@ LL | | }
|
help: try instead
|
LL - if a.is_empty() && !b.is_empty() {
LL -
LL - panic!("panic3");
LL - }
LL + assert!(!(a.is_empty() && !b.is_empty()), "panic3");
LL ~
LL ~ assert!(!(a.is_empty() && !b.is_empty()), "panic3");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -101,11 +86,8 @@ LL | | }
|
help: try instead
|
LL - if b.is_empty() || a.is_empty() {
LL -
LL - panic!("panic4");
LL - }
LL + assert!(!(b.is_empty() || a.is_empty()), "panic4");
LL ~
LL ~ assert!(!(b.is_empty() || a.is_empty()), "panic4");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -119,11 +101,8 @@ LL | | }
|
help: try instead
|
LL - if a.is_empty() || !b.is_empty() {
LL -
LL - panic!("panic5");
LL - }
LL + assert!(!(a.is_empty() || !b.is_empty()), "panic5");
LL ~
LL ~ assert!(!(a.is_empty() || !b.is_empty()), "panic5");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -137,11 +116,8 @@ LL | | }
|
help: try instead
|
LL - if a.is_empty() {
LL -
LL - panic!("with expansion {}", one!())
LL - }
LL + assert!(!a.is_empty(), "with expansion {}", one!());
LL ~
LL ~ assert!(!a.is_empty(), "with expansion {}", one!());
|

error: only a `panic!` in `if`-then statement
Expand All @@ -158,16 +134,14 @@ LL | | }
|
help: try instead
|
LL - if a > 2 {
LL -
LL - // comment
LL - /* this is a
LL - multiline
LL - comment */
LL - /// Doc comment
LL - panic!("panic with comment") // comment after `panic!`
LL - }
LL + assert!(!(a > 2), "panic with comment");
LL ~
LL + // comment
LL + /* this is a
LL + multiline
LL + comment */
LL + /// Doc comment
LL + // comment after `panic!`
LL ~ assert!(!(a > 2), "panic with comment");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -182,11 +156,8 @@ LL | | };
|
help: try instead
|
LL - const BAR: () = if N == 0 {
LL -
LL - panic!()
LL - };
LL + const BAR: () = assert!(!(N == 0), );
LL ~ const BAR: () =
LL ~ assert!(!(N == 0), );
|

error: aborting due to 10 previous errors
Expand Down