clippy_utils::source

Function snippet_block

Source
pub fn snippet_block<'a>(
    sess: &impl HasSession,
    span: Span,
    default: &'a str,
    indent_relative_to: Option<Span>,
) -> Cow<'a, str>
Expand description

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

snippet_block(cx, block.span, "..", None)
// where, `block` is the block of the if expr
    if x {
        y;
    }
// will return the snippet
{
    y;
}
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.