about summary refs log tree commit diff
path: root/clippy_utils/src/source.rs
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2021-04-13 20:55:12 -0400
committerJason Newcomb <jsnewcomb@pm.me>2021-04-22 09:36:46 -0400
commitef9ad806179665cce9c5813db369038d84291fd8 (patch)
treefff40f50c72ca7b33bb852f42284760399042f78 /clippy_utils/src/source.rs
parent74cf5f2fc6c121ae0eaaa709c9a303cd82fe9b30 (diff)
downloadrust-ef9ad806179665cce9c5813db369038d84291fd8.tar.gz
rust-ef9ad806179665cce9c5813db369038d84291fd8.zip
Add examples to better explain `walk_span_to_context`
Diffstat (limited to 'clippy_utils/src/source.rs')
-rw-r--r--clippy_utils/src/source.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs
index 2e731c182ec..4d49b43bde9 100644
--- a/clippy_utils/src/source.rs
+++ b/clippy_utils/src/source.rs
@@ -302,6 +302,28 @@ pub fn snippet_with_context(
 /// inside a macro expansion, or the original span if it is not. Note this will return `None` in the
 /// case of the span being in a macro expansion, but the target context is from expanding a macro
 /// argument.
+///
+/// Given the following
+///
+/// ```rust,ignore
+/// macro_rules! m { ($e:expr) => { f($e) }; }
+/// g(m!(0))
+/// ```
+///
+/// If called with a span of the call to `f` and a context of the call to `g` this will return a
+/// span containing `m!(0)`. However, if called with a span of the literal `0` this will give a span
+/// containing `0` as the context is the same as the outer context.
+///
+/// This will traverse through multiple macro calls. Given the following:
+///
+/// ```rust,ignore
+/// macro_rules! m { ($e:expr) => { n!($e, 0) }; }
+/// macro_rules! n { ($e:expr, $f:expr) => { f($e, $f) }; }
+/// g(m!(0))
+/// ```
+///
+/// If called with a span of the call to `f` and a context of the call to `g` this will return a
+/// span containing `m!(0)`.
 pub fn walk_span_to_context(span: Span, outer: SyntaxContext) -> Option<Span> {
     let outer_span = hygiene::walk_chain(span, outer);
     (outer_span.ctxt() == outer).then(|| outer_span)