Skip to content

Commit ba947c5

Browse files
Fix manual_assert suggests wrongly for macros (#15264)
Closes #15227 changelog: [`manual_assert`] fix wrong suggestions for macros
2 parents d9acd91 + 901ab5b commit ba947c5

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
6060
ExprKind::Unary(UnOp::Not, e) => (e, ""),
6161
_ => (cond, "!"),
6262
};
63-
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_paren();
63+
let cond_sugg =
64+
sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability).maybe_paren();
6465
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
6566
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
6667
// we show to the user the suggestion without the comments, but when applying the fix, include the

tests/ui/manual_assert.edition2018.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,23 @@ LL - };
189189
LL + const BAR: () = assert!(!(N == 0), );
190190
|
191191

192-
error: aborting due to 10 previous errors
192+
error: only a `panic!` in `if`-then statement
193+
--> tests/ui/manual_assert.rs:116:5
194+
|
195+
LL | / if !is_x86_feature_detected!("ssse3") {
196+
LL | |
197+
LL | | panic!("SSSE3 is not supported");
198+
LL | | }
199+
| |_____^
200+
|
201+
help: try instead
202+
|
203+
LL - if !is_x86_feature_detected!("ssse3") {
204+
LL -
205+
LL - panic!("SSSE3 is not supported");
206+
LL - }
207+
LL + assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
208+
|
209+
210+
error: aborting due to 11 previous errors
193211

tests/ui/manual_assert.edition2021.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,23 @@ LL - };
189189
LL + const BAR: () = assert!(!(N == 0), );
190190
|
191191

192-
error: aborting due to 10 previous errors
192+
error: only a `panic!` in `if`-then statement
193+
--> tests/ui/manual_assert.rs:116:5
194+
|
195+
LL | / if !is_x86_feature_detected!("ssse3") {
196+
LL | |
197+
LL | | panic!("SSSE3 is not supported");
198+
LL | | }
199+
| |_____^
200+
|
201+
help: try instead
202+
|
203+
LL - if !is_x86_feature_detected!("ssse3") {
204+
LL -
205+
LL - panic!("SSSE3 is not supported");
206+
LL - }
207+
LL + assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
208+
|
209+
210+
error: aborting due to 11 previous errors
193211

tests/ui/manual_assert.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,17 @@ fn issue12505() {
105105
};
106106
}
107107
}
108+
109+
fn issue15227(left: u64, right: u64) -> u64 {
110+
macro_rules! is_x86_feature_detected {
111+
($feature:literal) => {
112+
$feature.len() > 0 && $feature.starts_with("ss")
113+
};
114+
}
115+
116+
if !is_x86_feature_detected!("ssse3") {
117+
//~^ manual_assert
118+
panic!("SSSE3 is not supported");
119+
}
120+
unsafe { todo!() }
121+
}

0 commit comments

Comments
 (0)