Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 51e9113

Browse files
committed
Add span_contains_comments util
1 parent be8bd60 commit 51e9113

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

clippy_utils/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ use rustc_hir::{
8787
Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem, TraitItemKind,
8888
TraitRef, TyKind, UnOp,
8989
};
90+
use rustc_lexer::{tokenize, TokenKind};
9091
use rustc_lint::{LateContext, Level, Lint, LintContext};
9192
use rustc_middle::hir::place::PlaceBase;
9293
use rustc_middle::ty as rustc_ty;
@@ -104,6 +105,7 @@ use rustc_semver::RustcVersion;
104105
use rustc_session::Session;
105106
use rustc_span::hygiene::{ExpnKind, MacroKind};
106107
use rustc_span::source_map::original_sp;
108+
use rustc_span::source_map::SourceMap;
107109
use rustc_span::sym;
108110
use rustc_span::symbol::{kw, Symbol};
109111
use rustc_span::{Span, DUMMY_SP};
@@ -2278,6 +2280,18 @@ pub fn walk_to_expr_usage<'tcx, T>(
22782280
None
22792281
}
22802282

2283+
/// Checks whether a given span has any comment token
2284+
/// This checks for all types of comment: line "//", block "/**", doc "///" "//!"
2285+
pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
2286+
let Ok(snippet) = sm.span_to_snippet(span) else { return false };
2287+
return tokenize(&snippet).any(|token| {
2288+
matches!(
2289+
token.kind,
2290+
TokenKind::BlockComment { .. } | TokenKind::LineComment { .. }
2291+
)
2292+
});
2293+
}
2294+
22812295
macro_rules! op_utils {
22822296
($($name:ident $assign:ident)*) => {
22832297
/// Binary operation traits like `LangItem::Add`

0 commit comments

Comments
 (0)