|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_config::types::{DisallowedPath, create_disallowed_map}; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 4 | +use clippy_utils::paths::PathNS; |
| 5 | +use rustc_data_structures::fx::FxHashMap; |
| 6 | +use rustc_hir::def::{DefKind, Res}; |
| 7 | +use rustc_hir::def_id::DefIdMap; |
| 8 | +use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty}; |
| 9 | +use rustc_hir::{AmbigArg, FnRetTy, ImplItem, ImplItemKind, Item, ItemKind, TraitItem, TraitItemKind, Ty, TyKind}; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_middle::ty::TyCtxt; |
| 12 | +use rustc_session::impl_lint_pass; |
| 13 | +use rustc_span::Span; |
| 14 | + |
| 15 | +declare_clippy_lint! { |
| 16 | + /// ### What it does |
| 17 | + /// Denies the configured types in clippy.toml from being returned by pub functions. |
| 18 | + /// |
| 19 | + /// ### Why is this bad? |
| 20 | + /// Some types are undesirable in public APIs (e.g. `anyhow::Result`). |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust,ignore |
| 24 | + /// pub fn foo() -> anyhow::Result<()> { |
| 25 | + /// Ok(()) |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + #[clippy::version = "1.80.0"] |
| 29 | + pub DISALLOWED_PUB_RETURN_TYPES, |
| 30 | + style, |
| 31 | + "use of disallowed types in pub function return types" |
| 32 | +} |
| 33 | + |
| 34 | +impl_lint_pass!(DisallowedPubReturnTypes => [DISALLOWED_PUB_RETURN_TYPES]); |
| 35 | + |
| 36 | +pub struct DisallowedPubReturnTypes { |
| 37 | + def_ids: DefIdMap<(&'static str, &'static DisallowedPath)>, |
| 38 | + prim_tys: FxHashMap<rustc_hir::PrimTy, (&'static str, &'static DisallowedPath)>, |
| 39 | +} |
| 40 | + |
| 41 | +impl DisallowedPubReturnTypes { |
| 42 | + pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self { |
| 43 | + let (def_ids, prim_tys) = create_disallowed_map( |
| 44 | + tcx, |
| 45 | + &conf.disallowed_pub_return_types, |
| 46 | + PathNS::Type, |
| 47 | + def_kind_predicate, |
| 48 | + "type", |
| 49 | + true, |
| 50 | + ); |
| 51 | + Self { def_ids, prim_tys } |
| 52 | + } |
| 53 | + |
| 54 | + fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) { |
| 55 | + let (path, disallowed_path) = match res { |
| 56 | + Res::Def(_, did) if let Some(&x) = self.def_ids.get(did) => x, |
| 57 | + Res::PrimTy(prim) if let Some(&x) = self.prim_tys.get(prim) => x, |
| 58 | + _ => return, |
| 59 | + }; |
| 60 | + span_lint_and_then( |
| 61 | + cx, |
| 62 | + DISALLOWED_PUB_RETURN_TYPES, |
| 63 | + span, |
| 64 | + format!("returning a disallowed type `{path}` in a public API"), |
| 65 | + disallowed_path.diag_amendment(span), |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +pub fn def_kind_predicate(def_kind: DefKind) -> bool { |
| 71 | + matches!( |
| 72 | + def_kind, |
| 73 | + DefKind::Struct |
| 74 | + | DefKind::Union |
| 75 | + | DefKind::Enum |
| 76 | + | DefKind::Trait |
| 77 | + | DefKind::TyAlias |
| 78 | + | DefKind::ForeignTy |
| 79 | + | DefKind::AssocTy |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +struct TyVisitor<'a, 'tcx> { |
| 84 | + cx: &'a LateContext<'tcx>, |
| 85 | + lint: &'a DisallowedPubReturnTypes, |
| 86 | +} |
| 87 | + |
| 88 | +impl<'tcx> Visitor<'tcx> for TyVisitor<'_, 'tcx> { |
| 89 | + fn visit_ty(&mut self, ty: &'tcx Ty<'tcx, AmbigArg>) { |
| 90 | + if let TyKind::Path(path) = &ty.kind { |
| 91 | + let res = self.cx.qpath_res(path, ty.hir_id); |
| 92 | + self.lint.check_res_emit(self.cx, &res, ty.span); |
| 93 | + } |
| 94 | + walk_ty(self, ty); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<'tcx> LateLintPass<'tcx> for DisallowedPubReturnTypes { |
| 99 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 100 | + if let ItemKind::Fn { ref sig, .. } = item.kind |
| 101 | + && cx.effective_visibilities.is_exported(item.owner_id.def_id) |
| 102 | + && let FnRetTy::Return(ty) = sig.decl.output |
| 103 | + { |
| 104 | + TyVisitor { cx, lint: self }.visit_ty_unambig(ty); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { |
| 109 | + if let ImplItemKind::Fn(ref sig, _) = item.kind |
| 110 | + && clippy_utils::trait_ref_of_method(cx, item.owner_id).is_none() |
| 111 | + && cx.effective_visibilities.is_exported(item.owner_id.def_id) |
| 112 | + && let FnRetTy::Return(ty) = sig.decl.output |
| 113 | + { |
| 114 | + TyVisitor { cx, lint: self }.visit_ty_unambig(ty); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) { |
| 119 | + if let TraitItemKind::Fn(ref sig, _) = item.kind |
| 120 | + && cx.effective_visibilities.is_exported(item.owner_id.def_id) |
| 121 | + && let FnRetTy::Return(ty) = sig.decl.output |
| 122 | + { |
| 123 | + TyVisitor { cx, lint: self }.visit_ty_unambig(ty); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments