Skip to content

Commit 4106c0f

Browse files
committed
fix: don't lint on if cfg!(..) { panic!(..) }
1 parent 69f2493 commit 4106c0f

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_session::declare_lint_pass;
10+
use rustc_span::sym;
1011

1112
declare_clippy_lint! {
1213
/// ### What it does
@@ -39,6 +40,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
3940
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
4041
if let Some(higher::If { cond, then, r#else: None }) = higher::If::hir(expr)
4142
&& !matches!(cond.kind, ExprKind::Let(_))
43+
// Don't lint on `if cfg!(..) { panic!(..) }
44+
&& root_macro_call(cond.span).is_none_or(|macro_call| cx.tcx.item_name(macro_call.def_id) != sym::cfg)
4245
&& !expr.span.from_expansion()
4346
&& let then = peel_blocks_with_stmt(then)
4447
&& let Some(macro_call) = root_macro_call(then.span)

tests/ui/manual_assert.edition2018.stderr

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,25 @@ LL + const BAR: () = assert!(N != 0, );
190190
|
191191

192192
error: only a `panic!` in `if`-then statement
193-
--> tests/ui/manual_assert.rs:116:5
193+
--> tests/ui/manual_assert.rs:119:5
194+
|
195+
LL | / if foo!() {
196+
LL | |
197+
LL | | panic!("not a `cfg`, so lint")
198+
LL | | }
199+
| |_____^
200+
|
201+
help: try instead
202+
|
203+
LL - if foo!() {
204+
LL -
205+
LL - panic!("not a `cfg`, so lint")
206+
LL - }
207+
LL + assert!(!foo!(), "not a `cfg`, so lint")
208+
|
209+
210+
error: only a `panic!` in `if`-then statement
211+
--> tests/ui/manual_assert.rs:132:5
194212
|
195213
LL | / if !is_x86_feature_detected!("ssse3") {
196214
LL | |
@@ -207,5 +225,5 @@ LL - }
207225
LL + assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
208226
|
209227

210-
error: aborting due to 11 previous errors
228+
error: aborting due to 12 previous errors
211229

tests/ui/manual_assert.edition2021.stderr

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,25 @@ LL + const BAR: () = assert!(N != 0, );
190190
|
191191

192192
error: only a `panic!` in `if`-then statement
193-
--> tests/ui/manual_assert.rs:116:5
193+
--> tests/ui/manual_assert.rs:119:5
194+
|
195+
LL | / if foo!() {
196+
LL | |
197+
LL | | panic!("not a `cfg`, so lint")
198+
LL | | }
199+
| |_____^
200+
|
201+
help: try instead
202+
|
203+
LL - if foo!() {
204+
LL -
205+
LL - panic!("not a `cfg`, so lint")
206+
LL - }
207+
LL + assert!(!foo!(), "not a `cfg`, so lint")
208+
|
209+
210+
error: only a `panic!` in `if`-then statement
211+
--> tests/ui/manual_assert.rs:132:5
194212
|
195213
LL | / if !is_x86_feature_detected!("ssse3") {
196214
LL | |
@@ -207,5 +225,5 @@ LL - }
207225
LL + assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
208226
|
209227

210-
error: aborting due to 11 previous errors
228+
error: aborting due to 12 previous errors
211229

tests/ui/manual_assert.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ fn issue12505() {
106106
}
107107
}
108108

109+
fn issue13883() {
110+
if cfg!(debug_assertions) {
111+
panic!("foobar")
112+
}
113+
114+
macro_rules! foo {
115+
() => {
116+
true
117+
};
118+
}
119+
if foo!() {
120+
//~^ manual_assert
121+
panic!("not a `cfg`, so lint")
122+
}
123+
}
124+
109125
fn issue15227(left: u64, right: u64) -> u64 {
110126
macro_rules! is_x86_feature_detected {
111127
($feature:literal) => {

0 commit comments

Comments
 (0)