Document the indent_relative_to arg of snippet_block

This commit is contained in:
flip1995 2020-02-04 17:02:46 +01:00
parent 7363728d18
commit 250c1842b1
No known key found for this signature in database
GPG Key ID: 693086869D506637

View File

@ -534,16 +534,40 @@ pub fn snippet_opt<T: LintContext>(cx: &T, span: Span) -> Option<String> {
cx.sess().source_map().span_to_snippet(span).ok() cx.sess().source_map().span_to_snippet(span).ok()
} }
/// Converts a span (from a block) to a code snippet if available, otherwise use /// Converts a span (from a block) to a code snippet if available, otherwise use default.
/// default. ///
/// This trims the code of indentation, except for the first line. Use it for /// This trims the code of indentation, except for the first line. Use it for blocks or block-like
/// blocks or block-like
/// things which need to be printed as such. /// 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 /// # Example
///
/// ```rust,ignore /// ```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>( pub fn snippet_block<'a, T: LintContext>(
cx: &T, cx: &T,
span: Span, span: Span,