|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::Expr; |
| 5 | +use rustc_lint::LateContext; |
| 6 | +use rustc_middle::ty::{self, GenericArg, Ty}; |
| 7 | +use rustc_span::def_id::DefId; |
| 8 | +use rustc_span::{Symbol, sym}; |
| 9 | + |
| 10 | +use super::CONFUSING_METHOD_TO_NUMERIC_CAST; |
| 11 | + |
| 12 | +fn get_primitive_ty_name(ty: Ty<'_>) -> Option<&'static str> { |
| 13 | + match ty.kind() { |
| 14 | + ty::Char => Some("char"), |
| 15 | + ty::Int(int) => Some(int.name_str()), |
| 16 | + ty::Uint(uint) => Some(uint.name_str()), |
| 17 | + ty::Float(float) => Some(float.name_str()), |
| 18 | + _ => None, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn get_const_name_and_ty_name( |
| 23 | + cx: &LateContext<'_>, |
| 24 | + method_name: Symbol, |
| 25 | + method_def_id: DefId, |
| 26 | + generics: &[GenericArg<'_>], |
| 27 | +) -> Option<(&'static str, &'static str)> { |
| 28 | + let method_name = method_name.as_str(); |
| 29 | + let diagnostic_name = cx.tcx.get_diagnostic_name(method_def_id); |
| 30 | + |
| 31 | + let ty_name = if diagnostic_name.is_some_and(|diag| diag == sym::cmp_ord_min || diag == sym::cmp_ord_max) { |
| 32 | + // We get the type on which the `min`/`max` method of the `Ord` trait is implemented. |
| 33 | + if let [ty] = generics |
| 34 | + && let Some(ty) = ty.as_type() |
| 35 | + { |
| 36 | + get_primitive_ty_name(ty)? |
| 37 | + } else { |
| 38 | + return None; |
| 39 | + } |
| 40 | + } else if let Some(impl_id) = cx.tcx.impl_of_method(method_def_id) |
| 41 | + && let Some(ty_name) = get_primitive_ty_name(cx.tcx.type_of(impl_id).instantiate_identity()) |
| 42 | + && ["min", "max", "minimum", "maximum", "min_value", "max_value"].contains(&method_name) |
| 43 | + { |
| 44 | + ty_name |
| 45 | + } else { |
| 46 | + return None; |
| 47 | + }; |
| 48 | + |
| 49 | + let const_name = if method_name.starts_with("max") { "MAX" } else { "MIN" }; |
| 50 | + Some((const_name, ty_name)) |
| 51 | +} |
| 52 | + |
| 53 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) { |
| 54 | + // We allow casts from any function type to any function type. |
| 55 | + match cast_to.kind() { |
| 56 | + ty::FnDef(..) | ty::FnPtr(..) => return, |
| 57 | + _ => { /* continue to checks */ }, |
| 58 | + } |
| 59 | + |
| 60 | + if let ty::FnDef(def_id, generics) = cast_from.kind() |
| 61 | + && let Some(method_name) = cx.tcx.opt_item_name(*def_id) |
| 62 | + && let Some((const_name, ty_name)) = get_const_name_and_ty_name(cx, method_name, *def_id, generics.as_slice()) |
| 63 | + { |
| 64 | + let mut applicability = Applicability::MaybeIncorrect; |
| 65 | + let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability); |
| 66 | + |
| 67 | + span_lint_and_then( |
| 68 | + cx, |
| 69 | + CONFUSING_METHOD_TO_NUMERIC_CAST, |
| 70 | + expr.span, |
| 71 | + format!("casting function pointer `{from_snippet}` to `{cast_to}`"), |
| 72 | + |diag| { |
| 73 | + diag.span_suggestion_verbose( |
| 74 | + expr.span, |
| 75 | + "did you mean to use the associated constant?", |
| 76 | + format!("{ty_name}::{const_name} as {cast_to}"), |
| 77 | + applicability, |
| 78 | + ); |
| 79 | + }, |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
0 commit comments