mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
invalid_value: factor finding dangerous inits into separate function
This commit is contained in:
parent
bdfd698f37
commit
5f7716d11b
@ -1876,9 +1876,35 @@ declare_lint_pass!(InvalidValue => [INVALID_VALUE]);
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &hir::Expr) {
|
||||
|
||||
#[derive(Debug)]
|
||||
enum InitKind { Zeroed, Uninit };
|
||||
|
||||
/// Determine if this expression is a "dangerous initialization".
|
||||
fn is_dangerous_init(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<InitKind> {
|
||||
const ZEROED_PATH: &[Symbol] = &[sym::core, sym::mem, sym::zeroed];
|
||||
const UININIT_PATH: &[Symbol] = &[sym::core, sym::mem, sym::uninitialized];
|
||||
|
||||
if let hir::ExprKind::Call(ref path_expr, ref _args) = expr.node {
|
||||
if let hir::ExprKind::Path(ref qpath) = path_expr.node {
|
||||
if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id)
|
||||
.opt_def_id()
|
||||
{
|
||||
if cx.match_def_path(def_id, &ZEROED_PATH) {
|
||||
return Some(InitKind::Zeroed);
|
||||
}
|
||||
if cx.match_def_path(def_id, &UININIT_PATH) {
|
||||
return Some(InitKind::Uninit);
|
||||
}
|
||||
// FIXME: Also detect `MaybeUninit::zeroed().assume_init()` and
|
||||
// `MaybeUninit::uninit().assume_init()`.
|
||||
// FIXME: Also detect `transmute` from 0.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Information about why a type cannot be initialized this way.
|
||||
/// Contains an error message and optionally a span to point at.
|
||||
type InitError = (String, Option<Span>);
|
||||
@ -1933,12 +1959,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
|
||||
}
|
||||
}
|
||||
|
||||
if let hir::ExprKind::Call(ref path_expr, ref _args) = expr.node {
|
||||
if let hir::ExprKind::Path(ref qpath) = path_expr.node {
|
||||
if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
|
||||
if cx.match_def_path(def_id, &ZEROED_PATH) ||
|
||||
cx.match_def_path(def_id, &UININIT_PATH)
|
||||
{
|
||||
if let Some(init) = is_dangerous_init(cx, expr) {
|
||||
// This conjures an instance of a type out of nothing,
|
||||
// using zeroed or uninitialized memory.
|
||||
// We are extremely conservative with what we warn about.
|
||||
@ -1950,11 +1971,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
|
||||
&format!(
|
||||
"the type `{}` does not permit {}",
|
||||
conjured_ty,
|
||||
if cx.match_def_path(def_id, &ZEROED_PATH) {
|
||||
"zero-initialization"
|
||||
} else {
|
||||
"being left uninitialized"
|
||||
}
|
||||
match init {
|
||||
InitKind::Zeroed => "zero-initialization",
|
||||
InitKind::Uninit => "being left uninitialized",
|
||||
},
|
||||
),
|
||||
);
|
||||
err.span_label(expr.span,
|
||||
@ -1970,6 +1990,3 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user