-
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.
Update mod.rs Update mod.rs
- Loading branch information
Showing
16 changed files
with
155 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_hir::{BorrowKind, Expr, ExprKind, Ty, TyKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::adjustment::Adjust; | ||
|
||
use super::PTR_TO_TEMPORARY; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
cast_expr: &'tcx Expr<'_>, | ||
cast_to: &'tcx Ty<'_>, | ||
) { | ||
if matches!(cast_to.kind, TyKind::Ptr(_)) | ||
&& let ExprKind::AddrOf(BorrowKind::Ref, _, e) = cast_expr.kind | ||
{ | ||
// rustc's criteria of a "temporary value", so this should be 100% accurate | ||
if !e.is_place_expr(|base| { | ||
cx.typeck_results() | ||
.adjustments() | ||
.get(base.hir_id) | ||
.is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_)))) | ||
}) { | ||
span_lint_and_help( | ||
cx, | ||
PTR_TO_TEMPORARY, | ||
expr.span, | ||
"raw pointer to a temporary value", | ||
None, | ||
"this causes Undefined Behavior; create a local binding to make it longer lived", | ||
); | ||
} | ||
} | ||
} |
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,34 @@ | ||
#![allow(clippy::explicit_auto_deref, clippy::unnecessary_cast, clippy::useless_vec)] | ||
|
||
use std::ptr::addr_of; | ||
|
||
fn a() -> i32 { | ||
0 | ||
} | ||
|
||
struct Vec3 { | ||
x: f32, | ||
y: f32, | ||
z: f32, | ||
} | ||
|
||
fn main() { | ||
let _p = &0 as *const i32; | ||
let _p = &a() as *const i32; | ||
let vec = vec![1]; | ||
let _p = &vec.len() as *const usize; | ||
let x = &(1 + 2) as *const i32; | ||
let x = &(x as *const i32) as *const *const i32; | ||
|
||
// Do not lint... | ||
let ptr = &Vec3 { x: 1.0, y: 2.0, z: 3.0 }; | ||
let some_variable = 1i32; | ||
let x = &(*ptr).x as *const f32; | ||
let x = &(some_variable) as *const i32; | ||
|
||
// ...As `addr_of!` does not report anything out of the ordinary | ||
let ptr = &Vec3 { x: 1.0, y: 2.0, z: 3.0 }; | ||
let some_variable = 1i32; | ||
let x = addr_of!((*ptr).x) as *const f32; | ||
let x = addr_of!(some_variable); | ||
} |
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,43 @@ | ||
error: raw pointer to a temporary value | ||
--> $DIR/ptr_to_temporary.rs:16:14 | ||
| | ||
LL | let _p = &0 as *const i32; | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: this causes Undefined Behavior; create a local binding to make it longer lived | ||
= note: `#[deny(clippy::ptr_to_temporary)]` on by default | ||
|
||
error: raw pointer to a temporary value | ||
--> $DIR/ptr_to_temporary.rs:17:14 | ||
| | ||
LL | let _p = &a() as *const i32; | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: this causes Undefined Behavior; create a local binding to make it longer lived | ||
|
||
error: raw pointer to a temporary value | ||
--> $DIR/ptr_to_temporary.rs:19:14 | ||
| | ||
LL | let _p = &vec.len() as *const usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: this causes Undefined Behavior; create a local binding to make it longer lived | ||
|
||
error: raw pointer to a temporary value | ||
--> $DIR/ptr_to_temporary.rs:20:13 | ||
| | ||
LL | let x = &(1 + 2) as *const i32; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: this causes Undefined Behavior; create a local binding to make it longer lived | ||
|
||
error: raw pointer to a temporary value | ||
--> $DIR/ptr_to_temporary.rs:21:13 | ||
| | ||
LL | let x = &(x as *const i32) as *const *const i32; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: this causes Undefined Behavior; create a local binding to make it longer lived | ||
|
||
error: aborting due to 5 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
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