From d134dddf706e16eb876a3b42c0a7f546626e5954 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Thu, 4 Nov 2021 12:09:48 +0100 Subject: Improve `clippy_utils` function docs --- clippy_utils/src/source.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'clippy_utils/src/source.rs') diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs index 789079510c5..7f68cc388eb 100644 --- a/clippy_utils/src/source.rs +++ b/clippy_utils/src/source.rs @@ -155,14 +155,22 @@ fn reindent_multiline_inner(s: &str, ignore_first: bool, indent: Option, .join("\n") } -/// Converts a span to a code snippet if available, otherwise use default. +/// 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 convert a given `Span` to a `str`. To create suggestions consider using +/// [`snippet_with_applicability`] to ensure that the applicability stays correct. /// /// # Example /// ```rust,ignore -/// snippet(cx, expr.span, "..") +/// // 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()" /// ``` pub fn snippet<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> { snippet_opt(cx, span).map_or_else(|| Cow::Borrowed(default), From::from) -- cgit 1.4.1-3-g733a5