diff --git a/clippy_lints/src/manual_assert.rs b/clippy_lints/src/manual_assert.rs index 8378e15c581c..003699237473 100644 --- a/clippy_lints/src/manual_assert.rs +++ b/clippy_lints/src/manual_assert.rs @@ -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); }, ); } diff --git a/tests/ui/manual_assert.edition2018.stderr b/tests/ui/manual_assert.edition2018.stderr index 8cedf2c68636..267ea096b025 100644 --- a/tests/ui/manual_assert.edition2018.stderr +++ b/tests/ui/manual_assert.edition2018.stderr @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/ui/manual_assert.edition2021.stderr b/tests/ui/manual_assert.edition2021.stderr index 8cedf2c68636..267ea096b025 100644 --- a/tests/ui/manual_assert.edition2021.stderr +++ b/tests/ui/manual_assert.edition2021.stderr @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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