|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use rustc_hir as hir; |
| 3 | +use rustc_lint::{LateContext, LateLintPass}; |
| 4 | +use rustc_middle::ty::Ty; |
| 5 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 6 | + |
| 7 | +declare_clippy_lint! { |
| 8 | + /// ### What it does |
| 9 | + /// Warns about needless / redundant type annotations. |
| 10 | + /// |
| 11 | + /// ### Why is this bad? |
| 12 | + /// Code is more idiomatic, shorter, and easier to modify. |
| 13 | + /// |
| 14 | + /// ### Example |
| 15 | + /// ```rust |
| 16 | + /// let foo: String = String::new(); |
| 17 | + /// ``` |
| 18 | + /// Use instead: |
| 19 | + /// ```rust |
| 20 | + /// let foo = String::new(); |
| 21 | + /// ``` |
| 22 | + #[clippy::version = "1.70.0"] |
| 23 | + pub REDUNDANT_TYPE_ANNOTATIONS, |
| 24 | + pedantic, |
| 25 | + "Warns about needless / redundant type annotations." |
| 26 | +} |
| 27 | +declare_lint_pass!(RedundantTypeAnnotations => [REDUNDANT_TYPE_ANNOTATIONS]); |
| 28 | + |
| 29 | +fn is_redundant_in_resolved_path<'tcx>(cx: &LateContext<'tcx>, res: hir::def::Res, func_return_type: Ty<'tcx>) -> bool { |
| 30 | + // type annotation is primitive |
| 31 | + if_chain! { |
| 32 | + if let hir::def::Res::PrimTy(primty) = res; |
| 33 | + |
| 34 | + if func_return_type.is_primitive(); |
| 35 | + if let Some(func_return_type_sym) = func_return_type.primitive_symbol(); |
| 36 | + |
| 37 | + then { |
| 38 | + return primty.name() == func_return_type_sym; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // type annotation is any other non generic type |
| 43 | + if_chain! { |
| 44 | + if let hir::def::Res::Def(_, defid) = res; |
| 45 | + if let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars(); |
| 46 | + |
| 47 | + then { |
| 48 | + return annotation_ty == func_return_type |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + false |
| 53 | +} |
| 54 | + |
| 55 | +fn is_redundant_in_func_call<'tcx>( |
| 56 | + cx: &LateContext<'tcx>, |
| 57 | + res: hir::def::Res, |
| 58 | + func_return_path: &hir::QPath<'tcx>, |
| 59 | +) -> bool { |
| 60 | + // TODO: Problem with something like |
| 61 | + // let a: String = StructTest::func() where func returns String::from |
| 62 | + // The problem is that the DefId that I get refers to the struct itself and not to string |
| 63 | + |
| 64 | + match func_return_path { |
| 65 | + // let a: String = f(); where f: fn f() -> String |
| 66 | + hir::QPath::Resolved(_, resolved_path) => { |
| 67 | + if_chain! { |
| 68 | + if let hir::def::Res::Def(_, defid) = resolved_path.res; |
| 69 | + if let Some(middle_ty_init) = cx.tcx.type_of(defid).no_bound_vars(); |
| 70 | + if middle_ty_init.is_fn(); |
| 71 | + if let Some(init_return_type) = middle_ty_init.fn_sig(cx.tcx).output().no_bound_vars(); |
| 72 | + then { |
| 73 | + return is_redundant_in_resolved_path(cx, res, init_return_type); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + false |
| 78 | + }, |
| 79 | + // let a: String = String::new(); |
| 80 | + hir::QPath::TypeRelative(func_hir_ty, _) => { |
| 81 | + if_chain! { |
| 82 | + if let hir::def::Res::Def(_, defid) = res; |
| 83 | + if let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars(); |
| 84 | + |
| 85 | + if let hir::TyKind::Path(init_ty_path) = &func_hir_ty.kind; |
| 86 | + if let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path; |
| 87 | + if let hir::def::Res::Def(_, init_defid) = resolved_init_ty_path.res; |
| 88 | + if let Some(init_ty) = cx.tcx.type_of(init_defid).no_bound_vars(); |
| 89 | + |
| 90 | + then { |
| 91 | + return annotation_ty == init_ty |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + false |
| 96 | + }, |
| 97 | + hir::QPath::LangItem(..) => false, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl LateLintPass<'_> for RedundantTypeAnnotations { |
| 102 | + fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'_>) { |
| 103 | + if_chain! { |
| 104 | + // type annotation part |
| 105 | + if let Some(ty) = &local.ty; |
| 106 | + if let hir::TyKind::Path(ty_path) = &ty.kind; |
| 107 | + if let hir::QPath::Resolved(_, resolved_path_ty) = ty_path; |
| 108 | + |
| 109 | + // initialization part |
| 110 | + if let Some(init) = local.init; |
| 111 | + |
| 112 | + then { |
| 113 | + match &init.kind { |
| 114 | + // When the initialization is a call to a function |
| 115 | + hir::ExprKind::Call(init_call, _) => { |
| 116 | + if let hir::ExprKind::Path(init_path) = &init_call.kind { |
| 117 | + if is_redundant_in_func_call(cx, resolved_path_ty.res, init_path) { |
| 118 | + span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "Redundant type annotation"); |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + // When the initialization is a path for example u32::MAX |
| 123 | + hir::ExprKind::Path(init_path) => { |
| 124 | + if_chain! { |
| 125 | + if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res; |
| 126 | + |
| 127 | + if let hir::QPath::TypeRelative(init_ty, _) = init_path; |
| 128 | + if let hir::TyKind::Path(init_ty_path) = &init_ty.kind; |
| 129 | + if let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path; |
| 130 | + if let hir::def::Res::PrimTy(primty_init) = resolved_init_ty_path.res; |
| 131 | + |
| 132 | + if primty == primty_init; |
| 133 | + then { |
| 134 | + span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "Redundant type annotation"); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + _ => () |
| 139 | + } |
| 140 | + } |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments