|
12 | 12 | //! code was written, and check if the span contains that text. Note this will only work correctly
|
13 | 13 | //! if the span is not from a `macro_rules` based macro.
|
14 | 14 |
|
15 |
| -use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy}; |
| 15 | +use rustc_ast::{ |
| 16 | + ast::{AttrKind, Attribute, IntTy, LitIntType, LitKind, StrStyle, UintTy}, |
| 17 | + token::CommentKind, |
| 18 | + AttrStyle, |
| 19 | +}; |
16 | 20 | use rustc_hir::{
|
17 | 21 | intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
|
18 | 22 | Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem,
|
@@ -271,6 +275,32 @@ fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirI
|
271 | 275 | (start_pat, end_pat)
|
272 | 276 | }
|
273 | 277 |
|
| 278 | +fn attr_search_pat(attr: &Attribute) -> (Pat, Pat) { |
| 279 | + match attr.kind { |
| 280 | + AttrKind::Normal(..) => { |
| 281 | + if matches!(attr.style, AttrStyle::Outer) { |
| 282 | + (Pat::Str("#["), Pat::Str("]")) |
| 283 | + } else { |
| 284 | + (Pat::Str("#!["), Pat::Str("]")) |
| 285 | + } |
| 286 | + }, |
| 287 | + AttrKind::DocComment(_kind @ CommentKind::Line, ..) => { |
| 288 | + if matches!(attr.style, AttrStyle::Outer) { |
| 289 | + (Pat::Str("///"), Pat::Str("")) |
| 290 | + } else { |
| 291 | + (Pat::Str("//!"), Pat::Str("")) |
| 292 | + } |
| 293 | + }, |
| 294 | + AttrKind::DocComment(_kind @ CommentKind::Block, ..) => { |
| 295 | + if matches!(attr.style, AttrStyle::Outer) { |
| 296 | + (Pat::Str("/**"), Pat::Str("*/")) |
| 297 | + } else { |
| 298 | + (Pat::Str("/*!"), Pat::Str("*/")) |
| 299 | + } |
| 300 | + }, |
| 301 | + } |
| 302 | +} |
| 303 | + |
274 | 304 | pub trait WithSearchPat {
|
275 | 305 | type Context: LintContext;
|
276 | 306 | fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
|
@@ -310,6 +340,18 @@ impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
|
310 | 340 | }
|
311 | 341 | }
|
312 | 342 |
|
| 343 | +impl<'cx> WithSearchPat for (&Attribute, &LateContext<'cx>) { |
| 344 | + type Context = LateContext<'cx>; |
| 345 | + |
| 346 | + fn search_pat(&self, _cx: &Self::Context) -> (Pat, Pat) { |
| 347 | + attr_search_pat(&self.0) |
| 348 | + } |
| 349 | + |
| 350 | + fn span(&self) -> Span { |
| 351 | + self.0.span |
| 352 | + } |
| 353 | +} |
| 354 | + |
313 | 355 | /// Checks if the item likely came from a proc-macro.
|
314 | 356 | ///
|
315 | 357 | /// This should be called after `in_external_macro` and the initial pattern matching of the ast as
|
|
0 commit comments