From 250c1842b1e27c1eb6d174e8a954ea37a2f707bb Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 4 Feb 2020 17:02:46 +0100 Subject: [PATCH] Document the indent_relative_to arg of snippet_block --- clippy_lints/src/utils/mod.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index b9a62382aa3..ed6d9f81cc9 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -534,16 +534,40 @@ pub fn snippet_opt(cx: &T, span: Span) -> Option { cx.sess().source_map().span_to_snippet(span).ok() } -/// Converts a span (from a block) to a code snippet if available, otherwise use -/// default. -/// This trims the code of indentation, except for the first line. Use it for -/// blocks or block-like +/// Converts a span (from a block) to a code snippet if available, otherwise use default. +/// +/// This trims the code of indentation, except for the first line. Use it for blocks or block-like /// things which need to be printed as such. /// +/// The `indent_relative_to` arg can be used, to provide a span, where the indentation of the +/// resulting snippet of the given span. +/// /// # Example +/// /// ```rust,ignore -/// snippet_block(cx, expr.span, "..", None) +/// snippet_block(cx, block.span, "..", None) +/// // where, `block` is the block of the if expr +/// if x { +/// y; +/// } +/// // will return the snippet +/// { +/// y; +/// } /// ``` +/// +/// ```rust,ignore +/// snippet_block(cx, block.span, "..", Some(if_expr.span)) +/// // where, `block` is the block of the if expr +/// if x { +/// y; +/// } +/// // will return the snippet +/// { +/// y; +/// } // aligned with `if` +/// ``` +/// Note that the first line of the snippet always has 0 indentation. pub fn snippet_block<'a, T: LintContext>( cx: &T, span: Span,