Skip to content

Commit

Permalink
chore: use multipart_suggestions for manual_assert, include comments …
Browse files Browse the repository at this point in the history
…in suggestion
  • Loading branch information
scottgerring committed Dec 11, 2024
1 parent 09589ff commit bc9e5a6
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 500 deletions.
26 changes: 8 additions & 18 deletions clippy_lints/src/manual_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,21 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par();
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
let base_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| {
// If we have comments, use a tool_only suggestion to add them back.
// Comments can be noisy, and this will hide them from the user's output.
if !comments.is_empty() {
diag.tool_only_span_suggestion(
expr.span.shrink_to_lo(),
"add comments back",
comments,
applicability,
);
}
// If we have comments to retain, include them in the final suggestion, if
// not, don't.
let suggestions = if comments.is_empty() {
vec![(expr.span.shrink_to_lo(), comments), (expr.span, base_sugg.clone())]
} else {
vec![(expr.span, base_sugg.clone())]
};

// And setup a multipart suggestion for the user-facing part.
diag.multipart_suggestion(
"replace `if`-then-`panic!` with `assert!`",
vec![(expr.span, base_sugg.clone())],
applicability,
);
diag.multipart_suggestion("replace `if`-then-`panic!` with `assert!`", suggestions, applicability);
},
);
}
Expand Down
83 changes: 0 additions & 83 deletions tests/ui/manual_assert.edition2018.1.fixed

This file was deleted.

70 changes: 0 additions & 70 deletions tests/ui/manual_assert.edition2018.2.fixed

This file was deleted.

