Skip to content

Commit f1ac7b5

Browse files
committed
Improve docs for struct_lint_level function.
1 parent c078410 commit f1ac7b5

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

compiler/rustc_middle/src/lint.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,44 @@ pub fn explain_lint_level_source(
274274
}
275275
}
276276

277+
/// The innermost function for emitting lints.
278+
///
279+
/// If you are loocking to implement a lint, look for higher level functions,
280+
/// for example:
281+
/// - [`TyCtxt::emit_spanned_lint`]
282+
/// - [`TyCtxt::struct_span_lint_hir`]
283+
/// - [`TyCtxt::emit_lint`]
284+
/// - [`TyCtxt::struct_lint_node`]
285+
/// - `LintContext::lookup`
286+
///
287+
/// ## `decorate` signature
288+
///
289+
/// Signature of `decorate` may be confusing at first, for instance what's the
290+
/// point of returning `&'b mut DiagnosticBuilder<'a, ()>` if the original can
291+
/// be used instead?
292+
/// ```ignore pseudo-code
293+
/// _ = decorate(&mut diag);
294+
/// /* use `diag` here again */
295+
/// ```
296+
///
297+
/// There 2 reasons for such choice signature.
298+
///
299+
/// First off all, it prevents accidental use `.emit()` -- it's clear that the
300+
/// builder will be later used and shouldn't be emitted right away (this is
301+
/// especially important because the old API expected you to call `.emit()` in
302+
/// the closure).
303+
///
304+
/// Second of all, it makes the most common case of adding just a single label
305+
/// /suggestion much nicer, since [`DiagnosticBuilder`] methods return
306+
/// `&mut DiagnosticBuilder`, you can just chain methods, without needed
307+
/// awkward `{ ...; }`:
308+
/// ```ignore pseudo-code
309+
/// struct_lint_level(
310+
/// ...,
311+
/// |lint| lint.span_label(sp, "lbl")
312+
/// // ^^^^^^^^^^^^^^^^^^^^^ returns `&mut DiagnosticBuilder` by default
313+
/// )
314+
/// ```
277315
pub fn struct_lint_level(
278316
sess: &Session,
279317
lint: &'static Lint,

0 commit comments

Comments
 (0)