-
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
7af811b
commit d805db1
Showing
9 changed files
with
138 additions
and
3 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,65 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_hir::intravisit::FnKind; | ||
use rustc_hir::{Body, FnDecl, FnRetTy, TyKind}; | ||
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 `!`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Instances of uninhabited types cannot be created. Creating | ||
/// a reference on such a type (which can be done in `unsafe` code) | ||
/// would create a risk of dereferencing it, which would be | ||
/// undefined behavior. | ||
/// | ||
/// ### Example | ||
/// The following function could dereference its parameter `x` without | ||
/// needing `unsafe`. This would be undefined behavior as no instance of | ||
/// type `std::convert::Infallible` can ever exist. | ||
/// ```no_run | ||
/// fn f(x: &std::convert::Infallible) { todo!() } | ||
/// ``` | ||
#[clippy::version = "1.76.0"] | ||
pub UNINHABITED_REFERENCE, | ||
suspicious, | ||
"reference on uninhabited type" | ||
} | ||
|
||
declare_lint_pass!(UninhabitedReference => [UNINHABITED_REFERENCE]); | ||
|
||
// This lint checks function parameters and return: this is where references to uninhabited types | ||
// are most susceptible to be exchanged between safe and unsafe parts of the program. | ||
impl LateLintPass<'_> for UninhabitedReference { | ||
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; | ||
} | ||
let ret_ty = match fndecl.output { | ||
FnRetTy::Return(ty) => Some(ty), | ||
FnRetTy::DefaultReturn(_) => None, | ||
}; | ||
for hir_ty in fndecl.inputs.iter().chain(ret_ty) { | ||
if 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
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,31 @@ | ||
#![warn(clippy::uninhabited_reference)] | ||
#![allow(unused, clippy::diverging_sub_expression, clippy::trivially_copy_pass_by_ref)] | ||
#![feature(never_type)] | ||
|
||
fn f1(x: &!) { | ||
todo!() | ||
} | ||
|
||
fn f2(x: &mut !) { | ||
todo!() | ||
} | ||
|
||
fn g(x: &std::convert::Infallible) { | ||
todo!() | ||
} | ||
|
||
struct S { | ||
a: std::convert::Infallible, | ||
b: u32, | ||
} | ||
|
||
fn h(x: &S) { | ||
todo!() | ||
} | ||
|
||
fn i() -> &'static ! { | ||
let x: &! = panic!(); | ||
x | ||
} | ||
|
||
fn main() {} |
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,35 @@ | ||
error: dereferencing a reference to an uninhabited type would be undefined behavior | ||
--> $DIR/uninhabited_reference.rs:5:10 | ||
| | ||
LL | fn f1(x: &!) { | ||
| ^^ | ||
| | ||
= 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 would be undefined behavior | ||
--> $DIR/uninhabited_reference.rs:9:10 | ||
| | ||
LL | fn f2(x: &mut !) { | ||
| ^^^^^^ | ||
|
||
error: dereferencing a reference to an uninhabited type would be undefined behavior | ||
--> $DIR/uninhabited_reference.rs:13:9 | ||
| | ||
LL | fn g(x: &std::convert::Infallible) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: dereferencing a reference to an uninhabited type would be undefined behavior | ||
--> $DIR/uninhabited_reference.rs:22:9 | ||
| | ||
LL | fn h(x: &S) { | ||
| ^^ | ||
|
||
error: dereferencing a reference to an uninhabited type would be undefined behavior | ||
--> $DIR/uninhabited_reference.rs:26:11 | ||
| | ||
LL | fn i() -> &'static ! { | ||
| ^^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|