about summary refs log tree commit diff
path: root/clippy_utils/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-14 23:58:51 +0000
committerbors <bors@rust-lang.org>2022-04-14 23:58:51 +0000
commit80bcd9bc6e91cc00fbf7c9bf7aab13c0a4ec2367 (patch)
treef546e4dfb560ee72e05569bc96c293a7d8aa5991 /clippy_utils/src
parentaade96f9028ad3031d92de1c8a127f1e1d24a9fc (diff)
parent9f131e5a0bbaa2d4a9cd8cd2524695725680ceb4 (diff)
downloadrust-80bcd9bc6e91cc00fbf7c9bf7aab13c0a4ec2367.tar.gz
rust-80bcd9bc6e91cc00fbf7c9bf7aab13c0a4ec2367.zip
Auto merge of #8614 - pitaj:fix-7597, r=giraffate
assertions_on_constants: ignore indirect `cfg!`

Fixes #7597

changelog: [`assertions_on_constants`] ignore constants indirectly based on `cfg!`
Diffstat (limited to 'clippy_utils/src')
-rw-r--r--clippy_utils/src/consts.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs
index 1d6f7acab13..0e916ca8164 100644
--- a/clippy_utils/src/consts.rs
+++ b/clippy_utils/src/consts.rs
@@ -5,7 +5,7 @@ use if_chain::if_chain;
 use rustc_ast::ast::{self, LitFloatType, LitKind};
 use rustc_data_structures::sync::Lrc;
 use rustc_hir::def::{DefKind, Res};
-use rustc_hir::{BinOp, BinOpKind, Block, Expr, ExprKind, HirId, QPath, UnOp};
+use rustc_hir::{BinOp, BinOpKind, Block, Expr, ExprKind, HirId, Item, ItemKind, Node, QPath, UnOp};
 use rustc_lint::LateContext;
 use rustc_middle::mir::interpret::Scalar;
 use rustc_middle::ty::subst::{Subst, SubstsRef};
@@ -400,6 +400,22 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
         let res = self.typeck_results.qpath_res(qpath, id);
         match res {
             Res::Def(DefKind::Const | DefKind::AssocConst, def_id) => {
+                // Check if this constant is based on `cfg!(..)`,
+                // which is NOT constant for our purposes.
+                if let Some(node) = self.lcx.tcx.hir().get_if_local(def_id) &&
+                let Node::Item(&Item {
+                    kind: ItemKind::Const(_, body_id),
+                    ..
+                }) = node &&
+                let Node::Expr(&Expr {
+                    kind: ExprKind::Lit(_),
+                    span,
+                    ..
+                }) = self.lcx.tcx.hir().get(body_id.hir_id) &&
+                is_direct_expn_of(span, "cfg").is_some() {
+                    return None;
+                }
+
                 let substs = self.typeck_results.node_substs(id);
                 let substs = if self.substs.is_empty() {
                     substs