Skip to content

Commit bc9e5a6

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

7 files changed

+148
-500
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.shrink_to_lo(), comments), (expr.span, base_sugg.clone())]
76+
} else {
77+
vec![(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

-70
This file was deleted.
+70-88
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,77 @@
1-
error: only a `panic!` in `if`-then statement
2-
--> tests/ui/manual_assert.rs:30:5
3-
|
4-
LL | / if !a.is_empty() {
5-
LL | | panic!("qaqaq{:?}", a);
6-
LL | | }
7-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(a.is_empty(), "qaqaq{:?}", a);`
8-
|
9-
= note: `-D clippy::manual-assert` implied by `-D warnings`
10-
= help: to override `-D warnings` add `#[allow(clippy::manual_assert)]`
1+
thread 'rustc' panicked at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:50:9:
2+
assertion `left == right` failed: span must not be empty and have no suggestion
3+
left: Some(SubstitutionPart { span: tests/ui/manual_assert.rs:30:5: 30:5 (#0), snippet: "" })
4+
right: None
5+
stack backtrace:
6+
0: 0x1127b15b8 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hd5de7b0df0729e03
7+
1: 0x10fa14e64 - core::fmt::write::h2d6cb8c661d0a83d
8+
2: 0x1127a5960 - std::io::Write::write_fmt::h0f134b74b751e0f9
9+
3: 0x1127b1478 - std::sys::backtrace::BacktraceLock::print::h93b2b495ebfc2cb2
10+
4: 0x1127b3948 - std::panicking::default_hook::{{closure}}::h5e69c7a046eca612
11+
5: 0x1127b3790 - std::panicking::default_hook::h9f5f5724d303148a
12+
6: 0x11061e57c - <alloc[2ffe1c66fee3fdaa]::boxed::Box<rustc_driver_impl[17a93adec6f3d70a]::install_ice_hook::{closure#0}> as core[1a7bc357c76bf42]::ops::function::Fn<(&dyn for<'a, 'b> core[1a7bc357c76bf42]::ops::function::Fn<(&'a std[a184c2360da8cf6c]::panic::PanicHookInfo<'b>,), Output = ()> + core[1a7bc357c76bf42]::marker::Send + core[1a7bc357c76bf42]::marker::Sync, &std[a184c2360da8cf6c]::panic::PanicHookInfo)>>::call
13+
7: 0x1127b4214 - std::panicking::rust_panic_with_hook::hab3d98f4160d14fd
14+
8: 0x1127b3e50 - std::panicking::begin_panic_handler::{{closure}}::h1577ffea1a58f5f7
15+
9: 0x1127b1a54 - std::sys::backtrace::__rust_end_short_backtrace::hae37950ca15bb119
16+
10: 0x1127b3b14 - _rust_begin_unwind
17+
11: 0x114e9725c - core::panicking::panic_fmt::h987dd6a54d7aba16
18+
12: 0x114e975f0 - core::panicking::assert_failed_inner::h12c770c2425fee7d
19+
13: 0x103102510 - core::panicking::assert_failed::hc915165530e4fffb
20+
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panicking.rs:373:5
21+
14: 0x102190eac - clippy_utils::diagnostics::validate_diag::h239c2d3579525ceb
22+
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:50:9
23+
15: 0x1021a7d1c - clippy_utils::diagnostics::span_lint_and_then::{{closure}}::h9b8b186ec716fe56
24+
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:261:9
25+
16: 0x102880df4 - core::ops::function::FnOnce::call_once{{vtable.shim}}::hf14a66775935d916
26+
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
27+
17: 0x1113b1670 - rustc_middle[7019c7ba16fcd5cd]::lint::lint_level::lint_level_impl
28+
18: 0x102420a80 - rustc_middle::lint::lint_level::hcb523238415a1e29
29+
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_middle/src/lint.rs:423:5
30+
19: 0x102900f00 - rustc_middle::ty::context::TyCtxt::node_span_lint::h7532365959f7d1f5
31+
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_middle/src/ty/context.rs:2950:9
32+
20: 0x10273a51c - <rustc_lint::context::LateContext as rustc_lint::context::LintContext>::opt_span_lint::hfbf1866d04803dc5
33+
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_lint/src/context.rs:669:24
34+
21: 0x10271cc1c - rustc_lint::context::LintContext::span_lint::h28363b347076190e
35+
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_lint/src/context.rs:585:9
36+
22: 0x1021969c0 - clippy_utils::diagnostics::span_lint_and_then::h479f5abc48e54fa2
37+
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:255:5
38+
23: 0x102835ee4 - <clippy_lints::manual_assert::ManualAssert as rustc_lint::passes::LateLintPass>::check_expr::hbc06e8db702404ba
39+
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_lints/src/manual_assert.rs:66:13
40+
24: 0x11113bf8c - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_expr::{closure#0}
41+
25: 0x111082e60 - rustc_hir[e37c4407e29c46ca]::intravisit::walk_expr::<rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass>>
42+
26: 0x11113bfa0 - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_expr::{closure#0}
43+
27: 0x11113b44c - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_nested_body
44+
28: 0x11113c398 - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_fn
45+
29: 0x111084964 - rustc_hir[e37c4407e29c46ca]::intravisit::walk_item::<rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass>>
46+
30: 0x111134a3c - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_nested_item
47+
31: 0x11113e570 - rustc_lint[385369ae408eed1c]::late::check_crate::{closure#0}
48+
32: 0x11113dc0c - rustc_lint[385369ae408eed1c]::late::check_crate
49+
33: 0x110f41640 - <rustc_session[ea051583fbb3404]::session::Session>::time::<(), rustc_interface[e97b870a1f7a7bd5]::passes::analysis::{closure#0}>
50+
34: 0x110ff20bc - rustc_interface[e97b870a1f7a7bd5]::passes::analysis
51+
35: 0x111ce813c - rustc_query_impl[3faacfedda06551]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3faacfedda06551]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[7019c7ba16fcd5cd]::query::erase::Erased<[u8; 1usize]>>
52+
36: 0x111e2433c - <rustc_query_impl[3faacfedda06551]::query_impl::analysis::dynamic_query::{closure#2} as core[1a7bc357c76bf42]::ops::function::FnOnce<(rustc_middle[7019c7ba16fcd5cd]::ty::context::TyCtxt, ())>>::call_once
53+
37: 0x111bf6828 - rustc_query_system[bd441621c7ea624b]::query::plumbing::try_execute_query::<rustc_query_impl[3faacfedda06551]::DynamicConfig<rustc_query_system[bd441621c7ea624b]::query::caches::SingleCache<rustc_middle[7019c7ba16fcd5cd]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3faacfedda06551]::plumbing::QueryCtxt, false>
54+
38: 0x111d6e564 - rustc_query_impl[3faacfedda06551]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
55+
39: 0x110620300 - <rustc_middle[7019c7ba16fcd5cd]::ty::context::GlobalCtxt>::enter::<rustc_driver_impl[17a93adec6f3d70a]::run_compiler::{closure#0}::{closure#1}::{closure#6}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>
56+
40: 0x1105d716c - <rustc_interface[e97b870a1f7a7bd5]::interface::Compiler>::enter::<rustc_driver_impl[17a93adec6f3d70a]::run_compiler::{closure#0}::{closure#1}, core[1a7bc357c76bf42]::result::Result<core[1a7bc357c76bf42]::option::Option<rustc_interface[e97b870a1f7a7bd5]::queries::Linker>, rustc_span[961a2807afbea68d]::ErrorGuaranteed>>
57+
41: 0x110610b60 - rustc_span[961a2807afbea68d]::create_session_globals_then::<core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>, rustc_interface[e97b870a1f7a7bd5]::util::run_in_thread_with_globals<rustc_interface[e97b870a1f7a7bd5]::util::run_in_thread_pool_with_globals<rustc_interface[e97b870a1f7a7bd5]::interface::run_compiler<core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>, rustc_driver_impl[17a93adec6f3d70a]::run_compiler::{closure#0}>::{closure#1}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#0}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
58+
42: 0x11064249c - std[a184c2360da8cf6c]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[e97b870a1f7a7bd5]::util::run_in_thread_with_globals<rustc_interface[e97b870a1f7a7bd5]::util::run_in_thread_pool_with_globals<rustc_interface[e97b870a1f7a7bd5]::interface::run_compiler<core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>, rustc_driver_impl[17a93adec6f3d70a]::run_compiler::{closure#0}>::{closure#1}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#0}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>
59+
43: 0x110645424 - <<std[a184c2360da8cf6c]::thread::Builder>::spawn_unchecked_<rustc_interface[e97b870a1f7a7bd5]::util::run_in_thread_with_globals<rustc_interface[e97b870a1f7a7bd5]::util::run_in_thread_pool_with_globals<rustc_interface[e97b870a1f7a7bd5]::interface::run_compiler<core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>, rustc_driver_impl[17a93adec6f3d70a]::run_compiler::{closure#0}>::{closure#1}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#0}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1a7bc357c76bf42]::result::Result<(), rustc_span[961a2807afbea68d]::ErrorGuaranteed>>::{closure#1} as core[1a7bc357c76bf42]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
60+
44: 0x1127be2d4 - std::sys::pal::unix::thread::Thread::new::thread_start::h0b41f7e8e279ede6
61+
45: 0x193cbdf94 - __pthread_joiner_wake
1162

12-
error: only a `panic!` in `if`-then statement
13-
--> tests/ui/manual_assert.rs:33:5
14-
|
15-
LL | / if !a.is_empty() {
16-
LL | | panic!("qwqwq");
17-
LL | | }
18-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(a.is_empty(), "qwqwq");`
63+
error: the compiler unexpectedly panicked. this is a bug.
1964

20-
error: only a `panic!` in `if`-then statement
21-
--> tests/ui/manual_assert.rs:50:5
22-
|
23-
LL | / if b.is_empty() {
24-
LL | | panic!("panic1");
25-
LL | | }
26-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!b.is_empty(), "panic1");`
65+
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml
2766

28-
error: only a `panic!` in `if`-then statement
29-
--> tests/ui/manual_assert.rs:53:5
30-
|
31-
LL | / if b.is_empty() && a.is_empty() {
32-
LL | | panic!("panic2");
33-
LL | | }
34-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(b.is_empty() && a.is_empty()), "panic2");`
67+
note: please make sure that you have updated to the latest nightly
3568

36-
error: only a `panic!` in `if`-then statement
37-
--> tests/ui/manual_assert.rs:56:5
38-
|
39-
LL | / if a.is_empty() && !b.is_empty() {
40-
LL | | panic!("panic3");
41-
LL | | }
42-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(a.is_empty() && !b.is_empty()), "panic3");`
69+
note: rustc 1.85.0-nightly (6b6a867ae 2024-11-27) running on aarch64-apple-darwin
4370

44-
error: only a `panic!` in `if`-then statement
45-
--> tests/ui/manual_assert.rs:59:5
46-
|
47-
LL | / if b.is_empty() || a.is_empty() {
48-
LL | | panic!("panic4");
49-
LL | | }
50-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(b.is_empty() || a.is_empty()), "panic4");`
71+
note: compiler flags: -Z ui-testing -Z deduplicate-diagnostics=no
5172

52-
error: only a `panic!` in `if`-then statement
53-
--> tests/ui/manual_assert.rs:62:5
54-
|
55-
LL | / if a.is_empty() || !b.is_empty() {
56-
LL | | panic!("panic5");
57-
LL | | }
58-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`
59-
60-
error: only a `panic!` in `if`-then statement
61-
--> tests/ui/manual_assert.rs:65:5
62-
|
63-
LL | / if a.is_empty() {
64-
LL | | panic!("with expansion {}", one!())
65-
LL | | }
66-
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!a.is_empty(), "with expansion {}", one!());`
67-
68-
error: only a `panic!` in `if`-then statement
69-
--> tests/ui/manual_assert.rs:77:5
70-
|
71-
LL | / if a > 2 {
72-
LL | | // comment
73-
LL | | /* this is a
74-
LL | | multiline
75-
... |
76-
LL | | panic!("panic with comment") // comment after `panic!`
77-
LL | | }
78-
| |_____^
79-
|
80-
help: replace `if`-then-`panic!` with `assert!`
81-
|
82-
LL | assert!(!(a > 2), "panic with comment");
83-
|
84-
85-
error: only a `panic!` in `if`-then statement
86-
--> tests/ui/manual_assert.rs:91:25
87-
|
88-
LL | const BAR: () = if N == 0 {
89-
| _________________________^
90-
LL | | panic!()
91-
LL | | };
92-
| |_________^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(N == 0), )`
93-
94-
error: aborting due to 10 previous errors
73+
query stack during panic:
74+
#0 [analysis] running analysis passes on this crate
75+
end of query stack
76+
note: Clippy version: clippy 0.1.85 (4afba90b20 2024-12-11)
9577

0 commit comments

Comments
 (0)