-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57397a5
commit 1ff8162
Showing
8 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_hir::intravisit::FnKind; | ||
use rustc_hir::{Body, ExprKind, FnDecl, FnRetTy, TyKind, UnOp}; | ||
use rustc_hir_analysis::hir_ty_to_ty; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// It detects references to uninhabited types, such as `!` and | ||
/// warns when those are either dereferenced or returned from a function. | ||
/// | ||
/// ### Why is this bad? | ||
/// Dereferencing a reference to an uninhabited type would create | ||
/// an instance of such a type, which cannot exist. This constitutes | ||
/// undefined behaviour. Such a reference could have been created | ||
/// by `unsafe` code. | ||
/// | ||
/// ### Example | ||
/// The following function can return a reference to an uninhabited type | ||
/// (`Infallible`) because it uses `unsafe` code to create it. However, | ||
/// the user of such a function could dereference the return value and | ||
/// trigger an undefined behaviour from safe code. | ||
/// | ||
/// ```no_run | ||
/// fn create_ref() -> &'static std::convert::Infallible { | ||
/// unsafe { std::mem::transmute(&()) } | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.76.0"] | ||
pub UNINHABITED_REFERENCE, | ||
suspicious, | ||
"reference to uninhabited type" | ||
} | ||
|
||
declare_lint_pass!(UninhabitedReference => [UNINHABITED_REFERENCE]); | ||
|
||
impl LateLintPass<'_> for UninhabitedReference { | ||
fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) { | ||
if expr.span.from_expansion() { | ||
return; | ||
} | ||
|
||
if let ExprKind::Unary(UnOp::Deref, _) = expr.kind { | ||
let ty = cx.typeck_results().expr_ty_adjusted(expr); | ||
if ty.is_privately_uninhabited(cx.tcx, cx.param_env) { | ||
span_lint( | ||
cx, | ||
UNINHABITED_REFERENCE, | ||
expr.span, | ||
"dereferencing a reference to an uninhabited type is undefined behavior", | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'_>, | ||
kind: FnKind<'_>, | ||
fndecl: &'_ FnDecl<'_>, | ||
_: &'_ Body<'_>, | ||
span: rustc_span::Span, | ||
_: rustc_span::def_id::LocalDefId, | ||
) { | ||
if span.from_expansion() || matches!(kind, FnKind::Closure) { | ||
return; | ||
} | ||
if let FnRetTy::Return(hir_ty) = fndecl.output | ||
&& let TyKind::Ref(_, mut_ty) = hir_ty.kind | ||
&& hir_ty_to_ty(cx.tcx, mut_ty.ty).is_privately_uninhabited(cx.tcx, cx.param_env) | ||
{ | ||
span_lint( | ||
cx, | ||
UNINHABITED_REFERENCE, | ||
hir_ty.span, | ||
"dereferencing a reference to an uninhabited type would be undefined behavior", | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![warn(clippy::uninhabited_reference)] | ||
#![feature(never_type)] | ||
|
||
fn ret_uninh_ref() -> &'static std::convert::Infallible { | ||
unsafe { std::mem::transmute(&()) } | ||
} | ||
|
||
fn main() { | ||
let x = ret_uninh_ref(); | ||
let _ = *x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: dereferencing a reference to an uninhabited type would be undefined behavior | ||
--> $DIR/uninhabited_reference.rs:4:23 | ||
| | ||
LL | fn ret_uninh_ref() -> &'static std::convert::Infallible { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::uninhabited-reference` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::uninhabited_reference)]` | ||
|
||
error: dereferencing a reference to an uninhabited type is undefined behavior | ||
--> $DIR/uninhabited_reference.rs:10:13 | ||
| | ||
LL | let _ = *x; | ||
| ^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|