Skip to content

Commit 793c00a

Browse files
committed
update manual_assert lint structure to fix build and address review
1 parent 0f8c62c commit 793c00a

File tree

5 files changed

+109
-152
lines changed

5 files changed

+109
-152
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,29 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
5151
// Should this have a config value?
5252
&& !is_else_clause(cx.tcx, expr)
5353
{
54-
let mut applicability = Applicability::MachineApplicable;
55-
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
56-
if !comments.is_empty() {
57-
comments += "\n";
58-
}
59-
let cond_sugg = !sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability);
60-
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
61-
let base_sugg = format!("assert!({cond_sugg}, {format_args_snip}){semicolon}");
62-
63-
let indent = indent_of(cx, expr.span);
64-
let full_sugg = reindent_multiline(format!("{comments}{base_sugg}").into(), true, indent);
6554
span_lint_and_then(
6655
cx,
6756
MANUAL_ASSERT,
6857
expr.span,
6958
"only a `panic!` in `if`-then statement",
7059
|diag| {
60+
let mut applicability = Applicability::MachineApplicable;
61+
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
62+
if !comments.is_empty() {
63+
comments += "\n";
64+
}
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+
);
7174
diag.multipart_suggestion(
7275
"replace `if`-then-`panic!` with `assert!`",
73-
vec![(expr.span, full_sugg.into())],
76+
vec![(expr.span, full_sugg)],
7477
applicability,
7578
);
7679
},

