|
| 1 | +usecrate::functions::REF_OPTION; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::is_trait_impl_item; |
| 4 | +use clippy_utils::source::snippet; |
| 5 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir as hir; |
| 8 | +use rustc_hir::intravisit::FnKind; |
| 9 | +use rustc_hir::{FnDecl,HirId}; |
| 10 | +use rustc_lint::LateContext; |
| 11 | +use rustc_middle::ty::{self,GenericArgKind,Mutability,Ty}; |
| 12 | +use rustc_span::def_id::LocalDefId; |
| 13 | +use rustc_span::{Span, sym}; |
| 14 | + |
| 15 | +fncheck_ty<'a>(cx:&LateContext<'a>,param:&rustc_hir::Ty<'a>,param_ty:Ty<'a>,fixes:&mutVec<(Span,String)>){ |
| 16 | +iflet ty::Ref(_, opt_ty,Mutability::Not) = param_ty.kind() |
| 17 | + && is_type_diagnostic_item(cx,*opt_ty, sym::Option) |
| 18 | + && let ty::Adt(_, opt_gen) = opt_ty.kind() |
| 19 | + && let[gen] = opt_gen.as_slice() |
| 20 | + && letGenericArgKind::Type(gen_ty) = gen.unpack() |
| 21 | + && !gen_ty.is_ref() |
| 22 | +// Need to gen the original spans, so first parsing mid, and hir parsing afterward |
| 23 | + && let hir::TyKind::Ref(lifetime, hir::MutTy{ ty, .. }) = param.kind |
| 24 | + && let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind |
| 25 | + && let(Some(first),Some(last)) = (path.segments.first(), path.segments.last()) |
| 26 | + && letSome(hir::GenericArgs{ |
| 27 | +args:[hir::GenericArg::Type(opt_ty)], |
| 28 | + .. |
| 29 | +}) = last.args |
| 30 | +{ |
| 31 | +let lifetime = snippet(cx, lifetime.ident.span,".."); |
| 32 | + fixes.push(( |
| 33 | + param.span, |
| 34 | +format!( |
| 35 | +"{}<&{lifetime}{}{}>", |
| 36 | + snippet(cx, first.ident.span.to(last.ident.span),".."), |
| 37 | +if lifetime.is_empty(){""} else {" "}, |
| 38 | + snippet(cx, opt_ty.span,"..") |
| 39 | +), |
| 40 | +)); |
| 41 | +} |
| 42 | +} |
| 43 | + |
| 44 | +fncheck_fn_sig<'a>(cx:&LateContext<'a>,decl:&FnDecl<'a>,span:Span,sig: ty::FnSig<'a>){ |
| 45 | +letmut fixes = Vec::new(); |
| 46 | +// Check function arguments' types |
| 47 | +for(param, param_ty)in decl.inputs.iter().zip(sig.inputs()){ |
| 48 | +check_ty(cx, param,*param_ty,&mut fixes); |
| 49 | +} |
| 50 | +// Check return type |
| 51 | +iflet hir::FnRetTy::Return(ty) = &decl.output{ |
| 52 | +check_ty(cx, ty, sig.output(),&mut fixes); |
| 53 | +} |
| 54 | +if !fixes.is_empty(){ |
| 55 | +span_lint_and_then( |
| 56 | + cx, |
| 57 | +REF_OPTION, |
| 58 | + span, |
| 59 | +"it is more idiomatic to use `Option<&T>` instead of `&Option<T>`", |
| 60 | + |diag| { |
| 61 | + diag.multipart_suggestion("change this to", fixes,Applicability::Unspecified); |
| 62 | +}, |
| 63 | +); |
| 64 | +} |
| 65 | +} |
| 66 | + |
| 67 | +#[allow(clippy::too_many_arguments)] |
| 68 | +pub(crate)fncheck_fn<'a>( |
| 69 | +cx:&LateContext<'a>, |
| 70 | +kind:FnKind<'_>, |
| 71 | +decl:&FnDecl<'a>, |
| 72 | +span:Span, |
| 73 | +hir_id:HirId, |
| 74 | +def_id:LocalDefId, |
| 75 | +body:&hir::Body<'_>, |
| 76 | +avoid_breaking_exported_api:bool, |
| 77 | +){ |
| 78 | +if avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id){ |
| 79 | +return; |
| 80 | +} |
| 81 | + |
| 82 | +ifletFnKind::Closure = kind { |
| 83 | +// Compute the span of the closure parameters + return type if set |
| 84 | +let span = iflet hir::FnRetTy::Return(out_ty) = &decl.output{ |
| 85 | +if decl.inputs.is_empty(){ |
| 86 | + out_ty.span |
| 87 | +}else{ |
| 88 | + span.with_hi(out_ty.span.hi()) |
| 89 | +} |
| 90 | +}elseiflet(Some(first),Some(last)) = (decl.inputs.first(), decl.inputs.last()){ |
| 91 | + first.span.to(last.span) |
| 92 | +}else{ |
| 93 | +// No parameters - no point in checking |
| 94 | +return; |
| 95 | +}; |
| 96 | + |
| 97 | +// Figure out the signature of the closure |
| 98 | +let ty::Closure(_, args) = cx.typeck_results().expr_ty(body.value).kind()else{ |
| 99 | +return; |
| 100 | +}; |
| 101 | +let sig = args.as_closure().sig().skip_binder(); |
| 102 | + |
| 103 | +check_fn_sig(cx, decl, span, sig); |
| 104 | +}elseif !is_trait_impl_item(cx, hir_id){ |
| 105 | +let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder(); |
| 106 | +check_fn_sig(cx, decl, span, sig); |
| 107 | +} |
| 108 | +} |
| 109 | + |
| 110 | +pub(super)fncheck_trait_item<'a>( |
| 111 | +cx:&LateContext<'a>, |
| 112 | +trait_item:&hir::TraitItem<'a>, |
| 113 | +avoid_breaking_exported_api:bool, |
| 114 | +){ |
| 115 | +iflet hir::TraitItemKind::Fn(ref sig, _) = trait_item.kind |
| 116 | + && !(avoid_breaking_exported_api && cx.effective_visibilities.is_exported(trait_item.owner_id.def_id)) |
| 117 | +{ |
| 118 | +let def_id = trait_item.owner_id.def_id; |
| 119 | +let ty_sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder(); |
| 120 | +check_fn_sig(cx, sig.decl, sig.span, ty_sig); |
| 121 | +} |
| 122 | +} |
0 commit comments