1 //! Clippy wrappers around rustc's diagnostic functions.
2 //!
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
6 //! or renamed.
7 //!
8 //! Thank you!
9 //! ~The `INTERNAL_METADATA_COLLECTOR` lint
10
11 use rustc_errors::{Applicability, Diagnostic, MultiSpan};
12 use rustc_hir::HirId;
13 use rustc_lint::{LateContext, Lint, LintContext};
14 use rustc_span::source_map::Span;
15 use std::env;
16
docs_link(diag: &mut Diagnostic, lint: &'static Lint)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::") {
20 diag.help(format!(
21 "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{lint}",
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)
25 })
26 ));
27 }
28 }
29 }
30
31 /// Emit a basic lint message with a `msg` and a `span`.
32 ///
33 /// This is the most primitive of our lint emission methods and can
34 /// be a good way to get a new lint started.
35 ///
36 /// Usually it's nicer to provide more context for lint messages.
37 /// Be sure the output is understandable when you use this method.
38 ///
39 /// # Example
40 ///
41 /// ```ignore
42 /// error: usage of mem::forget on Drop type
43 /// --> $DIR/mem_forget.rs:17:5
44 /// |
45 /// 17 | std::mem::forget(seven);
46 /// | ^^^^^^^^^^^^^^^^^^^^^^^
47 /// ```
span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str)48 pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
49 cx.struct_span_lint(lint, sp, msg.to_string(), |diag| {
50 docs_link(diag, lint);
51 diag
52 });
53 }
54
55 /// Same as `span_lint` but with an extra `help` message.
56 ///
57 /// Use this if you want to provide some general help but
58 /// can't provide a specific machine applicable suggestion.
59 ///
60 /// The `help` message can be optionally attached to a `Span`.
61 ///
62 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
63 ///
64 /// # Example
65 ///
66 /// ```text
67 /// error: constant division of 0.0 with 0.0 will always result in NaN
68 /// --> $DIR/zero_div_zero.rs:6:25
69 /// |
70 /// 6 | let other_f64_nan = 0.0f64 / 0.0;
71 /// | ^^^^^^^^^^^^
72 /// |
73 /// = help: consider using `f64::NAN` if you would like a constant representing NaN
74 /// ```
span_lint_and_help<T: LintContext>( cx: &T, lint: &'static Lint, span: impl Into<MultiSpan>, msg: &str, help_span: Option<Span>, help: &str, )75 pub fn span_lint_and_help<T: LintContext>(
76 cx: &T,
77 lint: &'static Lint,
78 span: impl Into<MultiSpan>,
79 msg: &str,
80 help_span: Option<Span>,
81 help: &str,
82 ) {
83 cx.struct_span_lint(lint, span, msg.to_string(), |diag| {
84 let help = help.to_string();
85 if let Some(help_span) = help_span {
86 diag.span_help(help_span, help.to_string());
87 } else {
88 diag.help(help.to_string());
89 }
90 docs_link(diag, lint);
91 diag
92 });
93 }
94
95 /// Like `span_lint` but with a `note` section instead of a `help` message.
96 ///
97 /// The `note` message is presented separately from the main lint message
98 /// and is attached to a specific span:
99 ///
100 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
101 ///
102 /// # Example
103 ///
104 /// ```text
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
107 /// |
108 /// 10 | forget(&SomeStruct);
109 /// | ^^^^^^^^^^^^^^^^^^^
110 /// |
111 /// = note: `-D clippy::forget-ref` implied by `-D warnings`
112 /// note: argument has type &SomeStruct
113 /// --> $DIR/drop_forget_ref.rs:10:12
114 /// |
115 /// 10 | forget(&SomeStruct);
116 /// | ^^^^^^^^^^^
117 /// ```
span_lint_and_note<T: LintContext>( cx: &T, lint: &'static Lint, span: impl Into<MultiSpan>, msg: &str, note_span: Option<Span>, note: &str, )118 pub fn span_lint_and_note<T: LintContext>(
119 cx: &T,
120 lint: &'static Lint,
121 span: impl Into<MultiSpan>,
122 msg: &str,
123 note_span: Option<Span>,
124 note: &str,
125 ) {
126 cx.struct_span_lint(lint, span, msg.to_string(), |diag| {
127 let note = note.to_string();
128 if let Some(note_span) = note_span {
129 diag.span_note(note_span, note);
130 } else {
131 diag.note(note);
132 }
133 docs_link(diag, lint);
134 diag
135 });
136 }
137
138 /// Like `span_lint` but allows to add notes, help and suggestions using a closure.
139 ///
140 /// If you need to customize your lint output a lot, use this function.
141 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str, f: F) where C: LintContext, S: Into<MultiSpan>, F: FnOnce(&mut Diagnostic),142 pub fn span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str, f: F)
143 where
144 C: LintContext,
145 S: Into<MultiSpan>,
146 F: FnOnce(&mut Diagnostic),
147 {
148 cx.struct_span_lint(lint, sp, msg.to_string(), |diag| {
149 f(diag);
150 docs_link(diag, lint);
151 diag
152 });
153 }
154
span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str)155 pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
156 cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| {
157 docs_link(diag, lint);
158 diag
159 });
160 }
161
span_lint_hir_and_then( cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: impl Into<MultiSpan>, msg: &str, f: impl FnOnce(&mut Diagnostic), )162 pub fn span_lint_hir_and_then(
163 cx: &LateContext<'_>,
164 lint: &'static Lint,
165 hir_id: HirId,
166 sp: impl Into<MultiSpan>,
167 msg: &str,
168 f: impl FnOnce(&mut Diagnostic),
169 ) {
170 cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| {
171 f(diag);
172 docs_link(diag, lint);
173 diag
174 });
175 }
176
177 /// Add a span lint with a suggestion on how to fix it.
178 ///
179 /// These suggestions can be parsed by rustfix to allow it to automatically fix your code.
180 /// In the example below, `help` is `"try"` and `sugg` is the suggested replacement `".any(|x| x >
181 /// 2)"`.
182 ///
183 /// If you change the signature, remember to update the internal lint `CollapsibleCalls`
184 ///
185 /// # Example
186 ///
187 /// ```text
188 /// error: This `.fold` can be more succinctly expressed as `.any`
189 /// --> $DIR/methods.rs:390:13
190 /// |
191 /// 390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
192 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
193 /// |
194 /// = note: `-D fold-any` implied by `-D warnings`
195 /// ```
196 #[cfg_attr(feature = "internal", allow(clippy::collapsible_span_lint_calls))]
span_lint_and_sugg<T: LintContext>( cx: &T, lint: &'static Lint, sp: Span, msg: &str, help: &str, sugg: String, applicability: Applicability, )197 pub fn span_lint_and_sugg<T: LintContext>(
198 cx: &T,
199 lint: &'static Lint,
200 sp: Span,
201 msg: &str,
202 help: &str,
203 sugg: String,
204 applicability: Applicability,
205 ) {
206 span_lint_and_then(cx, lint, sp, msg, |diag| {
207 diag.span_suggestion(sp, help.to_string(), sugg, applicability);
208 });
209 }
210
211 /// Create a suggestion made from several `span → replacement`.
212 ///
213 /// Note: in the JSON format (used by `compiletest_rs`), the help message will
214 /// appear once per
215 /// replacement. In human-readable format though, it only appears once before
216 /// the whole suggestion.
multispan_sugg<I>(diag: &mut Diagnostic, help_msg: &str, sugg: I) where I: IntoIterator<Item = (Span, String)>,217 pub fn multispan_sugg<I>(diag: &mut Diagnostic, help_msg: &str, sugg: I)
218 where
219 I: IntoIterator<Item = (Span, String)>,
220 {
221 multispan_sugg_with_applicability(diag, help_msg, Applicability::Unspecified, sugg);
222 }
223
224 /// Create a suggestion made from several `span → replacement`.
225 ///
226 /// rustfix currently doesn't support the automatic application of suggestions with
227 /// multiple spans. This is tracked in issue [rustfix#141](https://github.com/rust-lang/rustfix/issues/141).
228 /// Suggestions with multiple spans will be silently ignored.
multispan_sugg_with_applicability<I>( diag: &mut Diagnostic, help_msg: &str, applicability: Applicability, sugg: I, ) where I: IntoIterator<Item = (Span, String)>,229 pub fn multispan_sugg_with_applicability<I>(
230 diag: &mut Diagnostic,
231 help_msg: &str,
232 applicability: Applicability,
233 sugg: I,
234 ) where
235 I: IntoIterator<Item = (Span, String)>,
236 {
237 diag.multipart_suggestion(help_msg.to_string(), sugg.into_iter().collect(), applicability);
238 }
239