tests/ui/manual_assert.edition2018.fixed

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ fn main() {
2727
{
2828
panic!("qaqaq{:?}", a);
2929
}
30+
//~^ manual_assert
3031
assert!(a.is_empty(), "qaqaq{:?}", a);
32+
//~^ manual_assert
3133
assert!(a.is_empty(), "qwqwq");
3234
if a.len() == 3 {
3335
println!("qwq");
@@ -43,11 +45,17 @@ fn main() {
4345
println!("qwq");
4446
}
4547
let b = vec![1, 2, 3];
48+
//~^ manual_assert
4649
assert!(!b.is_empty(), "panic1");
50+
//~^ manual_assert
4751
assert!(!(b.is_empty() && a.is_empty()), "panic2");
52+
//~^ manual_assert
4853
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
54+
//~^ manual_assert
4955
assert!(!(b.is_empty() || a.is_empty()), "panic4");
56+
//~^ manual_assert
5057
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
58+
//~^ manual_assert
5159
assert!(!a.is_empty(), "with expansion {}", one!());
5260
if a.is_empty() {
5361
let _ = 0;
@@ -58,19 +66,33 @@ fn main() {
5866

5967
fn issue7730(a: u8) {
6068
// Suggestion should preserve comment
69+
//~^ manual_assert
6170
// comment
6271
/* this is a
6372
multiline
6473
comment */
6574
/// Doc comment
6675
// comment after `panic!`
67-
assert!(!(a > 2), "panic with comment");
76+
assert!(a <= 2, "panic with comment");
6877
}
6978

7079
fn issue12505() {
7180
struct Foo<T, const N: usize>(T);
7281

7382
impl<T, const N: usize> Foo<T, N> {
74-
const BAR: () = assert!(!(N == 0), );
83+
const BAR: () = //~^ manual_assert
84+
assert!(N != 0, );
7585
}
7686
}
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+
}

tests/ui/manual_assert.edition2018.stderr

Lines changed: 23 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ LL | | }
1111
= help: to override `-D warnings` add `#[allow(clippy::manual_assert)]`
1212
help: replace `if`-then-`panic!` with `assert!`
1313
|
14-
LL - if !a.is_empty() {
15-
LL -
16-
LL - panic!("qaqaq{:?}", a);
17-
LL - }
14+
LL ~
1815
LL + assert!(a.is_empty(), "qaqaq{:?}", a);
1916
|
2017

2118
error: only a `panic!` in `if`-then statement
22-
--> tests/ui/manual_assert.rs:33:5
19+
--> tests/ui/manual_assert.rs:34:5
2320
|
2421
LL | / if !a.is_empty() {
2522
LL | |
@@ -29,15 +26,12 @@ LL | | }
2926
|
3027
help: replace `if`-then-`panic!` with `assert!`
3128
|
32-
LL - if !a.is_empty() {
33-
LL -
34-
LL - panic!("qwqwq");
35-
LL - }
29+
LL ~
3630
LL + assert!(a.is_empty(), "qwqwq");
3731
|
3832

3933
error: only a `panic!` in `if`-then statement
40-
--> tests/ui/manual_assert.rs:50:5
34+
--> tests/ui/manual_assert.rs:52:5
4135
|
4236
LL | / if b.is_empty() {
4337
LL | |
@@ -47,29 +41,10 @@ LL | | }
4741
|
4842
help: replace `if`-then-`panic!` with `assert!`
4943
|
50-
LL - if b.is_empty() {
51-
LL -
52-
LL - panic!("panic1");
53-
LL - }
44+
LL ~
5445
LL + assert!(!b.is_empty(), "panic1");
5546
|
5647

57-
error: only a `panic!` in `if`-then statement
58-
--> tests/ui/manual_assert.rs:53:5
59-
|
60-
LL | / if b.is_empty() && a.is_empty() {
61-
LL | | panic!("panic2");
62-
LL | | }
63-
| |_____^
64-
|
65-
help: replace `if`-then-`panic!` with `assert!`
66-
|
67-
LL - if b.is_empty() && a.is_empty() {
68-
LL - panic!("panic2");
69-
LL - }
70-
LL + assert!(!(b.is_empty() && a.is_empty()), "panic2");
71-
|
72-
7348
error: only a `panic!` in `if`-then statement
7449
--> tests/ui/manual_assert.rs:56:5
7550
|
@@ -81,15 +56,12 @@ LL | | }
8156
|
8257
help: replace `if`-then-`panic!` with `assert!`
8358
|
84-
LL - if b.is_empty() && a.is_empty() {
85-
LL -
86-
LL - panic!("panic2");
87-
LL - }
59+
LL ~
8860
LL + assert!(!(b.is_empty() && a.is_empty()), "panic2");
8961
|
9062

9163
error: only a `panic!` in `if`-then statement
92-
--> tests/ui/manual_assert.rs:62:5
64+
--> tests/ui/manual_assert.rs:60:5
9365
|
9466
LL | / if a.is_empty() && !b.is_empty() {
9567
LL | |
@@ -99,15 +71,12 @@ LL | | }
9971
|
10072
help: replace `if`-then-`panic!` with `assert!`
10173
|
102-
LL - if a.is_empty() && !b.is_empty() {
103-
LL -
104-
LL - panic!("panic3");
105-
LL - }
74+
LL ~
10675
LL + assert!(!(a.is_empty() && !b.is_empty()), "panic3");
10776
|
10877

10978
error: only a `panic!` in `if`-then statement
110-
--> tests/ui/manual_assert.rs:59:5
79+
--> tests/ui/manual_assert.rs:64:5
11180
|
11281
LL | / if b.is_empty() || a.is_empty() {
11382
LL | |
@@ -117,15 +86,12 @@ LL | | }
11786
|
11887
help: replace `if`-then-`panic!` with `assert!`
11988
|
120-
LL - if b.is_empty() || a.is_empty() {
121-
LL -
122-
LL - panic!("panic4");
123-
LL - }
89+
LL ~
12490
LL + assert!(!(b.is_empty() || a.is_empty()), "panic4");
12591
|
12692

12793
error: only a `panic!` in `if`-then statement
128-
--> tests/ui/manual_assert.rs:62:5
94+
--> tests/ui/manual_assert.rs:68:5
12995
|
13096
LL | / if a.is_empty() || !b.is_empty() {
13197
LL | |
@@ -135,15 +101,12 @@ LL | | }
135101
|
136102
help: replace `if`-then-`panic!` with `assert!`
137103
|
138-
LL - if a.is_empty() || !b.is_empty() {
139-
LL -
140-
LL - panic!("panic5");
141-
LL - }
104+
LL ~
142105
LL + assert!(!(a.is_empty() || !b.is_empty()), "panic5");
143106
|
144107

145108
error: only a `panic!` in `if`-then statement
146-
--> tests/ui/manual_assert.rs:65:5
109+
--> tests/ui/manual_assert.rs:72:5
147110
|
148111
LL | / if a.is_empty() {
149112
LL | |
@@ -153,15 +116,12 @@ LL | | }
153116
|
154117
help: replace `if`-then-`panic!` with `assert!`
155118
|
156-
LL - if a.is_empty() {
157-
LL -
158-
LL - panic!("with expansion {}", one!())
159-
LL - }
119+
LL ~
160120
LL + assert!(!a.is_empty(), "with expansion {}", one!());
161121
|
162122

163123
error: only a `panic!` in `if`-then statement
164-
--> tests/ui/manual_assert.rs:77:5
124+
--> tests/ui/manual_assert.rs:85:5
165125
|
166126
LL | / if a > 2 {
167127
LL | |
@@ -174,17 +134,18 @@ LL | | }
174134
|
175135
help: replace `if`-then-`panic!` with `assert!`
176136
|
177-
LL ~ // comment
137+
LL ~
138+
LL + // comment
178139
LL + /* this is a
179140
LL + multiline
180141
LL + comment */
181142
LL + /// Doc comment
182143
LL + // comment after `panic!`
183-
LL + assert!(!(a > 2), "panic with comment");
144+
LL + assert!(a <= 2, "panic with comment");
184145
|
185146

186147
error: only a `panic!` in `if`-then statement
187-
--> tests/ui/manual_assert.rs:91:25
148+
--> tests/ui/manual_assert.rs:100:25
188149
|
189150
LL | const BAR: () = if N == 0 {
190151
| _________________________^
@@ -195,15 +156,12 @@ LL | | };
195156
|
196157
help: replace `if`-then-`panic!` with `assert!`
197158
|
198-
LL - const BAR: () = if N == 0 {
199-
LL -
200-
LL - panic!()
201-
LL - };
202-
LL + const BAR: () = assert!(!(N == 0), );
159+
LL ~ const BAR: () =
160+
LL ~ assert!(N != 0, );
203161
|
204162

205163
error: only a `panic!` in `if`-then statement
206-
--> tests/ui/manual_assert.rs:116:5
164+
--> tests/ui/manual_assert.rs:114:5
207165
|
208166
LL | / if !is_x86_feature_detected!("ssse3") {
209167
LL | |
@@ -213,10 +171,7 @@ LL | | }
213171
|
214172
help: replace `if`-then-`panic!` with `assert!`
215173
|
216-
LL - if !is_x86_feature_detected!("ssse3") {
217-
LL -
218-
LL - panic!("SSSE3 is not supported");
219-
LL - }
174+
LL ~
220175
LL + assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
221176
|
222177

tests/ui/manual_assert.edition2021.fixed

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ fn main() {
2727
{
2828
panic!("qaqaq{:?}", a);
2929
}
30+
//~^ manual_assert
3031
assert!(a.is_empty(), "qaqaq{:?}", a);
32+
//~^ manual_assert
3133
assert!(a.is_empty(), "qwqwq");
3234
if a.len() == 3 {
3335
println!("qwq");
@@ -43,11 +45,17 @@ fn main() {
4345
println!("qwq");
4446
}
4547
let b = vec![1, 2, 3];
48+
//~^ manual_assert
4649
assert!(!b.is_empty(), "panic1");
50+
//~^ manual_assert
4751
assert!(!(b.is_empty() && a.is_empty()), "panic2");
52+
//~^ manual_assert
4853
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
54+
//~^ manual_assert
4955
assert!(!(b.is_empty() || a.is_empty()), "panic4");
56+
//~^ manual_assert
5057
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
58+
//~^ manual_assert
5159
assert!(!a.is_empty(), "with expansion {}", one!());
5260
if a.is_empty() {
5361
let _ = 0;
@@ -58,19 +66,33 @@ fn main() {
5866

5967
fn issue7730(a: u8) {
6068
// Suggestion should preserve comment
69+
//~^ manual_assert
6170
// comment
6271
/* this is a
6372
multiline
6473
comment */
6574
/// Doc comment
6675
// comment after `panic!`
67-
assert!(!(a > 2), "panic with comment");
76+
assert!(a <= 2, "panic with comment");
6877
}
6978

7079
fn issue12505() {
7180
struct Foo<T, const N: usize>(T);
7281

7382
impl<T, const N: usize> Foo<T, N> {
74-
const BAR: () = assert!(!(N == 0), );
83+
const BAR: () = //~^ manual_assert
84+
assert!(N != 0, );
7585
}
7686
}
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)