Skip to content

Commit 11da8c1

Browse files
committed
Auto merge of #4479 - rust-lang:uninit_assume_init, r=flip1995
lint against `MaybeUninit::uninit().assume_init()` changelog: add `uninit_assumed_init` lint This fixes #4272
2 parents aeadf15 + b01f2d1 commit 11da8c1

File tree

8 files changed

+118
-2
lines changed

8 files changed

+118
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ Released 2018-09-13
11761176
[`type_repetition_in_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds
11771177
[`unicode_not_nfc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unicode_not_nfc
11781178
[`unimplemented`]: https://rust-lang.github.io/rust-clippy/master/index.html#unimplemented
1179+
[`uninit_assumed_init`]: https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init
11791180
[`unit_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
11801181
[`unit_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_cmp
11811182
[`unknown_clippy_lints`]: https://rust-lang.github.io/rust-clippy/master/index.html#unknown_clippy_lints

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 311 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 312 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
804804
methods::STRING_EXTEND_CHARS,
805805
methods::SUSPICIOUS_MAP,
806806
methods::TEMPORARY_CSTRING_AS_PTR,
807+
methods::UNINIT_ASSUMED_INIT,
807808
methods::UNNECESSARY_FILTER_MAP,
808809
methods::UNNECESSARY_FOLD,
809810
methods::USELESS_ASREF,
@@ -1116,6 +1117,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
11161117
methods::CLONE_DOUBLE_REF,
11171118
methods::INTO_ITER_ON_ARRAY,
11181119
methods::TEMPORARY_CSTRING_AS_PTR,
1120+
methods::UNINIT_ASSUMED_INIT,
11191121
minmax::MIN_MAX,
11201122
misc::CMP_NAN,
11211123
misc::FLOAT_CMP,

clippy_lints/src/methods/mod.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,38 @@ declare_clippy_lint! {
951951
"suspicious usage of map"
952952
}
953953

954+
declare_clippy_lint! {
955+
/// **What it does:** Checks for `MaybeUninit::uninit().assume_init()`.
956+
///
957+
/// **Why is this bad?** For most types, this is undefined behavior.
958+
///
959+
/// **Known problems:** For now, we accept empty tuples and tuples / arrays
960+
/// of `MaybeUninit`. There may be other types that allow uninitialized
961+
/// data, but those are not yet rigorously defined.
962+
///
963+
/// **Example:**
964+
///
965+
/// ```rust
966+
/// // Beware the UB
967+
/// use std::mem::MaybeUninit;
968+
///
969+
/// let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
970+
/// ```
971+
///
972+
/// Note that the following is OK:
973+
///
974+
/// ```rust
975+
/// use std::mem::MaybeUninit;
976+
///
977+
/// let _: [MaybeUninit<bool>; 5] = unsafe {
978+
/// MaybeUninit::uninit().assume_init()
979+
/// };
980+
/// ```
981+
pub UNINIT_ASSUMED_INIT,
982+
correctness,
983+
"`MaybeUninit::uninit().assume_init()`"
984+
}
985+
954986
declare_lint_pass!(Methods => [
955987
OPTION_UNWRAP_USED,
956988
RESULT_UNWRAP_USED,
@@ -991,6 +1023,7 @@ declare_lint_pass!(Methods => [
9911023
INTO_ITER_ON_ARRAY,
9921024
INTO_ITER_ON_REF,
9931025
SUSPICIOUS_MAP,
1026+
UNINIT_ASSUMED_INIT,
9941027
]);
9951028

9961029
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
@@ -1038,6 +1071,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10381071
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0]),
10391072
["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
10401073
["count", "map"] => lint_suspicious_map(cx, expr),
1074+
["assume_init"] => lint_maybe_uninit(cx, &arg_lists[0][0], expr),
10411075
_ => {},
10421076
}
10431077

@@ -2662,6 +2696,37 @@ fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: Ty<'_
26622696
}
26632697
}
26642698

