Skip to content

Commit d630546

Browse files
committed
new lint: uninhabited_reference
1 parent ee83760 commit d630546

8 files changed

+117
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5583,6 +5583,7 @@ Released 2018-09-13
55835583
[`undropped_manually_drops`]: https://rust-lang.github.io/rust-clippy/master/index.html#undropped_manually_drops
55845584
[`unicode_not_nfc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unicode_not_nfc
55855585
[`unimplemented`]: https://rust-lang.github.io/rust-clippy/master/index.html#unimplemented
5586+
[`uninhabited_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#uninhabited_reference
55865587
[`uninit_assumed_init`]: https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init
55875588
[`uninit_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#uninit_vec
55885589
[`uninlined_format_args`]: https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
679679
crate::unicode::INVISIBLE_CHARACTERS_INFO,
680680
crate::unicode::NON_ASCII_LITERAL_INFO,
681681
crate::unicode::UNICODE_NOT_NFC_INFO,
682+
crate::uninhabited_reference::UNINHABITED_REFERENCE_INFO,
682683
crate::uninit_vec::UNINIT_VEC_INFO,
683684
crate::unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD_INFO,
684685
crate::unit_types::LET_UNIT_VALUE_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ mod tuple_array_conversions;
326326
mod types;
327327
mod undocumented_unsafe_blocks;
328328
mod unicode;
329+
mod uninhabited_reference;
329330
mod uninit_vec;
330331
mod unit_return_expecting_ord;
331332
mod unit_types;
@@ -1071,6 +1072,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
10711072
store.register_late_pass(|_| Box::new(iter_over_hash_type::IterOverHashType));
10721073
store.register_late_pass(|_| Box::new(impl_hash_with_borrow_str_and_bytes::ImplHashWithBorrowStrBytes));
10731074
store.register_late_pass(|_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity));
1075+
store.register_late_pass(|_| Box::new(uninhabited_reference::UninhabitedReference));
10741076
// add lints here, do not remove this comment, it's used in `new_lint`
10751077
}
10761078

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use rustc_hir::intravisit::FnKind;
3+
use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnRetTy, TyKind, UnOp};
4+
use rustc_hir_analysis::hir_ty_to_ty;
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::declare_lint_pass;
7+
use rustc_span::def_id::LocalDefId;
8+
use rustc_span::Span;
9+
10+
declare_clippy_lint! {
11+
/// ### What it does
12+
/// It detects references to uninhabited types, such as `!` and
13+
/// warns when those are either dereferenced or returned from a function.
14+
///
15+
/// ### Why is this bad?
16+
/// Dereferencing a reference to an uninhabited type would create
17+
/// an instance of such a type, which cannot exist. This constitutes
18+
/// undefined behaviour. Such a reference could have been created
19+
/// by `unsafe` code.
20+
///
21+
/// ### Example
22+
/// The following function can return a reference to an uninhabited type
23+
/// (`Infallible`) because it uses `unsafe` code to create it. However,
24+
/// the user of such a function could dereference the return value and
25+
/// trigger an undefined behavior from safe code.
26+
///
27+
/// ```no_run
28+
/// fn create_ref() -> &'static std::convert::Infallible {
29+
/// unsafe { std::mem::transmute(&()) }
30+
/// }
31+
/// ```
32+
#[clippy::version = "1.76.0"]
33+
pub UNINHABITED_REFERENCE,
34+
suspicious,
35+
"reference to uninhabited type"
36+
}
37+
38+
declare_lint_pass!(UninhabitedReference => [UNINHABITED_REFERENCE]);
39+
40+
impl LateLintPass<'_> for UninhabitedReference {
41+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
42+
if expr.span.from_expansion() {
43+
return;
44+
}
45+
46+
if let ExprKind::Unary(UnOp::Deref, _) = expr.kind {
47+
let ty = cx.typeck_results().expr_ty_adjusted(expr);
48+
if ty.is_privately_uninhabited(cx.tcx, cx.param_env) {
49+
span_lint(
50+
cx,
51+
UNINHABITED_REFERENCE,
52+
expr.span,
53+
"dereferencing a reference to an uninhabited type is undefined behavior",
54+
);
55+
}
56+
}
57+
}
58+
59+
fn check_fn(
60+
&mut self,
61+
cx: &LateContext<'_>,
62+
kind: FnKind<'_>,
63+
fndecl: &'_ FnDecl<'_>,
64+
_: &'_ Body<'_>,
65+
span: Span,
66+
_: LocalDefId,
67+
) {
68+
if span.from_expansion() || matches!(kind, FnKind::Closure) {
69+
return;
70+
}
71+
if let FnRetTy::Return(hir_ty) = fndecl.output
72+
&& let TyKind::Ref(_, mut_ty) = hir_ty.kind
73+
&& hir_ty_to_ty(cx.tcx, mut_ty.ty).is_privately_uninhabited(cx.tcx, cx.param_env)
74+
{
75+
span_lint(
76+
cx,
77+
UNINHABITED_REFERENCE,
78+
hir_ty.span,
79+
"dereferencing a reference to an uninhabited type would be undefined behavior",
80+
);
81+
}
82+
}
83+
}

tests/ui/infallible_destructuring_match.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(exhaustive_patterns, never_type)]
22
#![allow(dead_code, unreachable_code, unused_variables)]
3-
#![allow(clippy::let_and_return)]
3+
#![allow(clippy::let_and_return, clippy::uninhabited_reference)]
44

55
enum SingleVariantEnum {
66
Variant(i32),

tests/ui/infallible_destructuring_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(exhaustive_patterns, never_type)]
22
#![allow(dead_code, unreachable_code, unused_variables)]
3-
#![allow(clippy::let_and_return)]
3+
#![allow(clippy::let_and_return, clippy::uninhabited_reference)]
44

55
enum SingleVariantEnum {
66
Variant(i32),

tests/ui/uninhabited_reference.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(clippy::uninhabited_reference)]
2+
#![feature(never_type)]
3+
4+
fn ret_uninh_ref() -> &'static std::convert::Infallible {
5+
unsafe { std::mem::transmute(&()) }
6+
}
7+
8+
fn main() {
9+
let x = ret_uninh_ref();
10+
let _ = *x;
11+
}

tests/ui/uninhabited_reference.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: dereferencing a reference to an uninhabited type would be undefined behavior
2+
--> $DIR/uninhabited_reference.rs:4:23
3+
|
4+
LL | fn ret_uninh_ref() -> &'static std::convert::Infallible {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::uninhabited-reference` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::uninhabited_reference)]`
9+
10+
error: dereferencing a reference to an uninhabited type is undefined behavior
11+
--> $DIR/uninhabited_reference.rs:10:13
12+
|
13+
LL | let _ = *x;
14+
| ^^
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)