From 25639101ddd18f5bcc47e924015d9a3143840397 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 4 Jun 2021 10:43:25 +0200 Subject: [PATCH 1/2] Don't treat `cfg!()` as a constant. --- clippy_utils/src/consts.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs index 0d7fdeeb920..15c27d1a996 100644 --- a/clippy_utils/src/consts.rs +++ b/clippy_utils/src/consts.rs @@ -1,6 +1,6 @@ #![allow(clippy::float_cmp)] -use crate::{clip, sext, unsext}; +use crate::{clip, is_direct_expn_of, sext, unsext}; use if_chain::if_chain; use rustc_ast::ast::{self, LitFloatType, LitKind}; use rustc_data_structures::sync::Lrc; @@ -230,7 +230,13 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { match e.kind { ExprKind::Path(ref qpath) => self.fetch_path(qpath, e.hir_id, self.typeck_results.expr_ty(e)), ExprKind::Block(block, _) => self.block(block), - ExprKind::Lit(ref lit) => Some(lit_to_constant(&lit.node, self.typeck_results.expr_ty_opt(e))), + ExprKind::Lit(ref lit) => { + if is_direct_expn_of(e.span, "cfg").is_some() { + None + } else { + Some(lit_to_constant(&lit.node, self.typeck_results.expr_ty_opt(e))) + } + }, ExprKind::Array(vec) => self.multi(vec).map(Constant::Vec), ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple), ExprKind::Repeat(value, _) => { From 38ab1a616631944e8cef21c9441e2ef2ffa3a594 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 4 Jun 2021 10:43:39 +0200 Subject: [PATCH 2/2] Add test for not linting on assert!(cfg!(..)). --- tests/ui/assertions_on_constants.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index e989de65404..6617ca183a8 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -28,4 +28,7 @@ fn main() { debug_assert!(false); // #3948 assert_const!(3); assert_const!(-1); + + // Don't lint on this: + assert!(cfg!(feature = "hey") || cfg!(not(feature = "asdf"))); }