Skip to content

Commit 35147d4

Browse files
committed
move uninit_assumed_init to its own module
1 parent 8623b33 commit 35147d4

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod option_as_ref_deref;
1515
mod option_map_unwrap_or;
1616
mod skip_while_next;
1717
mod suspicious_map;
18+
mod uninit_assumed_init;
1819
mod unnecessary_filter_map;
1920
mod unnecessary_lazy_eval;
2021
mod unwrap_used;
@@ -1719,7 +1720,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17191720
filter_map_identity::check(cx, expr, arg_lists[0], method_spans[0]);
17201721
},
17211722
["count", "map"] => suspicious_map::check(cx, expr),
1722-
["assume_init"] => lint_maybe_uninit(cx, &arg_lists[0][0], expr),
1723+
["assume_init"] => uninit_assumed_init::check(cx, &arg_lists[0][0], expr),
17231724
["unwrap_or", arith @ ("checked_add" | "checked_sub" | "checked_mul")] => {
17241725
manual_saturating_arithmetic::lint(cx, expr, &arg_lists, &arith["checked_".len()..])
17251726
},
@@ -3548,34 +3549,6 @@ fn lint_into_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_
35483549
}
35493550
}
35503551

3551-
/// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
3552-
fn lint_maybe_uninit(cx: &LateContext<'_>, expr: &hir::Expr<'_>, outer: &hir::Expr<'_>) {
3553-
if_chain! {
3554-
if let hir::ExprKind::Call(ref callee, ref args) = expr.kind;
3555-
if args.is_empty();
3556-
if let hir::ExprKind::Path(ref path) = callee.kind;
3557-
if match_qpath(path, &paths::MEM_MAYBEUNINIT_UNINIT);
3558-
if !is_maybe_uninit_ty_valid(cx, cx.typeck_results().expr_ty_adjusted(outer));
3559-
then {
3560-
span_lint(
3561-
cx,
3562-
UNINIT_ASSUMED_INIT,
3563-
outer.span,
3564-
"this call for this type may be undefined behavior"
3565-
);
3566-
}
3567-
}
3568-
}
3569-
3570-
fn is_maybe_uninit_ty_valid(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
3571-
match ty.kind() {
3572-
ty::Array(ref component, _) => is_maybe_uninit_ty_valid(cx, component),
3573-
ty::Tuple(ref types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
3574-
ty::Adt(ref adt, _) => match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT),
3575-
_ => false,
3576-
}
3577-
}
3578-
35793552
fn lint_map_collect(
35803553
cx: &LateContext<'_>,
35813554
expr: &hir::Expr<'_>,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::utils::{match_def_path, match_qpath, paths, span_lint};
2+
use if_chain::if_chain;
3+
use rustc_hir as hir;
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty::{self, Ty};
6+
7+
use super::UNINIT_ASSUMED_INIT;
8+
9+
/// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
10+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, outer: &hir::Expr<'_>) {
11+
if_chain! {
12+
if let hir::ExprKind::Call(ref callee, ref args) = expr.kind;
13+
if args.is_empty();
14+
if let hir::ExprKind::Path(ref path) = callee.kind;
15+
if match_qpath(path, &paths::MEM_MAYBEUNINIT_UNINIT);
16+
if !is_maybe_uninit_ty_valid(cx, cx.typeck_results().expr_ty_adjusted(outer));
17+
then {
18+
span_lint(
19+
cx,
20+
UNINIT_ASSUMED_INIT,
21+
outer.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(ref component, _) => is_maybe_uninit_ty_valid(cx, component),
31+
ty::Tuple(ref types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
32+
ty::Adt(ref adt, _) => match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT),
33+
_ => false,
34+
}
35+
}

0 commit comments

Comments
 (0)