Skip to content

Commit d134ddd

Browse files
committed
Improve clippy_utils function docs
1 parent 1011e08 commit d134ddd

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

clippy_utils/src/source.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,22 @@ fn reindent_multiline_inner(s: &str, ignore_first: bool, indent: Option<usize>,
155155
.join("\n")
156156
}
157157

158-
/// Converts a span to a code snippet if available, otherwise use default.
158+
/// Converts a span to a code snippet if available, otherwise returns the default.
159159
///
160160
/// This is useful if you want to provide suggestions for your lint or more generally, if you want
161-
/// to convert a given `Span` to a `str`.
161+
/// to convert a given `Span` to a `str`. To create suggestions consider using
162+
/// [`snippet_with_applicability`] to ensure that the applicability stays correct.
162163
///
163164
/// # Example
164165
/// ```rust,ignore
165-
/// snippet(cx, expr.span, "..")
166+
/// // Given two spans one for `value` and one for the `init` expression.
167+
/// let value = Vec::new();
168+
/// // ^^^^^ ^^^^^^^^^^
169+
/// // span1 span2
170+
///
171+
/// // The snipped call would return the corresponding code snippet
172+
/// snippet(cx, span1, "..") // -> "value"
173+
/// snippet(cx, span2, "..") // -> "Vec::new()"
166174
/// ```
167175
pub fn snippet<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
168176
snippet_opt(cx, span).map_or_else(|| Cow::Borrowed(default), From::from)

clippy_utils/src/ty.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<
114114

115115
/// Checks whether a type implements a trait.
116116
/// The function returns false in case the type contains an inference variable.
117-
/// See also [`get_trait_def_id`](super::get_trait_def_id).
117+
///
118+
/// See:
119+
/// * [`get_trait_def_id`](super::get_trait_def_id) to get a trait [`DefId`].
120+
/// * [Common tools for writing lints] for an example how to use this function and other options.
121+
///
122+
/// [Common tools for writing lints]: https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#checking-if-a-type-implements-a-specific-trait
118123
pub fn implements_trait<'tcx>(
119124
cx: &LateContext<'tcx>,
120125
ty: Ty<'tcx>,
@@ -254,9 +259,17 @@ pub fn is_type_ref_to_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_ite
254259
}
255260
}
256261

257-
/// Checks if the type is equal to a diagnostic item
262+
/// Checks if the type is equal to a diagnostic item. To check if a type implements a
263+
/// trait marked with a diagnostic item use [`implements_trait`].
264+
///
265+
/// For a further exploitation what diagnostic items are see [diagnostic items] in
266+
/// rustc-dev-guide.
267+
///
268+
/// ---
258269
///
259270
/// If you change the signature, remember to update the internal lint `MatchTypeOnDiagItem`
271+
///
272+
/// [Diagnostic Items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
260273
pub fn is_type_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
261274
match ty.kind() {
262275
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),

0 commit comments

Comments
 (0)