@@ -534,16 +534,40 @@ pub fn snippet_opt<T: LintContext>(cx: &T, span: Span) -> Option<String> {
534
534
cx. sess ( ) . source_map ( ) . span_to_snippet ( span) . ok ( )
535
535
}
536
536
537
- /// Converts a span (from a block) to a code snippet if available, otherwise use
538
- /// default.
539
- /// This trims the code of indentation, except for the first line. Use it for
540
- /// blocks or block-like
537
+ /// Converts a span (from a block) to a code snippet if available, otherwise use default.
538
+ ///
539
+ /// This trims the code of indentation, except for the first line. Use it for blocks or block-like
541
540
/// things which need to be printed as such.
542
541
///
542
+ /// The `indent_relative_to` arg can be used, to provide a span, where the indentation of the
543
+ /// resulting snippet of the given span.
544
+ ///
543
545
/// # Example
546
+ ///
544
547
/// ```rust,ignore
545
- /// snippet_block(cx, expr.span, "..", None)
548
+ /// snippet_block(cx, block.span, "..", None)
549
+ /// // where, `block` is the block of the if expr
550
+ /// if x {
551
+ /// y;
552
+ /// }
553
+ /// // will return the snippet
554
+ /// {
555
+ /// y;
556
+ /// }
557
+ /// ```
558
+ ///
559
+ /// ```rust,ignore
560
+ /// snippet_block(cx, block.span, "..", Some(if_expr.span))
561
+ /// // where, `block` is the block of the if expr
562
+ /// if x {
563
+ /// y;
564
+ /// }
565
+ /// // will return the snippet
566
+ /// {
567
+ /// y;
568
+ /// } // aligned with `if`
546
569
/// ```
570
+ /// Note that the first line of the snippet always has 0 indentation.
547
571
pub fn snippet_block < ' a , T : LintContext > (
548
572
cx : & T ,
549
573
span : Span ,
0 commit comments