diff options
| author | Alessio Cosenza <cosenzaalessio17@gmail.com> | 2023-05-19 17:45:49 +0200 |
|---|---|---|
| committer | Alessio Cosenza <cosenzaalessio17@gmail.com> | 2023-06-05 10:22:45 +0200 |
| commit | 6f26df1c9a2ca1e4d161c49733c460fd32fddb02 (patch) | |
| tree | d4bbe4a1549ed52bc25930f2b94578aadf179cdb | |
| parent | 6776608f21a14fdc9ca3751cdac6ade48b3f0346 (diff) | |
| download | rust-6f26df1c9a2ca1e4d161c49733c460fd32fddb02.tar.gz rust-6f26df1c9a2ca1e4d161c49733c460fd32fddb02.zip | |
Extract common logic to function
| -rw-r--r-- | clippy_lints/src/redundant_type_annotations.rs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/clippy_lints/src/redundant_type_annotations.rs b/clippy_lints/src/redundant_type_annotations.rs index f277adfa94d..da139cdde92 100644 --- a/clippy_lints/src/redundant_type_annotations.rs +++ b/clippy_lints/src/redundant_type_annotations.rs @@ -111,6 +111,17 @@ fn is_redundant_in_func_call<'tcx>( false } +fn extract_primty<'tcx>(ty_kind: &hir::TyKind<'tcx>) -> Option<hir::PrimTy> { + if let hir::TyKind::Path(ty_path) = ty_kind + && let hir::QPath::Resolved(_, resolved_path_ty) = ty_path + && let hir::def::Res::PrimTy(primty) = resolved_path_ty.res + { + Some(primty) + } else { + None + } +} + impl LateLintPass<'_> for RedundantTypeAnnotations { fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'tcx>) { // type annotation part @@ -144,14 +155,10 @@ impl LateLintPass<'_> for RedundantTypeAnnotations { // When the initialization is a path for example u32::MAX hir::ExprKind::Path(init_path) => { // TODO: check for non primty - if let hir::TyKind::Path(ty_path) = &ty.kind - && let hir::QPath::Resolved(_, resolved_path_ty) = ty_path - && let hir::def::Res::PrimTy(primty) = resolved_path_ty.res + if let Some(primty) = extract_primty(&ty.kind) && let hir::QPath::TypeRelative(init_ty, _) = init_path - && let hir::TyKind::Path(init_ty_path) = &init_ty.kind - && let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path - && let hir::def::Res::PrimTy(primty_init) = resolved_init_ty_path.res + && let Some(primty_init) = extract_primty(&init_ty.kind) && primty == primty_init { |
