Function clippy_utils::source::snippet

source ·
pub fn snippet<'a, T: LintContext>(
    cx: &T,
    span: Span,
    default: &'a str
) -> Cow<'a, str>
Expand description

Converts a span to a code snippet if available, otherwise returns the default.

This is useful if you want to provide suggestions for your lint or more generally, if you want to convert a given Span to a str. To create suggestions consider using snippet_with_applicability to ensure that the applicability stays correct.

Example

// Given two spans one for `value` and one for the `init` expression.
let value = Vec::new();
//  ^^^^^   ^^^^^^^^^^
//  span1   span2

// The snipped call would return the corresponding code snippet
snippet(cx, span1, "..") // -> "value"
snippet(cx, span2, "..") // -> "Vec::new()"