2699+
/// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
2700+
fn lint_maybe_uninit(cx: &LateContext<'_, '_>, expr: &hir::Expr, outer: &hir::Expr) {
2701+
if_chain! {
2702+
if let hir::ExprKind::Call(ref callee, ref args) = expr.node;
2703+
if args.is_empty();
2704+
if let hir::ExprKind::Path(ref path) = callee.node;
2705+
if match_qpath(path, &paths::MEM_MAYBEUNINIT_UNINIT);
2706+
if !is_maybe_uninit_ty_valid(cx, cx.tables.expr_ty_adjusted(outer));
2707+
then {
2708+
span_lint(
2709+
cx,
2710+
UNINIT_ASSUMED_INIT,
2711+
outer.span,
2712+
"this call for this type may be undefined behavior"
2713+
);
2714+
}
2715+
}
2716+
}
2717+
2718+
fn is_maybe_uninit_ty_valid(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> bool {
2719+
match ty.sty {
2720+
ty::Array(ref component, _) => is_maybe_uninit_ty_valid(cx, component),
2721+
ty::Tuple(ref types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
2722+
ty::Adt(ref adt, _) => {
2723+
// needs to be a MaybeUninit
2724+
match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT)
2725+
},
2726+
_ => false,
2727+
}
2728+
}
2729+
26652730
fn lint_suspicious_map(cx: &LateContext<'_, '_>, expr: &hir::Expr) {
26662731
span_help_and_lint(
26672732
cx,

clippy_lints/src/utils/paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub const LINT: [&str; 3] = ["rustc", "lint", "Lint"];
4949
pub const LINT_PASS: [&str; 3] = ["rustc", "lint", "LintPass"];
5050
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
5151
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
52+
pub const MEM_MAYBEUNINIT: [&str; 4] = ["core", "mem", "maybe_uninit", "MaybeUninit"];
53+
pub const MEM_MAYBEUNINIT_UNINIT: [&str; 5] = ["core", "mem", "maybe_uninit", "MaybeUninit", "uninit"];
5254
pub const MEM_REPLACE: [&str; 3] = ["core", "mem", "replace"];
5355
pub const MUTEX: [&str; 4] = ["std", "sync", "mutex", "Mutex"];
5456
pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"];

src/lintlist/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 311] = [
9+
pub const ALL_LINTS: [Lint; 312] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1890,6 +1890,13 @@ pub const ALL_LINTS: [Lint; 311] = [
18901890
deprecation: None,
18911891
module: "panic_unimplemented",
18921892
},
1893+
Lint {
1894+
name: "uninit_assumed_init",
1895+
group: "correctness",
1896+
desc: "`MaybeUninit::uninit().assume_init()`",
1897+
deprecation: None,
1898+
module: "methods",
1899+
},
18931900
Lint {
18941901
name: "unit_arg",
18951902
group: "complexity",

tests/ui/uninit.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(stmt_expr_attributes)]
2+
3+
use std::mem::MaybeUninit;
4+
5+
#[allow(clippy::let_unit_value)]
6+
fn main() {
7+
let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
8+
9+
// edge case: For now we lint on empty arrays
10+
let _: [u8; 0] = unsafe { MaybeUninit::uninit().assume_init() };
11+
12+
// edge case: For now we accept unit tuples
13+
let _: () = unsafe { MaybeUninit::uninit().assume_init() };
14+
15+
// This is OK, because `MaybeUninit` allows uninitialized data.
16+
let _: MaybeUninit<usize> = unsafe { MaybeUninit::uninit().assume_init() };
17+
18+
// This is OK, because all constitutent types are uninit-compatible.
19+
let _: (MaybeUninit<usize>, MaybeUninit<bool>) = unsafe { MaybeUninit::uninit().assume_init() };
20+
21+
// This is OK, because all constitutent types are uninit-compatible.
22+
let _: (MaybeUninit<usize>, [MaybeUninit<bool>; 2]) = unsafe { MaybeUninit::uninit().assume_init() };
23+
}

tests/ui/uninit.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this call for this type may be undefined behavior
2+
--> $DIR/uninit.rs:7:29
3+
|
4+
LL | let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(clippy::uninit_assumed_init)]` on by default
8+
9+
error: this call for this type may be undefined behavior
10+
--> $DIR/uninit.rs:10:31
11+
|
12+
LL | let _: [u8; 0] = unsafe { MaybeUninit::uninit().assume_init() };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)