forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rust-lang#4294
- Loading branch information
Showing
6 changed files
with
167 additions
and
0 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,32 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use rustc_hir::{Expr, ExprKind, UnOp}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::RAW_ASSIGN_TO_DROP; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, lhs: &'tcx Expr<'_>) { | ||
if let ExprKind::Unary(UnOp::Deref, expr) = lhs.kind | ||
&& let ty = cx.typeck_results().expr_ty(expr) | ||
&& ty.is_unsafe_ptr() | ||
&& let Some(deref_ty) = ty.builtin_deref(true) | ||
&& deref_ty.needs_drop(cx.tcx, cx.typing_env()) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
RAW_ASSIGN_TO_DROP, | ||
expr.span, | ||
"assignment via raw pointer always executes destructor", | ||
|diag| { | ||
diag.note(format!( | ||
"the destructor defined by `{deref_ty}` is executed during assignment of the new value" | ||
)); | ||
diag.span_label( | ||
expr.span, | ||
"this place may be uninitialized, causing Undefined Behavior when the destructor executes", | ||
); | ||
diag.help("use `std::ptr::write()` to overwrite a (possibly uninitialized) place"); | ||
diag.help("use `std::ptr::drop_in_place()` to drop the previous value if such value exists"); | ||
}, | ||
); | ||
} | ||
} |
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,23 @@ | ||
#![warn(clippy::raw_assign_to_drop)] | ||
|
||
fn main() { | ||
unsafe fn foo(r: *mut String, i: *mut i32) { | ||
*r = "foo".to_owned(); | ||
|
||
// no lint on {integer} | ||
*i = 47; | ||
|
||
(*r, *r) = ("foo".to_owned(), "bar".to_owned()); | ||
|
||
(*r, *i) = ("foo".to_owned(), 47); | ||
|
||
let mut x: String = Default::default(); | ||
*(&mut x as *mut _) = "Foo".to_owned(); | ||
|
||
// no lint on `u8` | ||
*x.as_mut_ptr() = b'a'; | ||
|
||
let mut v: Vec<String> = vec![]; | ||
*v.as_mut_ptr() = Default::default(); | ||
} | ||
} |
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,64 @@ | ||
error: assignment via raw pointer always executes destructor | ||
--> tests/ui/raw_assign_to_drop.rs:5:10 | ||
| | ||
LL | *r = "foo".to_owned(); | ||
| ^ this place may be uninitialized, causing Undefined Behavior when the destructor executes | ||
| | ||
= note: the destructor defined by `std::string::String` is executed during assignment of the new value | ||
= help: use `std::ptr::write()` to overwrite a (possibly uninitialized) place | ||
= help: use `std::ptr::drop_in_place()` to drop the previous value if such value exists | ||
= note: `-D clippy::raw-assign-to-drop` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::raw_assign_to_drop)]` | ||
|
||
error: assignment via raw pointer always executes destructor | ||
--> tests/ui/raw_assign_to_drop.rs:10:11 | ||
| | ||
LL | (*r, *r) = ("foo".to_owned(), "bar".to_owned()); | ||
| ^ this place may be uninitialized, causing Undefined Behavior when the destructor executes | ||
| | ||
= note: the destructor defined by `std::string::String` is executed during assignment of the new value | ||
= help: use `std::ptr::write()` to overwrite a (possibly uninitialized) place | ||
= help: use `std::ptr::drop_in_place()` to drop the previous value if such value exists | ||
|
||
error: assignment via raw pointer always executes destructor | ||
--> tests/ui/raw_assign_to_drop.rs:10:15 | ||
| | ||
LL | (*r, *r) = ("foo".to_owned(), "bar".to_owned()); | ||
| ^ this place may be uninitialized, causing Undefined Behavior when the destructor executes | ||
| | ||
= note: the destructor defined by `std::string::String` is executed during assignment of the new value | ||
= help: use `std::ptr::write()` to overwrite a (possibly uninitialized) place | ||
= help: use `std::ptr::drop_in_place()` to drop the previous value if such value exists | ||
|
||
error: assignment via raw pointer always executes destructor | ||
--> tests/ui/raw_assign_to_drop.rs:12:11 | ||
| | ||
LL | (*r, *i) = ("foo".to_owned(), 47); | ||
| ^ this place may be uninitialized, causing Undefined Behavior when the destructor executes | ||
| | ||
= note: the destructor defined by `std::string::String` is executed during assignment of the new value | ||
= help: use `std::ptr::write()` to overwrite a (possibly uninitialized) place | ||
= help: use `std::ptr::drop_in_place()` to drop the previous value if such value exists | ||
|
||
error: assignment via raw pointer always executes destructor | ||
--> tests/ui/raw_assign_to_drop.rs:15:10 | ||
| | ||
LL | *(&mut x as *mut _) = "Foo".to_owned(); | ||
| ^^^^^^^^^^^^^^^^^^ this place may be uninitialized, causing Undefined Behavior when the destructor executes | ||
| | ||
= note: the destructor defined by `std::string::String` is executed during assignment of the new value | ||
= help: use `std::ptr::write()` to overwrite a (possibly uninitialized) place | ||
= help: use `std::ptr::drop_in_place()` to drop the previous value if such value exists | ||
|
||
error: assignment via raw pointer always executes destructor | ||
--> tests/ui/raw_assign_to_drop.rs:21:10 | ||
| | ||
LL | *v.as_mut_ptr() = Default::default(); | ||
| ^^^^^^^^^^^^^^ this place may be uninitialized, causing Undefined Behavior when the destructor executes | ||
| | ||
= note: the destructor defined by `std::string::String` is executed during assignment of the new value | ||
= help: use `std::ptr::write()` to overwrite a (possibly uninitialized) place | ||
= help: use `std::ptr::drop_in_place()` to drop the previous value if such value exists | ||
|
||
error: aborting due to 6 previous errors | ||
|