1 use rustc_errors::Applicability;
2 use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
3 use rustc_lint::LateContext;
4 use rustc_span::symbol::sym;
7 get_qpath_generic_tys, is_ty_param_diagnostic_item, snippet_with_applicability, span_lint_and_sugg,
12 pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
13 if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
14 if let Some(alternate) = match_buffer_type(cx, qpath) {
19 "usage of `Rc<T>` when T is a buffer type",
21 format!("Rc<{}>", alternate),
22 Applicability::MachineApplicable,
24 } else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
25 let qpath = match &ty.kind {
26 TyKind::Path(qpath) => qpath,
29 let inner_span = match get_qpath_generic_tys(qpath).next() {
33 let mut applicability = Applicability::MachineApplicable;
38 "usage of `Rc<T>` when T is a buffer type",
42 snippet_with_applicability(cx, inner_span, "..", &mut applicability)
44 Applicability::MachineApplicable,
47 } else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
48 if let Some(alternate) = match_buffer_type(cx, qpath) {
53 "usage of `Arc<T>` when T is a buffer type",
55 format!("Arc<{}>", alternate),
56 Applicability::MachineApplicable,
58 } else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
59 let qpath = match &ty.kind {
60 TyKind::Path(qpath) => qpath,
63 let inner_span = match get_qpath_generic_tys(qpath).next() {
67 let mut applicability = Applicability::MachineApplicable;
72 "usage of `Arc<T>` when T is a buffer type",
76 snippet_with_applicability(cx, inner_span, "..", &mut applicability)
78 Applicability::MachineApplicable,
84 fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
85 if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
87 } else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
88 Some("std::ffi::OsStr")
89 } else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
90 Some("std::path::Path")