|
| 1 | +use clippy_utils::ty::ty_cannot_be_named; |
1 | 2 | use rustc_errors::Applicability; |
2 | 3 | use rustc_lint::LateContext; |
3 | | -use rustc_middle::ty::Ty; |
| 4 | +use rustc_middle::ty::{self, Ty}; |
4 | 5 |
|
5 | 6 | pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) { |
6 | 7 | if let rustc_hir::TyKind::Ptr(rustc_hir::MutTy { ty, .. }) = cast_to_hir.kind |
7 | 8 | && matches!(ty.kind, rustc_hir::TyKind::Infer(())) |
| 9 | + && let ty::RawPtr(pointee, _) = ty_into.kind() |
8 | 10 | { |
9 | | - clippy_utils::diagnostics::span_lint_and_sugg( |
10 | | - cx, |
11 | | - super::AS_POINTER_UNDERSCORE, |
12 | | - cast_to_hir.span, |
13 | | - "using inferred pointer cast", |
14 | | - "use explicit type", |
15 | | - ty_into.to_string(), |
16 | | - Applicability::MachineApplicable, |
17 | | - ); |
| 11 | + if ty_cannot_be_named(*pointee) { |
| 12 | + clippy_utils::diagnostics::span_lint_and_help( |
| 13 | + cx, |
| 14 | + super::AS_POINTER_UNDERSCORE, |
| 15 | + cast_to_hir.span, |
| 16 | + format!("using inferred pointer cast to unnameable type: `{ty_into}`"), |
| 17 | + None, |
| 18 | + "use explicit type", |
| 19 | + ); |
| 20 | + } else { |
| 21 | + clippy_utils::diagnostics::span_lint_and_sugg( |
| 22 | + cx, |
| 23 | + super::AS_POINTER_UNDERSCORE, |
| 24 | + cast_to_hir.span, |
| 25 | + "using inferred pointer cast", |
| 26 | + "use explicit type", |
| 27 | + ty_into.to_string(), |
| 28 | + Applicability::MachineApplicable, |
| 29 | + ); |
| 30 | + }; |
18 | 31 | } |
19 | 32 | } |
0 commit comments