|
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, QPath, TraitItem, TraitItemKind, |
| 19 | + 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,27 @@ 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, visibility) = match kind { |
| 255 | + FnKind::ItemFn(.., header) => ( |
| 256 | + fn_header_search_pat(*header), |
| 257 | + Pat::Str(""), |
| 258 | + tcx.visibility(tcx.hir().local_def_id(hir_id)), |
| 259 | + ), |
| 260 | + FnKind::Method(.., sig) => ( |
| 261 | + fn_header_search_pat(sig.header), |
| 262 | + Pat::Str(""), |
| 263 | + tcx.visibility(tcx.hir().local_def_id(hir_id)), |
| 264 | + ), |
| 265 | + FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, &body.value).1), |
| 266 | + }; |
| 267 | + if visibility.is_public() { |
| 268 | + (Pat::Str("pub"), end_pat) |
| 269 | + } else { |
| 270 | + (start_pat, end_pat) |
| 271 | + } |
| 272 | +} |
| 273 | + |
253 | 274 | pub trait WithSearchPat {
|
254 | 275 | type Context: LintContext;
|
255 | 276 | fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
|
@@ -277,6 +298,18 @@ impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
|
277 | 298 | impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
|
278 | 299 | impl_with_search_pat!(LateContext: Variant with variant_search_pat);
|
279 | 300 |
|
| 301 | +impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) { |
| 302 | + type Context = LateContext<'cx>; |
| 303 | + |
| 304 | + fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat) { |
| 305 | + fn_kind_pat(cx.tcx, &self.0, &self.1, self.2) |
| 306 | + } |
| 307 | + |
| 308 | + fn span(&self) -> Span { |
| 309 | + self.3 |
| 310 | + } |
| 311 | +} |
| 312 | + |
280 | 313 | /// Checks if the item likely came from a proc-macro.
|
281 | 314 | ///
|
282 | 315 | /// This should be called after `in_external_macro` and the initial pattern matching of the ast as
|
|
0 commit comments