2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2021-03-16 11:06:34 -05:00
|
|
|
use clippy_utils::{match_def_path, match_qpath, paths};
|
2021-03-02 10:03:47 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
|
|
|
|
use super::UNINIT_ASSUMED_INIT;
|
|
|
|
|
|
|
|
/// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
|
2021-03-10 23:40:20 -06:00
|
|
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
|
2021-03-02 10:03:47 -06:00
|
|
|
if_chain! {
|
2021-04-02 16:35:32 -05:00
|
|
|
if let hir::ExprKind::Call(callee, args) = recv.kind;
|
2021-03-02 10:03:47 -06:00
|
|
|
if args.is_empty();
|
|
|
|
if let hir::ExprKind::Path(ref path) = callee.kind;
|
|
|
|
if match_qpath(path, &paths::MEM_MAYBEUNINIT_UNINIT);
|
2021-03-10 23:40:20 -06:00
|
|
|
if !is_maybe_uninit_ty_valid(cx, cx.typeck_results().expr_ty_adjusted(expr));
|
2021-03-02 10:03:47 -06:00
|
|
|
then {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNINIT_ASSUMED_INIT,
|
2021-03-10 23:40:20 -06:00
|
|
|
expr.span,
|
2021-03-02 10:03:47 -06:00
|
|
|
"this call for this type may be undefined behavior"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_maybe_uninit_ty_valid(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
|
|
|
match ty.kind() {
|
2021-04-02 16:35:32 -05:00
|
|
|
ty::Array(component, _) => is_maybe_uninit_ty_valid(cx, component),
|
|
|
|
ty::Tuple(types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
|
|
|
|
ty::Adt(adt, _) => match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT),
|
2021-03-02 10:03:47 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|