-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5493 - ebroto:unsafe_derive_deserialize, r=flip1995
Implement unsafe_derive_deserialize lint Added `unsafe_derive_deserialize` lint to check for cases when automatically deriving `serde::Deserialize` can be problematic, i.e. with types that have methods using `unsafe`. Closes #5471 changelog: Add lint [`unsafe_derive_deserialize`]
- Loading branch information
Showing
9 changed files
with
246 additions
and
6 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
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,60 @@ | ||
#![warn(clippy::unsafe_derive_deserialize)] | ||
#![allow(unused, clippy::missing_safety_doc)] | ||
|
||
extern crate serde; | ||
|
||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub struct A {} | ||
impl A { | ||
pub unsafe fn new(_a: i32, _b: i32) -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct B {} | ||
impl B { | ||
pub unsafe fn unsafe_method(&self) {} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct C {} | ||
impl C { | ||
pub fn unsafe_block(&self) { | ||
unsafe {} | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct D {} | ||
impl D { | ||
pub fn inner_unsafe_fn(&self) { | ||
unsafe fn inner() {} | ||
} | ||
} | ||
|
||
// Does not derive `Deserialize`, should be ignored | ||
pub struct E {} | ||
impl E { | ||
pub unsafe fn new(_a: i32, _b: i32) -> Self { | ||
Self {} | ||
} | ||
|
||
pub unsafe fn unsafe_method(&self) {} | ||
|
||
pub fn unsafe_block(&self) { | ||
unsafe {} | ||
} | ||
|
||
pub fn inner_unsafe_fn(&self) { | ||
unsafe fn inner() {} | ||
} | ||
} | ||
|
||
// Does not have methods using `unsafe`, should be ignored | ||
#[derive(Deserialize)] | ||
pub struct F {} | ||
|
||
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: you are deriving `serde::Deserialize` on a type that has methods using `unsafe` | ||
--> $DIR/unsafe_derive_deserialize.rs:8:10 | ||
| | ||
LL | #[derive(Deserialize)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::unsafe-derive-deserialize` implied by `-D warnings` | ||
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe` | ||
--> $DIR/unsafe_derive_deserialize.rs:16:10 | ||
| | ||
LL | #[derive(Deserialize)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe` | ||
--> $DIR/unsafe_derive_deserialize.rs:22:10 | ||
| | ||
LL | #[derive(Deserialize)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe` | ||
--> $DIR/unsafe_derive_deserialize.rs:30:10 | ||
| | ||
LL | #[derive(Deserialize)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html | ||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 4 previous errors | ||
|