1 //! Clippy wrappers around rustc's diagnostic functions.
3 //! These functions are used by the `INTERNAL_METADATA_COLLECTOR` lint to collect the corresponding
4 //! lint applicability. Please make sure that you update the `LINT_EMISSION_FUNCTIONS` variable in
5 //! `clippy_lints::utils::internal_lints::metadata_collector` when a new function is added
9 //! ~The `INTERNAL_METADATA_COLLECTOR` lint
11 use rustc_errors::{Applicability, Diagnostic, MultiSpan};
13 use rustc_lint::{LateContext, Lint, LintContext};
14 use rustc_span::source_map::Span;
17 fn docs_link(diag: &mut Diagnostic, lint: &'static Lint) {
18 if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() {
19 if let Some(lint) = lint.name_lower().strip_prefix("clippy::") {
21 "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}",
22 &option_env!("RUST_RELEASE_NUM").map_or("master".to_string(), |n| {
23 // extract just major + minor version and ignore patch versions
24 format!("rust-{}", n.rsplit_once('.').unwrap().1)
32 /// Emit a basic lint message with a `msg` and a `span`.
34 /// This is the most primitive of our lint emission methods and can
35 /// be a good way to get a new lint started.
37 /// Usually it's nicer to provide more context for lint messages.
38 /// Be sure the output is understandable when you use this method.
43 /// error: usage of mem::forget on Drop type
44 /// --> $DIR/mem_forget.rs:17:5
46 /// 17 | std::mem::forget(seven);
47 /// | ^^^^^^^^^^^^^^^^^^^^^^^
49 pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
50 cx.struct_span_lint(lint, sp, msg, |diag| {
51 docs_link(diag, lint);
56 /// Same as `span_lint` but with an extra `help` message.
58 /// Use this if you want to provide some general help but
59 /// can't provide a specific machine applicable suggestion.
61 /// The `help` message can be optionally attached to a `Span`.
63 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
68 /// error: constant division of 0.0 with 0.0 will always result in NaN
69 /// --> $DIR/zero_div_zero.rs:6:25
71 /// 6 | let other_f64_nan = 0.0f64 / 0.0;
74 /// = help: consider using `f64::NAN` if you would like a constant representing NaN
76 pub fn span_lint_and_help<'a, T: LintContext>(
79 span: impl Into<MultiSpan>,
81 help_span: Option<Span>,
84 cx.struct_span_lint(lint, span, msg, |diag| {
85 if let Some(help_span) = help_span {
86 diag.span_help(help_span, help);
90 docs_link(diag, lint);
95 /// Like `span_lint` but with a `note` section instead of a `help` message.
97 /// The `note` message is presented separately from the main lint message
98 /// and is attached to a specific span:
100 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
105 /// error: calls to `std::mem::forget` with a reference instead of an owned value. Forgetting a reference does nothing.
106 /// --> $DIR/drop_forget_ref.rs:10:5
108 /// 10 | forget(&SomeStruct);
109 /// | ^^^^^^^^^^^^^^^^^^^
111 /// = note: `-D clippy::forget-ref` implied by `-D warnings`
112 /// note: argument has type &SomeStruct
113 /// --> $DIR/drop_forget_ref.rs:10:12
115 /// 10 | forget(&SomeStruct);
118 pub fn span_lint_and_note<'a, T: LintContext>(
121 span: impl Into<MultiSpan>,
123 note_span: Option<Span>,
126 cx.struct_span_lint(lint, span, msg, |diag| {
127 if let Some(note_span) = note_span {
128 diag.span_note(note_span, note);
132 docs_link(diag, lint);
137 /// Like `span_lint` but allows to add notes, help and suggestions using a closure.
139 /// If you need to customize your lint output a lot, use this function.
140 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
141 pub fn span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str, f: F)
145 F: FnOnce(&mut Diagnostic),
147 cx.struct_span_lint(lint, sp, msg, |diag| {
149 docs_link(diag, lint);
154 pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
155 cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg, |diag| {
156 docs_link(diag, lint);
161 pub fn span_lint_hir_and_then(
162 cx: &LateContext<'_>,
165 sp: impl Into<MultiSpan>,
167 f: impl FnOnce(&mut Diagnostic),
169 cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg, |diag| {
171 docs_link(diag, lint);
176 /// Add a span lint with a suggestion on how to fix it.
178 /// These suggestions can be parsed by rustfix to allow it to automatically fix your code.
179 /// In the example below, `help` is `"try"` and `sugg` is the suggested replacement `".any(|x| x >
182 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
187 /// error: This `.fold` can be more succinctly expressed as `.any`
188 /// --> $DIR/methods.rs:390:13
190 /// 390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
191 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
193 /// = note: `-D fold-any` implied by `-D warnings`
195 #[cfg_attr(feature = "internal", allow(clippy::collapsible_span_lint_calls))]
196 pub fn span_lint_and_sugg<'a, T: LintContext>(
203 applicability: Applicability,
205 span_lint_and_then(cx, lint, sp, msg, |diag| {
206 diag.span_suggestion(sp, help, sugg, applicability);
210 /// Create a suggestion made from several `span → replacement`.
212 /// Note: in the JSON format (used by `compiletest_rs`), the help message will
214 /// replacement. In human-readable format though, it only appears once before
215 /// the whole suggestion.
216 pub fn multispan_sugg<I>(diag: &mut Diagnostic, help_msg: &str, sugg: I)
218 I: IntoIterator<Item = (Span, String)>,
220 multispan_sugg_with_applicability(diag, help_msg, Applicability::Unspecified, sugg);
223 /// Create a suggestion made from several `span → replacement`.
225 /// rustfix currently doesn't support the automatic application of suggestions with
226 /// multiple spans. This is tracked in issue [rustfix#141](https://github.com/rust-lang/rustfix/issues/141).
227 /// Suggestions with multiple spans will be silently ignored.
228 pub fn multispan_sugg_with_applicability<I>(
229 diag: &mut Diagnostic,
231 applicability: Applicability,
234 I: IntoIterator<Item = (Span, String)>,
236 diag.multipart_suggestion(help_msg, sugg.into_iter().collect(), applicability);