-
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.
Add lint for publically constructable
Unsafe
types`
- Loading branch information
Showing
9 changed files
with
135 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_hir::{Item, ItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// Detects types with `Unsafe` in the name that are publically constructable. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// `Unsafe` in the name of a type implies that there is some kind of safety invariant | ||
/// being held by constructing said type, however, this invariant may not be checked | ||
/// if a user can safely publically construct it. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// pub struct UnsafeToken {} | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// pub struct UnsafeToken { | ||
/// _private: () | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.84.0"] | ||
pub CONSTRUCTABLE_UNSAFE_TYPE, | ||
suspicious, | ||
"`Unsafe` types that are publically constructable" | ||
} | ||
|
||
declare_lint_pass!(ConstructableUnsafeType => [CONSTRUCTABLE_UNSAFE_TYPE]); | ||
|
||
impl LateLintPass<'_> for ConstructableUnsafeType { | ||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { | ||
if let ItemKind::Struct(variant, generics) = item.kind | ||
&& { | ||
// If the type contains `Unsafe`, but is not exactly. | ||
let name = item.ident.as_str(); | ||
name.contains("Unsafe") && name.len() != "Unsafe".len() | ||
} | ||
&& generics.params.is_empty() | ||
&& cx.effective_visibilities.is_reachable(item.owner_id.def_id) | ||
&& variant | ||
.fields() | ||
.iter() | ||
.all(|f| cx.effective_visibilities.is_exported(f.def_id)) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
CONSTRUCTABLE_UNSAFE_TYPE, | ||
item.span, | ||
"`Unsafe` type is publically constructable", | ||
None, | ||
"give this type a private field, or make it private", | ||
); | ||
} | ||
} | ||
} |
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,19 @@ | ||
#![warn(clippy::constructable_unsafe_type)] | ||
|
||
struct PrivateUnsafeToken; | ||
pub struct GoodUnsafeToken { | ||
_private: (), | ||
} | ||
|
||
pub struct DangerousUnsafeToken1; | ||
//~^ error: `Unsafe` type is publically constructable | ||
pub struct DangerousUnsafeToken2(); | ||
//~^ error: `Unsafe` type is publically constructable | ||
pub struct DangerousUnsafeToken3 {} | ||
//~^ error: `Unsafe` type is publically constructable | ||
pub struct DangerousUnsafeToken4 { | ||
//~^ error: `Unsafe` type is publically constructable | ||
pub public: (), | ||
} | ||
|
||
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,39 @@ | ||
error: `Unsafe` type is publically constructable | ||
--> tests/ui/constructable_unsafe_type.rs:8:1 | ||
| | ||
LL | pub struct DangerousUnsafeToken1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: give this type a private field, or make it private | ||
= note: `-D clippy::constructable-unsafe-type` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::constructable_unsafe_type)]` | ||
|
||
error: `Unsafe` type is publically constructable | ||
--> tests/ui/constructable_unsafe_type.rs:10:1 | ||
| | ||
LL | pub struct DangerousUnsafeToken2(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: give this type a private field, or make it private | ||
|
||
error: `Unsafe` type is publically constructable | ||
--> tests/ui/constructable_unsafe_type.rs:12:1 | ||
| | ||
LL | pub struct DangerousUnsafeToken3 {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: give this type a private field, or make it private | ||
|
||
error: `Unsafe` type is publically constructable | ||
--> tests/ui/constructable_unsafe_type.rs:14:1 | ||
| | ||
LL | / pub struct DangerousUnsafeToken4 { | ||
LL | | | ||
LL | | pub public: (), | ||
LL | | } | ||
| |_^ | ||
| | ||
= help: give this type a private field, or make it private | ||
|
||
error: aborting due to 4 previous errors | ||
|
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