Skip to content

Commit 7154e32

Browse files
committed
Add examples to better explain walk_span_to_context
1 parent a7ddaf6 commit 7154e32

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

clippy_utils/src/source.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ pub fn snippet_with_context(
293293
/// inside a macro expansion, or the original span if it is not. Note this will return `None` in the
294294
/// case of the span being in a macro expansion, but the target context is from expanding a macro
295295
/// argument.
296+
///
297+
/// Given the following
298+
///
299+
/// ```rust,ignore
300+
/// macro_rules! m { ($e:expr) => { f($e) }; }
301+
/// g(m!(0))
302+
/// ```
303+
///
304+
/// If called with a span of the call to `f` and a context of the call to `g` this will return a
305+
/// span containing `m!(0)`. However, if called with a span of the literal `0` this will give a span
306+
/// containing `0` as the context is the same as the outer context.
307+
///
308+
/// This will traverse through multiple macro calls. Given the following:
309+
///
310+
/// ```rust,ignore
311+
/// macro_rules! m { ($e:expr) => { n!($e, 0) }; }
312+
/// macro_rules! n { ($e:expr, $f:expr) => { f($e, $f) }; }
313+
/// g(m!(0))
314+
/// ```
315+
///
316+
/// If called with a span of the call to `f` and a context of the call to `g` this will return a
317+
/// span containing `m!(0)`.
296318
pub fn walk_span_to_context(span: Span, outer: SyntaxContext) -> Option<Span> {
297319
let outer_span = hygiene::walk_chain(span, outer);
298320
(outer_span.ctxt() == outer).then(|| outer_span)

0 commit comments

Comments
 (0)