Skip to content

Commit f9f854f

Browse files
committed
Auto merge of #12617 - y21:issue-12616, r=Alexendoo
avoid an ICE in `ptr_as_ptr` when getting the def_id of a local Fixes #12616 `Res::def_id` can panic, so avoid calling it in favor of `opt_def_id`, so we can gracefully handle resolutions that don't have a `DefId` (e.g. local variables) and get a false negative in the worst case, rather than an ICE changelog: Fix ICE in [`ptr_as_ptr`] when the cast expression is a function call to a local variable
2 parents 3b1b654 + e575f05 commit f9f854f

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

clippy_lints/src/casts/ptr_as_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {
6262
// we omit following `cast`:
6363
let omit_cast = if let ExprKind::Call(func, []) = cast_expr.kind
6464
&& let ExprKind::Path(ref qpath @ QPath::Resolved(None, path)) = func.kind
65+
&& let Some(method_defid) = path.res.opt_def_id()
6566
{
66-
let method_defid = path.res.def_id();
6767
if cx.tcx.is_diagnostic_item(sym::ptr_null, method_defid) {
6868
OmitFollowedCastReason::Null(qpath)
6969
} else if cx.tcx.is_diagnostic_item(sym::ptr_null_mut, method_defid) {

tests/ui/crashes/ice-12616.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![warn(clippy::ptr_as_ptr)]
2+
#![allow(clippy::unnecessary_operation, clippy::unnecessary_cast)]
3+
4+
fn main() {
5+
let s = std::ptr::null::<()>;
6+
s().cast::<()>();
7+
}

tests/ui/crashes/ice-12616.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![warn(clippy::ptr_as_ptr)]
2+
#![allow(clippy::unnecessary_operation, clippy::unnecessary_cast)]
3+
4+
fn main() {
5+
let s = std::ptr::null::<()>;
6+
s() as *const ();
7+
}

tests/ui/crashes/ice-12616.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: `as` casting between raw pointers without changing its mutability
2+
--> tests/ui/crashes/ice-12616.rs:6:5
3+
|
4+
LL | s() as *const ();
5+
| ^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `s().cast::<()>()`
6+
|
7+
= note: `-D clippy::ptr-as-ptr` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::ptr_as_ptr)]`
9+
10+
error: `as` casting between raw pointers without changing its mutability
11+
--> tests/ui/crashes/ice-12616.rs:6:5
12+
|
13+
LL | s() as *const ();
14+
| ^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `s().cast::<()>()`
15+
|
16+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)