diff options
| author | dswij <dswijj@gmail.com> | 2022-08-01 20:31:23 +0800 |
|---|---|---|
| committer | dswij <dswijj@gmail.com> | 2022-08-28 00:07:00 +0800 |
| commit | 51e9113c60ce0699c8d343102828f26928081dc3 (patch) | |
| tree | 590bf1507e1a8cb57b53d8abc50fd8fd7cbd76d9 | |
| parent | be8bd600009dd087f6c2cd4b354cf016b2072fd3 (diff) | |
| download | rust-51e9113c60ce0699c8d343102828f26928081dc3.tar.gz rust-51e9113c60ce0699c8d343102828f26928081dc3.zip | |
Add `span_contains_comments` util
| -rw-r--r-- | clippy_utils/src/lib.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 9308f085214..4c5adde6190 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -87,6 +87,7 @@ use rustc_hir::{ Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem, TraitItemKind, TraitRef, TyKind, UnOp, }; +use rustc_lexer::{tokenize, TokenKind}; use rustc_lint::{LateContext, Level, Lint, LintContext}; use rustc_middle::hir::place::PlaceBase; use rustc_middle::ty as rustc_ty; @@ -104,6 +105,7 @@ use rustc_semver::RustcVersion; use rustc_session::Session; use rustc_span::hygiene::{ExpnKind, MacroKind}; use rustc_span::source_map::original_sp; +use rustc_span::source_map::SourceMap; use rustc_span::sym; use rustc_span::symbol::{kw, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -2278,6 +2280,18 @@ pub fn walk_to_expr_usage<'tcx, T>( None } +/// Checks whether a given span has any comment token +/// This checks for all types of comment: line "//", block "/**", doc "///" "//!" +pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool { + let Ok(snippet) = sm.span_to_snippet(span) else { return false }; + return tokenize(&snippet).any(|token| { + matches!( + token.kind, + TokenKind::BlockComment { .. } | TokenKind::LineComment { .. } + ) + }); +} + macro_rules! op_utils { ($($name:ident $assign:ident)*) => { /// Binary operation traits like `LangItem::Add` |