158 changes: 70 additions & 88 deletions tests/ui/manual_assert.edition2018.stderr
Original file line number Diff line number Diff line change
@@ -1,95 +1,77 @@
error: only a `panic!` in `if`-then statement
--> tests/ui/manual_assert.rs:30:5
|
LL | / if !a.is_empty() {
LL | | panic!("qaqaq{:?}", a);
LL | | }
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(a.is_empty(), "qaqaq{:?}", a);`
|
= note: `-D clippy::manual-assert` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_assert)]`
thread 'rustc' panicked at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:50:9:
assertion `left == right` failed: span must not be empty and have no suggestion
left: Some(SubstitutionPart { span: tests/ui/manual_assert.rs:30:5: 30:5 (#0), snippet: "" })
right: None
stack backtrace:
0: 0x1127b15b8 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hd5de7b0df0729e03
1: 0x10fa14e64 - core::fmt::write::h2d6cb8c661d0a83d
2: 0x1127a5960 - std::io::Write::write_fmt::h0f134b74b751e0f9
3: 0x1127b1478 - std::sys::backtrace::BacktraceLock::print::h93b2b495ebfc2cb2
4: 0x1127b3948 - std::panicking::default_hook::{{closure}}::h5e69c7a046eca612
5: 0x1127b3790 - std::panicking::default_hook::h9f5f5724d303148a
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
7: 0x1127b4214 - std::panicking::rust_panic_with_hook::hab3d98f4160d14fd
8: 0x1127b3e50 - std::panicking::begin_panic_handler::{{closure}}::h1577ffea1a58f5f7
9: 0x1127b1a54 - std::sys::backtrace::__rust_end_short_backtrace::hae37950ca15bb119
10: 0x1127b3b14 - _rust_begin_unwind
11: 0x114e9725c - core::panicking::panic_fmt::h987dd6a54d7aba16
12: 0x114e975f0 - core::panicking::assert_failed_inner::h12c770c2425fee7d
13: 0x103102510 - core::panicking::assert_failed::hc915165530e4fffb
at /Users/scott.gerring/.rustup/toolchains/nightly-2024-11-28-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panicking.rs:373:5
14: 0x102190eac - clippy_utils::diagnostics::validate_diag::h239c2d3579525ceb
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:50:9
15: 0x1021a7d1c - clippy_utils::diagnostics::span_lint_and_then::{{closure}}::h9b8b186ec716fe56
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:261:9
16: 0x102880df4 - core::ops::function::FnOnce::call_once{{vtable.shim}}::hf14a66775935d916
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
17: 0x1113b1670 - rustc_middle[7019c7ba16fcd5cd]::lint::lint_level::lint_level_impl
18: 0x102420a80 - rustc_middle::lint::lint_level::hcb523238415a1e29
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
19: 0x102900f00 - rustc_middle::ty::context::TyCtxt::node_span_lint::h7532365959f7d1f5
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
20: 0x10273a51c - <rustc_lint::context::LateContext as rustc_lint::context::LintContext>::opt_span_lint::hfbf1866d04803dc5
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
21: 0x10271cc1c - rustc_lint::context::LintContext::span_lint::h28363b347076190e
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
22: 0x1021969c0 - clippy_utils::diagnostics::span_lint_and_then::h479f5abc48e54fa2
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_utils/src/diagnostics.rs:255:5
23: 0x102835ee4 - <clippy_lints::manual_assert::ManualAssert as rustc_lint::passes::LateLintPass>::check_expr::hbc06e8db702404ba
at /Users/scott.gerring/Documents/code/3rdparty/rust-clippy/clippy_lints/src/manual_assert.rs:66:13
24: 0x11113bf8c - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_expr::{closure#0}
25: 0x111082e60 - rustc_hir[e37c4407e29c46ca]::intravisit::walk_expr::<rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass>>
26: 0x11113bfa0 - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_expr::{closure#0}
27: 0x11113b44c - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_nested_body
28: 0x11113c398 - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_fn
29: 0x111084964 - rustc_hir[e37c4407e29c46ca]::intravisit::walk_item::<rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass>>
30: 0x111134a3c - <rustc_lint[385369ae408eed1c]::late::LateContextAndPass<rustc_lint[385369ae408eed1c]::late::RuntimeCombinedLateLintPass> as rustc_hir[e37c4407e29c46ca]::intravisit::Visitor>::visit_nested_item
31: 0x11113e570 - rustc_lint[385369ae408eed1c]::late::check_crate::{closure#0}
32: 0x11113dc0c - rustc_lint[385369ae408eed1c]::late::check_crate
33: 0x110f41640 - <rustc_session[ea051583fbb3404]::session::Session>::time::<(), rustc_interface[e97b870a1f7a7bd5]::passes::analysis::{closure#0}>
34: 0x110ff20bc - rustc_interface[e97b870a1f7a7bd5]::passes::analysis
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]>>
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
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>
38: 0x111d6e564 - rustc_query_impl[3faacfedda06551]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
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>>
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>>
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}>
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>>
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}
44: 0x1127be2d4 - std::sys::pal::unix::thread::Thread::new::thread_start::h0b41f7e8e279ede6
45: 0x193cbdf94 - __pthread_joiner_wake

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

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

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

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

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

error: only a `panic!` in `if`-then statement
--> tests/ui/manual_assert.rs:62:5
|
LL | / if a.is_empty() || !b.is_empty() {
LL | | panic!("panic5");
LL | | }
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`

error: only a `panic!` in `if`-then statement
--> tests/ui/manual_assert.rs:65:5
|
LL | / if a.is_empty() {
LL | | panic!("with expansion {}", one!())
LL | | }
| |_____^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!a.is_empty(), "with expansion {}", one!());`

error: only a `panic!` in `if`-then statement
--> tests/ui/manual_assert.rs:77:5
|
LL | / if a > 2 {
LL | | // comment
LL | | /* this is a
LL | | multiline
... |
LL | | panic!("panic with comment") // comment after `panic!`
LL | | }
| |_____^
|
help: replace `if`-then-`panic!` with `assert!`
|
LL | assert!(!(a > 2), "panic with comment");
|

error: only a `panic!` in `if`-then statement
--> tests/ui/manual_assert.rs:91:25
|
LL | const BAR: () = if N == 0 {
| _________________________^
LL | | panic!()
LL | | };
| |_________^ help: replace `if`-then-`panic!` with `assert!`: `assert!(!(N == 0), )`

error: aborting due to 10 previous errors
query stack during panic:
#0 [analysis] running analysis passes on this crate
end of query stack
note: Clippy version: clippy 0.1.85 (4afba90b20 2024-12-11)

Loading

0 comments on commit bc9e5a6

Please sign in to comment.