|
14 | 14 |
|
15 | 15 | use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy};
|
16 | 16 | use rustc_hir::{
|
17 |
| - Block, BlockCheckMode, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, Impl, ImplItem, ImplItemKind, |
18 |
| - IsAuto, Item, ItemKind, LoopSource, MatchSource, QPath, TraitItem, TraitItemKind, UnOp, UnsafeSource, Unsafety, |
19 |
| - Variant, VariantData, YieldSource, |
| 17 | + intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId, |
| 18 | + Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem, |
| 19 | + TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource, |
20 | 20 | };
|
21 | 21 | use rustc_lint::{LateContext, LintContext};
|
22 | 22 | use rustc_middle::ty::TyCtxt;
|
@@ -250,6 +250,26 @@ fn variant_search_pat(v: &Variant<'_>) -> (Pat, Pat) {
|
250 | 250 | }
|
251 | 251 | }
|
252 | 252 |
|
| 253 | +fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirId) -> (Pat, Pat) { |
| 254 | + let (start_pat, end_pat) = match kind { |
| 255 | + FnKind::ItemFn(.., header) => (fn_header_search_pat(*header), Pat::Str("")), |
| 256 | + FnKind::Method(.., sig) => (fn_header_search_pat(sig.header), Pat::Str("")), |
| 257 | + FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, &body.value).1), |
| 258 | + }; |
| 259 | + let start_pat = match tcx.hir().get(hir_id) { |
| 260 | + Node::Item(Item { vis_span, .. }) | Node::ImplItem(ImplItem { vis_span, .. }) => { |
| 261 | + if vis_span.is_empty() { |
| 262 | + start_pat |
| 263 | + } else { |
| 264 | + Pat::Str("pub") |
| 265 | + } |
| 266 | + }, |
| 267 | + Node::TraitItem(_) => start_pat, |
| 268 | + _ => Pat::Str(""), |
| 269 | + }; |
| 270 | + (start_pat, end_pat) |
| 271 | +} |
| 272 | + |
253 | 273 | pub trait WithSearchPat {
|
254 | 274 | type Context: LintContext;
|
255 | 275 | fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
|
@@ -277,6 +297,18 @@ impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
|
277 | 297 | impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
|
278 | 298 | impl_with_search_pat!(LateContext: Variant with variant_search_pat);
|
279 | 299 |
|
| 300 | +impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) { |
| 301 | + type Context = LateContext<'cx>; |
| 302 | + |
| 303 | + fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat) { |
| 304 | + fn_kind_pat(cx.tcx, self.0, self.1, self.2) |
| 305 | + } |
| 306 | + |
| 307 | + fn span(&self) -> Span { |
| 308 | + self.3 |
| 309 | + } |
| 310 | +} |
| 311 | + |
280 | 312 | /// Checks if the item likely came from a proc-macro.
|
281 | 313 | ///
|
282 | 314 | /// This should be called after `in_external_macro` and the initial pattern matching of the ast as
|
|
0 commit comments