Skip to content

Commit

Permalink
feat: make const_is_empty lint ignore external constants
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Feb 26, 2024
1 parent 1159e2c commit 1c0617b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions clippy_utils/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_middle::mir::interpret::{alloc_range, Scalar};
use rustc_middle::mir::ConstValue;
use rustc_middle::ty::{self, EarlyBinder, FloatTy, GenericArgsRef, IntTy, List, ScalarInt, Ty, TyCtxt, UintTy};
use rustc_middle::{bug, mir, span_bug};
use rustc_span::def_id::DefId;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::SyntaxContext;
use rustc_target::abi::Size;
Expand Down Expand Up @@ -487,6 +488,14 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
ExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr_is_empty(self.lcx.tcx.hir().body(body).value),
ExprKind::DropTemps(e) => self.expr_is_empty(e),
ExprKind::Path(ref qpath) => {
if !self
.typeck_results
.qpath_res(qpath, e.hir_id)
.opt_def_id()
.is_some_and(DefId::is_local)
{
return None;
}
self.fetch_path_and_apply(qpath, e.hir_id, self.typeck_results.expr_ty(e), |this, result| {
mir_is_empty(this.lcx, result)
})
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const_is_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,8 @@ fn const_expressions() {
let _ = const_rand().is_empty();
// Do not lint, we do not recurse into functions
}

fn constant_from_external_crate() {
let _ = std::env::consts::EXE_EXTENSION.is_empty();
// Do not lint, `exe_ext` comes from the `std` crate
}

0 comments on commit 1c0617b

Please sign in to comment.