Skip to content

Commit 1c0617b

Browse files
committed
feat: make const_is_empty lint ignore external constants
1 parent 1159e2c commit 1c0617b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clippy_utils/src/consts.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::mir::interpret::{alloc_range, Scalar};
1313
use rustc_middle::mir::ConstValue;
1414
use rustc_middle::ty::{self, EarlyBinder, FloatTy, GenericArgsRef, IntTy, List, ScalarInt, Ty, TyCtxt, UintTy};
1515
use rustc_middle::{bug, mir, span_bug};
16+
use rustc_span::def_id::DefId;
1617
use rustc_span::symbol::{Ident, Symbol};
1718
use rustc_span::SyntaxContext;
1819
use rustc_target::abi::Size;
@@ -487,6 +488,14 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
487488
ExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr_is_empty(self.lcx.tcx.hir().body(body).value),
488489
ExprKind::DropTemps(e) => self.expr_is_empty(e),
489490
ExprKind::Path(ref qpath) => {
491+
if !self
492+
.typeck_results
493+
.qpath_res(qpath, e.hir_id)
494+
.opt_def_id()
495+
.is_some_and(DefId::is_local)
496+
{
497+
return None;
498+
}
490499
self.fetch_path_and_apply(qpath, e.hir_id, self.typeck_results.expr_ty(e), |this, result| {
491500
mir_is_empty(this.lcx, result)
492501
})

tests/ui/const_is_empty.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,8 @@ fn const_expressions() {
167167
let _ = const_rand().is_empty();
168168
// Do not lint, we do not recurse into functions
169169
}
170+
171+
fn constant_from_external_crate() {
172+
let _ = std::env::consts::EXE_EXTENSION.is_empty();
173+
// Do not lint, `exe_ext` comes from the `std` crate
174+
}

0 commit comments

Comments
 (0)