-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add manual_dangling_ptr
lint
#14107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add manual_dangling_ptr
lint
#14107
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,82 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::SpanRangeExt; | ||
use clippy_utils::ty::is_normalizable; | ||
use clippy_utils::{expr_or_init, match_def_path, path_def_id, paths, std_or_core}; | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, Ty, TyKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Spanned; | ||
|
||
use super::MANUAL_DANGLING_PTR; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) { | ||
if let TyKind::Ptr(ref ptr_ty) = to.kind { | ||
let init_expr = expr_or_init(cx, from); | ||
if is_expr_const_aligned(cx, init_expr, ptr_ty.ty) | ||
&& let Some(std_or_core) = std_or_core(cx) | ||
{ | ||
let sugg_fn = match ptr_ty.mutbl { | ||
Mutability::Not => "ptr::dangling", | ||
Mutability::Mut => "ptr::dangling_mut", | ||
}; | ||
|
||
let sugg = if let TyKind::Infer(()) = ptr_ty.ty.kind { | ||
format!("{std_or_core}::{sugg_fn}()") | ||
} else if let Some(mut_ty_snip) = ptr_ty.ty.span.get_source_text(cx) { | ||
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()") | ||
} else { | ||
return; | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_DANGLING_PTR, | ||
expr.span, | ||
"manual creation of a dangling pointer", | ||
"use", | ||
sugg, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
|
||
// Checks if the given expression is a call to `align_of` whose generic argument matches the target | ||
// type, or a positive constant literal that matches the target type's alignment. | ||
fn is_expr_const_aligned(cx: &LateContext<'_>, expr: &Expr<'_>, to: &Ty<'_>) -> bool { | ||
match expr.kind { | ||
ExprKind::Call(fun, _) => is_align_of_call(cx, fun, to), | ||
ExprKind::Lit(lit) => is_literal_aligned(cx, lit, to), | ||
_ => false, | ||
} | ||
} | ||
|
||
fn is_align_of_call(cx: &LateContext<'_>, fun: &Expr<'_>, to: &Ty<'_>) -> bool { | ||
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind | ||
&& let Some(fun_id) = path_def_id(cx, fun) | ||
&& match_def_path(cx, fun_id, &paths::ALIGN_OF) | ||
&& let Some(args) = path.segments.last().and_then(|seg| seg.args) | ||
&& let [GenericArg::Type(generic_ty)] = args.args | ||
{ | ||
let typeck = cx.typeck_results(); | ||
return typeck.node_type(generic_ty.hir_id) == typeck.node_type(to.hir_id); | ||
} | ||
false | ||
} | ||
|
||
fn is_literal_aligned(cx: &LateContext<'_>, lit: &Spanned<LitKind>, to: &Ty<'_>) -> bool { | ||
let LitKind::Int(val, _) = lit.node else { return false }; | ||
if val == 0 { | ||
return false; | ||
} | ||
let to_mid_ty = cx.typeck_results().node_type(to.hir_id); | ||
is_normalizable(cx, cx.param_env, to_mid_ty) | ||
&& cx | ||
.tcx | ||
.layout_of(cx.typing_env().as_query_input(to_mid_ty)) | ||
.is_ok_and(|layout| { | ||
let align = u128::from(layout.align.abi.bytes()); | ||
u128::from(val) <= align | ||
}) | ||
} | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
#![warn(clippy::manual_dangling_ptr)] | ||
use std::mem; | ||
|
||
pub fn foo(_const: *const f32, _mut: *mut i32) {} | ||
|
||
fn main() { | ||
let _: *const u8 = std::ptr::dangling(); | ||
//~^ manual_dangling_ptr | ||
let _ = std::ptr::dangling::<u32>(); | ||
//~^ manual_dangling_ptr | ||
let _ = std::ptr::dangling_mut::<f32>(); | ||
//~^ manual_dangling_ptr | ||
|
||
let _ = std::ptr::dangling::<u8>(); | ||
//~^ manual_dangling_ptr | ||
let _ = std::ptr::dangling::<u32>(); | ||
//~^ manual_dangling_ptr | ||
let _ = std::ptr::dangling::<usize>(); | ||
//~^ manual_dangling_ptr | ||
|
||
foo(std::ptr::dangling(), std::ptr::dangling_mut()); | ||
//~^ manual_dangling_ptr | ||
//~| manual_dangling_ptr | ||
} | ||
|
||
fn should_not_lint() { | ||
let _ = 0x10 as *mut i32; | ||
let _ = mem::align_of::<u32>() as *const u8; | ||
|
||
foo(0 as _, 0 as _); | ||
} | ||
|
||
#[clippy::msrv = "1.83"] | ||
fn _msrv_1_83() { | ||
// `{core, std}::ptr::dangling` was stabilized in 1.84. Do not lint this | ||
foo(4 as *const _, 4 as *mut _); | ||
} | ||
|
||
#[clippy::msrv = "1.84"] | ||
fn _msrv_1_84() { | ||
foo(std::ptr::dangling(), std::ptr::dangling_mut()); | ||
//~^ manual_dangling_ptr | ||
//~| manual_dangling_ptr | ||
} |
This file contains hidden or 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,44 @@ | ||
#![warn(clippy::manual_dangling_ptr)] | ||
use std::mem; | ||
|
||
pub fn foo(_const: *const f32, _mut: *mut i32) {} | ||
Comment on lines
+1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Jarcho I have modified the tests to use at most 32-bit types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add the PR to the merge queue? |
||
|
||
fn main() { | ||
let _: *const u8 = 1 as *const _; | ||
//~^ manual_dangling_ptr | ||
let _ = 2 as *const u32; | ||
//~^ manual_dangling_ptr | ||
let _ = 4 as *mut f32; | ||
//~^ manual_dangling_ptr | ||
|
||
let _ = mem::align_of::<u8>() as *const u8; | ||
//~^ manual_dangling_ptr | ||
let _ = mem::align_of::<u32>() as *const u32; | ||
//~^ manual_dangling_ptr | ||
let _ = mem::align_of::<usize>() as *const usize; | ||
//~^ manual_dangling_ptr | ||
|
||
foo(4 as *const _, 4 as *mut _); | ||
//~^ manual_dangling_ptr | ||
//~| manual_dangling_ptr | ||
} | ||
|
||
fn should_not_lint() { | ||
let _ = 0x10 as *mut i32; | ||
let _ = mem::align_of::<u32>() as *const u8; | ||
|
||
foo(0 as _, 0 as _); | ||
} | ||
|
||
#[clippy::msrv = "1.83"] | ||
fn _msrv_1_83() { | ||
// `{core, std}::ptr::dangling` was stabilized in 1.84. Do not lint this | ||
foo(4 as *const _, 4 as *mut _); | ||
} | ||
|
||
#[clippy::msrv = "1.84"] | ||
fn _msrv_1_84() { | ||
foo(4 as *const _, 4 as *mut _); | ||
//~^ manual_dangling_ptr | ||
//~| manual_dangling_ptr | ||
} |
This file contains hidden or 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 @@ | ||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:7:24 | ||
| | ||
LL | let _: *const u8 = 1 as *const _; | ||
| ^^^^^^^^^^^^^ help: use: `std::ptr::dangling()` | ||
| | ||
= note: `-D clippy::manual-dangling-ptr` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_dangling_ptr)]` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:9:13 | ||
| | ||
LL | let _ = 2 as *const u32; | ||
| ^^^^^^^^^^^^^^^ help: use: `std::ptr::dangling::<u32>()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:11:13 | ||
| | ||
LL | let _ = 4 as *mut f32; | ||
| ^^^^^^^^^^^^^ help: use: `std::ptr::dangling_mut::<f32>()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:14:13 | ||
| | ||
LL | let _ = mem::align_of::<u8>() as *const u8; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `std::ptr::dangling::<u8>()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:16:13 | ||
| | ||
LL | let _ = mem::align_of::<u32>() as *const u32; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `std::ptr::dangling::<u32>()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:18:13 | ||
| | ||
LL | let _ = mem::align_of::<usize>() as *const usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `std::ptr::dangling::<usize>()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:21:9 | ||
| | ||
LL | foo(4 as *const _, 4 as *mut _); | ||
| ^^^^^^^^^^^^^ help: use: `std::ptr::dangling()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:21:24 | ||
| | ||
LL | foo(4 as *const _, 4 as *mut _); | ||
| ^^^^^^^^^^^ help: use: `std::ptr::dangling_mut()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:41:9 | ||
| | ||
LL | foo(4 as *const _, 4 as *mut _); | ||
| ^^^^^^^^^^^^^ help: use: `std::ptr::dangling()` | ||
|
||
error: manual creation of a dangling pointer | ||
--> tests/ui/manual_dangling_ptr.rs:41:24 | ||
| | ||
LL | foo(4 as *const _, 4 as *mut _); | ||
| ^^^^^^^^^^^ help: use: `std::ptr::dangling_mut()` | ||
|
||
error: aborting due to 10 previous errors | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.