Skip to content

Commit b1752f6

Browse files
committed
Auto merge of rust-lang#7319 - m-ou-se:cfg-not-const, r=camsteffen
Don't warn about `cfg!(..)` as a constant in assertions This makes clippy understand that `cfg!(..)` is not just a hardcoded `true` or `false` (even though it expands to one of those). cc `@khyperia` changelog: Don't treat `cfg!(..)` as a constant in [`assertions-on-constants`]
2 parents 5f746a1 + 38ab1a6 commit b1752f6

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

clippy_utils/src/consts.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::float_cmp)]
22

3-
use crate::{clip, sext, unsext};
3+
use crate::{clip, is_direct_expn_of, sext, unsext};
44
use if_chain::if_chain;
55
use rustc_ast::ast::{self, LitFloatType, LitKind};
66
use rustc_data_structures::sync::Lrc;
@@ -230,7 +230,13 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
230230
match e.kind {
231231
ExprKind::Path(ref qpath) => self.fetch_path(qpath, e.hir_id, self.typeck_results.expr_ty(e)),
232232
ExprKind::Block(block, _) => self.block(block),
233-
ExprKind::Lit(ref lit) => Some(lit_to_constant(&lit.node, self.typeck_results.expr_ty_opt(e))),
233+
ExprKind::Lit(ref lit) => {
234+
if is_direct_expn_of(e.span, "cfg").is_some() {
235+
None
236+
} else {
237+
Some(lit_to_constant(&lit.node, self.typeck_results.expr_ty_opt(e)))
238+
}
239+
},
234240
ExprKind::Array(vec) => self.multi(vec).map(Constant::Vec),
235241
ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple),
236242
ExprKind::Repeat(value, _) => {

tests/ui/assertions_on_constants.rs

+3
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ fn main() {
2828
debug_assert!(false); // #3948
2929
assert_const!(3);
3030
assert_const!(-1);
31+
32+
// Don't lint on this:
33+
assert!(cfg!(feature = "hey") || cfg!(not(feature = "asdf")));
3134
}

0 commit comments

Comments
 (0)