Skip to content

Commit ff1b1a7

Browse files
committed
Document some span_lint_* util functions
1 parent 848116b commit ff1b1a7

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

clippy_lints/src/utils/mod.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,45 @@ impl<'a> DiagnosticWrapper<'a> {
634634
}
635635
}
636636

637+
/// Emit a basic lint message with a `msg` and a `span`.
638+
///
639+
/// This is the most primitive of our lint emission methods and can
640+
/// be a good way to get a new lint started.
641+
///
642+
/// Usually it's nicer to provide more context for lint messages.
643+
/// Be sure the output is understandable when you use this method.
644+
///
645+
/// # Example
646+
///
647+
/// ```ignore
648+
/// error: usage of mem::forget on Drop type
649+
/// --> $DIR/mem_forget.rs:17:5
650+
/// |
651+
/// 17 | std::mem::forget(seven);
652+
/// | ^^^^^^^^^^^^^^^^^^^^^^^
653+
/// ```
637654
pub fn span_lint<'a, T: LintContext<'a>>(cx: &T, lint: &'static Lint, sp: Span, msg: &str) {
638655
DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)).docs_link(lint);
639656
}
640657

658+
/// Same as `span_lint` but with an extra `help` message.
659+
///
660+
/// Use this if you want to provide some general help but
661+
/// can't provide a specific machine applicable suggestion.
662+
///
663+
/// The `help` message is not attached to any `Span`.
664+
///
665+
/// # Example
666+
///
667+
/// ```ignore
668+
/// error: constant division of 0.0 with 0.0 will always result in NaN
669+
/// --> $DIR/zero_div_zero.rs:6:25
670+
/// |
671+
/// 6 | let other_f64_nan = 0.0f64 / 0.0;
672+
/// | ^^^^^^^^^^^^
673+
/// |
674+
/// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN
675+
/// ```
641676
pub fn span_help_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
642677
cx: &'a T,
643678
lint: &'static Lint,
@@ -650,6 +685,27 @@ pub fn span_help_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
650685
db.docs_link(lint);
651686
}
652687

688+
/// Like `span_lint` but with a `note` section instead of a `help` message.
689+
///
690+
/// The `note` message is presented separately from the main lint message
691+
/// and is attached to a specific span:
692+
///
693+
/// # Example
694+
///
695+
/// ```ignore
696+
/// error: calls to `std::mem::forget` with a reference instead of an owned value. Forgetting a reference does nothing.
697+
/// --> $DIR/drop_forget_ref.rs:10:5
698+
/// |
699+
/// 10 | forget(&SomeStruct);
700+
/// | ^^^^^^^^^^^^^^^^^^^
701+
/// |
702+
/// = note: `-D clippy::forget-ref` implied by `-D warnings`
703+
/// note: argument has type &SomeStruct
704+
/// --> $DIR/drop_forget_ref.rs:10:12
705+
/// |
706+
/// 10 | forget(&SomeStruct);
707+
/// | ^^^^^^^^^^^
708+
/// ```
653709
pub fn span_note_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
654710
cx: &'a T,
655711
lint: &'static Lint,

0 commit comments

Comments
 (0)