]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/uninit_assumed_init.rs
Auto merge of #7546 - mgeier:patch-1, r=giraffate
[rust.git] / clippy_lints / src / methods / uninit_assumed_init.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{is_expr_path_def_path, match_def_path, paths};
3 use if_chain::if_chain;
4 use rustc_hir as hir;
5 use rustc_lint::LateContext;
6 use rustc_middle::ty::{self, Ty};
7
8 use super::UNINIT_ASSUMED_INIT;
9
10 /// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
11 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
12     if_chain! {
13         if let hir::ExprKind::Call(callee, args) = recv.kind;
14         if args.is_empty();
15         if is_expr_path_def_path(cx, callee, &paths::MEM_MAYBEUNINIT_UNINIT);
16         if !is_maybe_uninit_ty_valid(cx, cx.typeck_results().expr_ty_adjusted(expr));
17         then {
18             span_lint(
19                 cx,
20                 UNINIT_ASSUMED_INIT,
21                 expr.span,
22                 "this call for this type may be undefined behavior"
23             );
24         }
25     }
26 }
27
28 fn is_maybe_uninit_ty_valid(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
29     match ty.kind() {
30         ty::Array(component, _) => is_maybe_uninit_ty_valid(cx, component),
31         ty::Tuple(types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
32         ty::Adt(adt, _) => match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT),
33         _ => false,
34     }
35 }