Skip to content

Commit 8ef9057

Browse files
committed
chore: update manual_assert span suggestions
1 parent 49ae1d4 commit 8ef9057

File tree

6 files changed

+297
-166
lines changed

6 files changed

+297
-166
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::{is_panic, root_macro_call};
3+
use clippy_utils::source::{indent_of, reindent_multiline};
34
use clippy_utils::{higher, is_else_clause, is_parent_stmt, peel_blocks_with_stmt, span_extract_comment, sugg};
45
use rustc_errors::Applicability;
56
use rustc_hir::{Expr, ExprKind};
@@ -50,32 +51,32 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
5051
// Should this have a config value?
5152
&& !is_else_clause(cx.tcx, expr)
5253
{
53-
let mut applicability = Applicability::MachineApplicable;
54-
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
55-
if !comments.is_empty() {
56-
comments += "\n";
57-
}
58-
let cond_sugg = !sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability);
59-
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
60-
let sugg = format!("assert!({cond_sugg}, {format_args_snip}){semicolon}");
61-
// we show to the user the suggestion without the comments, but when applying the fix, include the
62-
// comments in the block
6354
span_lint_and_then(
6455
cx,
6556
MANUAL_ASSERT,
6657
expr.span,
6758
"only a `panic!` in `if`-then statement",
6859
|diag| {
69-
// comments can be noisy, do not show them to the user
60+
let mut applicability = Applicability::MachineApplicable;
61+
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
7062
if !comments.is_empty() {
71-
diag.tool_only_span_suggestion(
72-
expr.span.shrink_to_lo(),
73-
"add comments back",
74-
comments,
75-
applicability,
76-
);
63+
comments += "\n";
7764
}
78-
diag.span_suggestion(expr.span, "try instead", sugg, applicability);
65+
let cond_sugg = !sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability);
66+
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
67+
68+
let indent = indent_of(cx, expr.span);
69+
let full_sugg = reindent_multiline(
70+
format!("{comments}assert!({cond_sugg}, {format_args_snip}){semicolon}").as_str(),
71+
true,
72+
indent,
73+
);
74+
diag.span_suggestion_verbose(
75+
expr.span,
76+
"replace `if`-then-`panic!` with `assert!`",
77+
full_sugg,
78+
applicability,
79+
);
7980
},
8081
);
8182
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//@revisions: edition2018 edition2021
2+
//@[edition2018] edition:2018
3+
//@[edition2021] edition:2021
4+
5+
#![warn(clippy::manual_assert)]
6+
#![allow(dead_code, unused_doc_comments)]
7+
#![allow(clippy::nonminimal_bool, clippy::uninlined_format_args, clippy::useless_vec)]
8+
9+
macro_rules! one {
10+
() => {
11+
1
12+
};
13+
}
14+
15+
fn main() {
16+
let a = vec![1, 2, 3];
17+
let c = Some(2);
18+
if !a.is_empty()
19+
&& a.len() == 3
20+
&& c.is_some()
21+
&& !a.is_empty()
22+
&& a.len() == 3
23+
&& !a.is_empty()
24+
&& a.len() == 3
25+
&& !a.is_empty()
26+
&& a.len() == 3
27+
{
28+
panic!("qaqaq{:?}", a);
29+
}
30+
//~^ manual_assert
31+
assert!(a.is_empty(), "qaqaq{:?}", a);
32+
//~^ manual_assert
33+
assert!(a.is_empty(), "qwqwq");
34+
if a.len() == 3 {
35+
println!("qwq");
36+
println!("qwq");
37+
println!("qwq");
38+
}
39+
if let Some(b) = c {
40+
panic!("orz {}", b);
41+
}
42+
if a.len() == 3 {
43+
panic!("qaqaq");
44+
} else {
45+
println!("qwq");
46+
}
47+
let b = vec![1, 2, 3];
48+
//~^ manual_assert
49+
assert!(!b.is_empty(), "panic1");
50+
//~^ manual_assert
51+
assert!(!(b.is_empty() && a.is_empty()), "panic2");
52+
//~^ manual_assert
53+
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
54+
//~^ manual_assert
55+
assert!(!(b.is_empty() || a.is_empty()), "panic4");
56+
//~^ manual_assert
57+
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
58+
//~^ manual_assert
59+
assert!(!a.is_empty(), "with expansion {}", one!());
60+
if a.is_empty() {
61+
let _ = 0;
62+
} else if a.len() == 1 {
63+
panic!("panic6");
64+
}
65+
}
66+
67+
fn issue7730(a: u8) {
68+
// Suggestion should preserve comment
69+
//~^ manual_assert
70+
// comment
71+
/* this is a
72+
multiline
73+
comment */
74+
/// Doc comment
75+
// comment after `panic!`
76+
assert!(a <= 2, "panic with comment");
77+
}
78+
79+
fn issue12505() {
80+
struct Foo<T, const N: usize>(T);
81+
82+
impl<T, const N: usize> Foo<T, N> {
83+
const BAR: () = //~^ manual_assert
84+
assert!(N != 0, );
85+
}
86+
}
87+
88+
fn issue15227(left: u64, right: u64) -> u64 {
89+
macro_rules! is_x86_feature_detected {
90+
($feature:literal) => {
91+
$feature.len() > 0 && $feature.starts_with("ss")
92+
};
93+
}
94+
95+
//~^ manual_assert
96+
assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
97+
unsafe { todo!() }
98+
}

0 commit comments

Comments
 (0)