]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/casts/fn_to_numeric_cast_any.rs
Merge commit '0eff589afc83e21a03a168497bbab6b4dfbb4ef6' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / casts / fn_to_numeric_cast_any.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
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, Ty};
7
8 use super::FN_TO_NUMERIC_CAST_ANY;
9
10 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
11     // We allow casts from any function type to any function type.
12     match cast_to.kind() {
13         ty::FnDef(..) | ty::FnPtr(..) => return,
14         _ => { /* continue to checks */ },
15     }
16
17     match cast_from.kind() {
18         ty::FnDef(..) | ty::FnPtr(_) => {
19             let mut applicability = Applicability::MaybeIncorrect;
20             let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);
21
22             span_lint_and_sugg(
23                 cx,
24                 FN_TO_NUMERIC_CAST_ANY,
25                 expr.span,
26                 &format!("casting function pointer `{}` to `{}`", from_snippet, cast_to),
27                 "did you mean to invoke the function?",
28                 format!("{}() as {}", from_snippet, cast_to),
29                 applicability,
30             );
31         },
32         _ => {},
33     }
34 }