diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2022-04-08 16:51:40 -0400 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2022-04-08 16:51:40 -0400 |
| commit | 63f6a79bf87cd5960c069bb45c88646519d096f8 (patch) | |
| tree | b178330610fe4b5844e7db0d253e881cf5f4822f /clippy_utils/src | |
| parent | a63308be0a66fe34aa1ccefef76fa6a0bc50d2a3 (diff) | |
| download | rust-63f6a79bf87cd5960c069bb45c88646519d096f8.tar.gz rust-63f6a79bf87cd5960c069bb45c88646519d096f8.zip | |
Don't lint various match lints when expanded by a proc-macro
Diffstat (limited to 'clippy_utils/src')
| -rw-r--r-- | clippy_utils/src/source.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs index dbad607c58e..beed7326803 100644 --- a/clippy_utils/src/source.rs +++ b/clippy_utils/src/source.rs @@ -7,9 +7,28 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LintContext}; use rustc_span::hygiene; +use rustc_span::source_map::SourceMap; use rustc_span::{BytePos, Pos, Span, SyntaxContext}; use std::borrow::Cow; +/// Checks if the span starts with the given text. This will return false if the span crosses +/// multiple files or if source is not available. +/// +/// This is used to check for proc macros giving unhelpful spans to things. +pub fn span_starts_with<T: LintContext>(cx: &T, span: Span, text: &str) -> bool { + fn helper(sm: &SourceMap, span: Span, text: &str) -> bool { + let pos = sm.lookup_byte_offset(span.lo()); + let Some(ref src) = pos.sf.src else { + return false; + }; + let end = span.hi() - pos.sf.start_pos; + src.get(pos.pos.0 as usize..end.0 as usize) + // Expression spans can include wrapping parenthesis. Remove them first. + .map_or(false, |s| s.trim_start_matches('(').starts_with(text)) + } + helper(cx.sess().source_map(), span, text) +} + /// Like `snippet_block`, but add braces if the expr is not an `ExprKind::Block`. /// Also takes an `Option<String>` which can be put inside the braces. pub fn expr_block<'a, T: LintContext>( |
