Skip to content

Commit d6506cc

Browse files
authored
Rollup merge of #102953 - WaffleLapkin:better_docs_for_decorate_param, r=RalfJung
Improve docs for `struct_lint_level` function. r? ``@RalfJung`` Does this answer your questions?
2 parents 91c7d02 + 39375e1 commit d6506cc

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

compiler/rustc_lint/src/context.rs

+19
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,11 @@ pub trait LintContext: Sized {
574574
fn sess(&self) -> &Session;
575575
fn lints(&self) -> &LintStore;
576576

577+
/// Emit a lint at the appropriate level, with an optional associated span and an existing diagnostic.
578+
///
579+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
580+
///
581+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
577582
fn lookup_with_diagnostics(
578583
&self,
579584
lint: &'static Lint,
@@ -872,6 +877,11 @@ pub trait LintContext: Sized {
872877

873878
// FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
874879
// set the span in their `decorate` function (preferably using set_span).
880+
/// Emit a lint at the appropriate level, with an optional associated span.
881+
///
882+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
883+
///
884+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
875885
fn lookup<S: Into<MultiSpan>>(
876886
&self,
877887
lint: &'static Lint,
@@ -893,6 +903,11 @@ pub trait LintContext: Sized {
893903
self.lookup(lint, Some(span), decorator.msg(), |diag| decorator.decorate_lint(diag));
894904
}
895905

906+
/// Emit a lint at the appropriate level, with an associated span.
907+
///
908+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
909+
///
910+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
896911
fn struct_span_lint<S: Into<MultiSpan>>(
897912
&self,
898913
lint: &'static Lint,
@@ -914,6 +929,10 @@ pub trait LintContext: Sized {
914929
}
915930

916931
/// Emit a lint at the appropriate level, with no associated span.
932+
///
933+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
934+
///
935+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
917936
fn lint(
918937
&self,
919938
lint: &'static Lint,

compiler/rustc_lint/src/levels.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10691069

10701070
/// Used to emit a lint-related diagnostic based on the current state of
10711071
/// this lint context.
1072+
///
1073+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
1074+
///
1075+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
10721076
pub(crate) fn struct_lint(
10731077
&self,
10741078
lint: &'static Lint,

compiler/rustc_middle/src/lint.rs

+33
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,39 @@ 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+
/// The return value of `decorate` is ignored by this function. So what is the
290+
/// point of returning `&'b mut DiagnosticBuilder<'a, ()>`?
291+
///
292+
/// There are 2 reasons for this signature.
293+
///
294+
/// First of all, it prevents accidental use of `.emit()` -- it's clear that the
295+
/// builder will be later used and shouldn't be emitted right away (this is
296+
/// especially important because the old API expected you to call `.emit()` in
297+
/// the closure).
298+
///
299+
/// Second of all, it makes the most common case of adding just a single label
300+
/// /suggestion much nicer, since [`DiagnosticBuilder`] methods return
301+
/// `&mut DiagnosticBuilder`, you can just chain methods, without needed
302+
/// awkward `{ ...; }`:
303+
/// ```ignore pseudo-code
304+
/// struct_lint_level(
305+
/// ...,
306+
/// |lint| lint.span_label(sp, "lbl")
307+
/// // ^^^^^^^^^^^^^^^^^^^^^ returns `&mut DiagnosticBuilder` by default
308+
/// )
309+
/// ```
277310
pub fn struct_lint_level(
278311
sess: &Session,
279312
lint: &'static Lint,

compiler/rustc_middle/src/ty/context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,11 @@ impl<'tcx> TyCtxt<'tcx> {
28232823
})
28242824
}
28252825

2826+
/// Emit a lint at the appropriate level for a hir node, with an associated span.
2827+
///
2828+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
2829+
///
2830+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
28262831
pub fn struct_span_lint_hir(
28272832
self,
28282833
lint: &'static Lint,
@@ -2848,6 +2853,11 @@ impl<'tcx> TyCtxt<'tcx> {
28482853
self.struct_lint_node(lint, id, decorator.msg(), |diag| decorator.decorate_lint(diag))
28492854
}
28502855

2856+
/// Emit a lint at the appropriate level for a hir node.
2857+
///
2858+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
2859+
///
2860+
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
28512861
pub fn struct_lint_node(
28522862
self,
28532863
lint: &'static Lint,

0 commit comments

Comments
 (0)