Skip to content

Commit ef9ad80

Browse files
committed
Add examples to better explain walk_span_to_context
1 parent 74cf5f2 commit ef9ad80

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
@@ -302,6 +302,28 @@ pub fn snippet_with_context(
302302
/// inside a macro expansion, or the original span if it is not. Note this will return `None` in the
303303
/// case of the span being in a macro expansion, but the target context is from expanding a macro
304304
/// argument.
305+
///
306+
/// Given the following
307+
///
308+
/// ```rust,ignore
309+
/// macro_rules! m { ($e:expr) => { f($e) }; }
310+
/// g(m!(0))
311+
/// ```
312+
///
313+
/// If called with a span of the call to `f` and a context of the call to `g` this will return a
314+
/// span containing `m!(0)`. However, if called with a span of the literal `0` this will give a span
315+
/// containing `0` as the context is the same as the outer context.
316+
///
317+
/// This will traverse through multiple macro calls. Given the following:
318+
///
319+
/// ```rust,ignore
320+
/// macro_rules! m { ($e:expr) => { n!($e, 0) }; }
321+
/// macro_rules! n { ($e:expr, $f:expr) => { f($e, $f) }; }
322+
/// g(m!(0))
323+
/// ```
324+
///
325+
/// If called with a span of the call to `f` and a context of the call to `g` this will return a
326+
/// span containing `m!(0)`.
305327
pub fn walk_span_to_context(span: Span, outer: SyntaxContext) -> Option<Span> {
306328
let outer_span = hygiene::walk_chain(span, outer);
307329
(outer_span.ctxt() == outer).then(|| outer_span)

0 commit comments

Comments
 (0)