Skip to content

Commit ebfe969

Browse files
committed
Parent of struct constructor is the struct itself
Also, the `Fn`/`AssocFn` cases were not tested anywhere, and according to the comments inside `TyCtxt::res_generic_def_id()` could have made the `TyCtxt::generics_of()` call also trigger an ICE.
1 parent 4d4789c commit ebfe969

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

clippy_lints/src/casts/needless_type_cast.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,7 @@ fn is_generic_res(cx: &LateContext<'_>, res: Res) -> bool {
182182
.iter()
183183
.any(|p| p.kind.is_ty_or_const())
184184
};
185-
match res {
186-
Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => has_type_params(def_id),
187-
// Ctor → Variant → ADT: constructor's parent is variant, variant's parent is the ADT
188-
Res::Def(DefKind::Ctor(..), def_id) => has_type_params(cx.tcx.parent(cx.tcx.parent(def_id))),
189-
_ => false,
190-
}
185+
cx.tcx.res_generics_def_id(res).is_some_and(has_type_params)
191186
}
192187

193188
fn is_cast_in_generic_context<'a>(cx: &LateContext<'a>, cast_expr: &Expr<'a>) -> bool {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@no-rustfix
2+
#![warn(clippy::needless_type_cast)]
3+
4+
struct Foo(*mut core::ffi::c_void);
5+
6+
enum Bar {
7+
Variant(*mut core::ffi::c_void),
8+
}
9+
10+
// Suggestions will not compile directly, as `123` is a literal which
11+
// is not compatible with the suggested `*mut core::ffi::c_void` type
12+
fn issue_16243() {
13+
let underlying: isize = 123;
14+
//~^ needless_type_cast
15+
let handle: Foo = Foo(underlying as _);
16+
17+
let underlying: isize = 123;
18+
//~^ needless_type_cast
19+
let handle: Bar = Bar::Variant(underlying as _);
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: this binding is defined as `isize` but is always cast to `*mut std::ffi::c_void`
2+
--> tests/ui/needless_type_cast_unfixable.rs:13:21
3+
|
4+
LL | let underlying: isize = 123;
5+
| ^^^^^ help: consider defining it as: `*mut std::ffi::c_void`
6+
|
7+
= note: `-D clippy::needless-type-cast` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::needless_type_cast)]`
9+
10+
error: this binding is defined as `isize` but is always cast to `*mut std::ffi::c_void`
11+
--> tests/ui/needless_type_cast_unfixable.rs:17:21
12+
|
13+
LL | let underlying: isize = 123;
14+
| ^^^^^ help: consider defining it as: `*mut std::ffi::c_void`
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)