Skip to content

Commit

Permalink
Auto merge of #7319 - m-ou-se:cfg-not-const, r=camsteffen
Browse files Browse the repository at this point in the history
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`]
  • Loading branch information
bors committed Jun 4, 2021
2 parents 5f746a1 + 38ab1a6 commit b1752f6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions clippy_utils/src/consts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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, _) => {
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/assertions_on_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}

0 comments on commit b1752f6

Please sign in to comment.