2019-02-24 14:11:05 +01:00
|
|
|
//! Clippy wrappers around rustc's diagnostic functions.
|
2021-05-06 11:51:22 +02:00
|
|
|
//!
|
|
|
|
//! These functions are used by the `INTERNAL_METADATA_COLLECTOR` lint to collect the corresponding
|
|
|
|
//! lint applicability. Please make sure that you update the `LINT_EMISSION_FUNCTIONS` variable in
|
|
|
|
//! `clippy_lints::utils::internal_lints::metadata_collector` when a new function is added
|
|
|
|
//! or renamed.
|
|
|
|
//!
|
|
|
|
//! Thank you!
|
|
|
|
//! ~The `INTERNAL_METADATA_COLLECTOR` lint
|
2019-02-24 14:11:05 +01:00
|
|
|
|
2022-06-19 23:21:14 +04:00
|
|
|
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
|
2020-01-07 01:39:50 +09:00
|
|
|
use rustc_hir::HirId;
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::{LateContext, Lint, LintContext};
|
2023-11-02 14:10:12 +11:00
|
|
|
use rustc_span::Span;
|
2019-02-24 14:11:05 +01:00
|
|
|
use std::env;
|
|
|
|
|
2022-01-23 20:41:46 +00:00
|
|
|
fn docs_link(diag: &mut Diagnostic, lint: &'static Lint) {
|
2020-02-10 15:59:14 +01:00
|
|
|
if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() {
|
2021-03-25 19:29:11 +01:00
|
|
|
if let Some(lint) = lint.name_lower().strip_prefix("clippy::") {
|
2022-12-29 14:28:34 +01:00
|
|
|
diag.help(format!(
|
2022-10-06 09:44:38 +02:00
|
|
|
"for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{lint}",
|
2021-03-25 19:29:11 +01:00
|
|
|
&option_env!("RUST_RELEASE_NUM").map_or("master".to_string(), |n| {
|
|
|
|
// extract just major + minor version and ignore patch versions
|
2021-09-08 16:31:47 +02:00
|
|
|
format!("rust-{}", n.rsplit_once('.').unwrap().1)
|
2022-10-06 09:44:38 +02:00
|
|
|
})
|
2021-03-25 19:29:11 +01:00
|
|
|
));
|
|
|
|
}
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a basic lint message with a `msg` and a `span`.
|
|
|
|
///
|
|
|
|
/// This is the most primitive of our lint emission methods and can
|
|
|
|
/// be a good way to get a new lint started.
|
|
|
|
///
|
|
|
|
/// Usually it's nicer to provide more context for lint messages.
|
|
|
|
/// Be sure the output is understandable when you use this method.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// error: usage of mem::forget on Drop type
|
|
|
|
/// --> $DIR/mem_forget.rs:17:5
|
|
|
|
/// |
|
|
|
|
/// 17 | std::mem::forget(seven);
|
|
|
|
/// | ^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
/// ```
|
2019-06-12 10:07:48 +07:00
|
|
|
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
|
2023-11-16 19:13:24 +01:00
|
|
|
#[expect(clippy::disallowed_methods)]
|
2024-01-16 14:29:28 +11:00
|
|
|
cx.span_lint(lint, sp, msg.to_string(), |diag| {
|
2022-09-16 19:07:01 +04:00
|
|
|
docs_link(diag, lint);
|
2020-02-10 15:59:14 +01:00
|
|
|
});
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as `span_lint` but with an extra `help` message.
|
|
|
|
///
|
|
|
|
/// Use this if you want to provide some general help but
|
|
|
|
/// can't provide a specific machine applicable suggestion.
|
|
|
|
///
|
2020-04-19 23:11:30 +02:00
|
|
|
/// The `help` message can be optionally attached to a `Span`.
|
2019-02-24 14:11:05 +01:00
|
|
|
///
|
2020-09-24 14:49:22 +02:00
|
|
|
/// If you change the signature, remember to update the internal lint `CollapsibleCalls`
|
|
|
|
///
|
2019-02-24 14:11:05 +01:00
|
|
|
/// # Example
|
|
|
|
///
|
2021-08-12 11:16:25 +02:00
|
|
|
/// ```text
|
2019-02-24 14:11:05 +01:00
|
|
|
/// error: constant division of 0.0 with 0.0 will always result in NaN
|
|
|
|
/// --> $DIR/zero_div_zero.rs:6:25
|
|
|
|
/// |
|
|
|
|
/// 6 | let other_f64_nan = 0.0f64 / 0.0;
|
|
|
|
/// | ^^^^^^^^^^^^
|
|
|
|
/// |
|
2021-11-04 12:52:36 +00:00
|
|
|
/// = help: consider using `f64::NAN` if you would like a constant representing NaN
|
2019-02-24 14:11:05 +01:00
|
|
|
/// ```
|
2022-11-21 20:34:47 +01:00
|
|
|
pub fn span_lint_and_help<T: LintContext>(
|
|
|
|
cx: &T,
|
2020-04-18 18:28:29 +08:00
|
|
|
lint: &'static Lint,
|
2022-05-21 13:24:00 +02:00
|
|
|
span: impl Into<MultiSpan>,
|
2020-04-18 18:28:29 +08:00
|
|
|
msg: &str,
|
|
|
|
help_span: Option<Span>,
|
|
|
|
help: &str,
|
|
|
|
) {
|
2023-11-16 19:13:24 +01:00
|
|
|
#[expect(clippy::disallowed_methods)]
|
2024-01-16 14:29:28 +11:00
|
|
|
cx.span_lint(lint, span, msg.to_string(), |diag| {
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 10:55:21 +10:00
|
|
|
let help = help.to_string();
|
2020-04-18 18:28:29 +08:00
|
|
|
if let Some(help_span) = help_span {
|
2020-04-19 20:38:07 +02:00
|
|
|
diag.span_help(help_span, help);
|
2020-04-18 18:28:29 +08:00
|
|
|
} else {
|
2020-04-19 20:38:07 +02:00
|
|
|
diag.help(help);
|
2020-04-18 18:28:29 +08:00
|
|
|
}
|
2022-09-16 19:07:01 +04:00
|
|
|
docs_link(diag, lint);
|
2020-02-10 15:59:14 +01:00
|
|
|
});
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `span_lint` but with a `note` section instead of a `help` message.
|
|
|
|
///
|
|
|
|
/// The `note` message is presented separately from the main lint message
|
|
|
|
/// and is attached to a specific span:
|
|
|
|
///
|
2020-09-24 14:49:22 +02:00
|
|
|
/// If you change the signature, remember to update the internal lint `CollapsibleCalls`
|
|
|
|
///
|
2019-02-24 14:11:05 +01:00
|
|
|
/// # Example
|
|
|
|
///
|
2021-08-12 11:16:25 +02:00
|
|
|
/// ```text
|
2019-02-24 14:11:05 +01:00
|
|
|
/// error: calls to `std::mem::forget` with a reference instead of an owned value. Forgetting a reference does nothing.
|
|
|
|
/// --> $DIR/drop_forget_ref.rs:10:5
|
|
|
|
/// |
|
|
|
|
/// 10 | forget(&SomeStruct);
|
|
|
|
/// | ^^^^^^^^^^^^^^^^^^^
|
|
|
|
/// |
|
|
|
|
/// = note: `-D clippy::forget-ref` implied by `-D warnings`
|
|
|
|
/// note: argument has type &SomeStruct
|
|
|
|
/// --> $DIR/drop_forget_ref.rs:10:12
|
|
|
|
/// |
|
|
|
|
/// 10 | forget(&SomeStruct);
|
|
|
|
/// | ^^^^^^^^^^^
|
|
|
|
/// ```
|
2022-11-21 20:34:47 +01:00
|
|
|
pub fn span_lint_and_note<T: LintContext>(
|
|
|
|
cx: &T,
|
2019-02-24 14:11:05 +01:00
|
|
|
lint: &'static Lint,
|
2021-02-02 20:43:30 -08:00
|
|
|
span: impl Into<MultiSpan>,
|
2019-02-24 14:11:05 +01:00
|
|
|
msg: &str,
|
2020-04-18 18:29:36 +08:00
|
|
|
note_span: Option<Span>,
|
2019-02-24 14:11:05 +01:00
|
|
|
note: &str,
|
|
|
|
) {
|
2023-11-16 19:13:24 +01:00
|
|
|
#[expect(clippy::disallowed_methods)]
|
2024-01-16 14:29:28 +11:00
|
|
|
cx.span_lint(lint, span, msg.to_string(), |diag| {
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 10:55:21 +10:00
|
|
|
let note = note.to_string();
|
2020-04-18 18:29:36 +08:00
|
|
|
if let Some(note_span) = note_span {
|
2020-04-19 20:38:07 +02:00
|
|
|
diag.span_note(note_span, note);
|
2020-02-10 15:59:14 +01:00
|
|
|
} else {
|
2020-04-19 20:38:07 +02:00
|
|
|
diag.note(note);
|
2020-02-10 15:59:14 +01:00
|
|
|
}
|
2022-09-16 19:07:01 +04:00
|
|
|
docs_link(diag, lint);
|
2020-02-10 15:59:14 +01:00
|
|
|
});
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-17 08:08:00 +02:00
|
|
|
/// Like `span_lint` but allows to add notes, help and suggestions using a closure.
|
|
|
|
///
|
|
|
|
/// If you need to customize your lint output a lot, use this function.
|
2020-09-24 14:49:22 +02:00
|
|
|
/// If you change the signature, remember to update the internal lint `CollapsibleCalls`
|
2021-04-08 17:50:13 +02:00
|
|
|
pub fn span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str, f: F)
|
2019-06-12 10:07:48 +07:00
|
|
|
where
|
2021-04-08 17:50:13 +02:00
|
|
|
C: LintContext,
|
|
|
|
S: Into<MultiSpan>,
|
2022-01-23 20:41:46 +00:00
|
|
|
F: FnOnce(&mut Diagnostic),
|
2019-02-24 14:11:05 +01:00
|
|
|
{
|
2023-11-16 19:13:24 +01:00
|
|
|
#[expect(clippy::disallowed_methods)]
|
2024-01-16 14:29:28 +11:00
|
|
|
cx.span_lint(lint, sp, msg.to_string(), |diag| {
|
2022-09-16 19:07:01 +04:00
|
|
|
f(diag);
|
|
|
|
docs_link(diag, lint);
|
2020-02-10 15:59:14 +01:00
|
|
|
});
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 18:43:35 +09:00
|
|
|
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
|
2023-11-16 19:13:24 +01:00
|
|
|
#[expect(clippy::disallowed_methods)]
|
2024-01-16 16:14:33 +11:00
|
|
|
cx.tcx.node_span_lint(lint, hir_id, sp, msg.to_string(), |diag| {
|
2022-09-16 19:07:01 +04:00
|
|
|
docs_link(diag, lint);
|
2020-02-10 15:59:14 +01:00
|
|
|
});
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 08:01:21 +01:00
|
|
|
pub fn span_lint_hir_and_then(
|
2020-06-25 23:41:36 +03:00
|
|
|
cx: &LateContext<'_>,
|
2019-02-24 14:11:05 +01:00
|
|
|
lint: &'static Lint,
|
2019-03-12 08:01:21 +01:00
|
|
|
hir_id: HirId,
|
2021-06-03 08:41:37 +02:00
|
|
|
sp: impl Into<MultiSpan>,
|
2019-02-24 14:11:05 +01:00
|
|
|
msg: &str,
|
2022-01-23 20:41:46 +00:00
|
|
|
f: impl FnOnce(&mut Diagnostic),
|
2019-02-24 14:11:05 +01:00
|
|
|
) {
|
2023-11-16 19:13:24 +01:00
|
|
|
#[expect(clippy::disallowed_methods)]
|
2024-01-16 16:14:33 +11:00
|
|
|
cx.tcx.node_span_lint(lint, hir_id, sp, msg.to_string(), |diag| {
|
2022-09-16 19:07:01 +04:00
|
|
|
f(diag);
|
|
|
|
docs_link(diag, lint);
|
2020-02-10 15:59:14 +01:00
|
|
|
});
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a span lint with a suggestion on how to fix it.
|
|
|
|
///
|
|
|
|
/// These suggestions can be parsed by rustfix to allow it to automatically fix your code.
|
|
|
|
/// In the example below, `help` is `"try"` and `sugg` is the suggested replacement `".any(|x| x >
|
|
|
|
/// 2)"`.
|
|
|
|
///
|
2020-09-24 14:49:22 +02:00
|
|
|
/// If you change the signature, remember to update the internal lint `CollapsibleCalls`
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-08-12 11:16:25 +02:00
|
|
|
/// ```text
|
2019-02-24 14:11:05 +01:00
|
|
|
/// error: This `.fold` can be more succinctly expressed as `.any`
|
|
|
|
/// --> $DIR/methods.rs:390:13
|
|
|
|
/// |
|
|
|
|
/// 390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
|
|
|
|
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
|
|
|
|
/// |
|
|
|
|
/// = note: `-D fold-any` implied by `-D warnings`
|
|
|
|
/// ```
|
2023-11-02 17:35:56 +01:00
|
|
|
#[expect(clippy::collapsible_span_lint_calls)]
|
2022-11-21 20:34:47 +01:00
|
|
|
pub fn span_lint_and_sugg<T: LintContext>(
|
|
|
|
cx: &T,
|
2019-02-24 14:11:05 +01:00
|
|
|
lint: &'static Lint,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
help: &str,
|
|
|
|
sugg: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) {
|
2020-04-17 08:08:00 +02:00
|
|
|
span_lint_and_then(cx, lint, sp, msg, |diag| {
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 10:55:21 +10:00
|
|
|
diag.span_suggestion(sp, help.to_string(), sugg, applicability);
|
2019-02-24 14:11:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a suggestion made from several `span → replacement`.
|
|
|
|
///
|
|
|
|
/// Note: in the JSON format (used by `compiletest_rs`), the help message will
|
|
|
|
/// appear once per
|
|
|
|
/// replacement. In human-readable format though, it only appears once before
|
|
|
|
/// the whole suggestion.
|
2022-01-23 20:41:46 +00:00
|
|
|
pub fn multispan_sugg<I>(diag: &mut Diagnostic, help_msg: &str, sugg: I)
|
2019-02-24 14:11:05 +01:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = (Span, String)>,
|
|
|
|
{
|
2021-06-03 08:41:37 +02:00
|
|
|
multispan_sugg_with_applicability(diag, help_msg, Applicability::Unspecified, sugg);
|
2020-05-17 17:36:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 17:50:13 +02:00
|
|
|
/// Create a suggestion made from several `span → replacement`.
|
|
|
|
///
|
|
|
|
/// rustfix currently doesn't support the automatic application of suggestions with
|
|
|
|
/// multiple spans. This is tracked in issue [rustfix#141](https://github.com/rust-lang/rustfix/issues/141).
|
|
|
|
/// Suggestions with multiple spans will be silently ignored.
|
2020-05-17 17:36:26 +02:00
|
|
|
pub fn multispan_sugg_with_applicability<I>(
|
2022-01-23 20:41:46 +00:00
|
|
|
diag: &mut Diagnostic,
|
2020-05-17 17:36:26 +02:00
|
|
|
help_msg: &str,
|
|
|
|
applicability: Applicability,
|
|
|
|
sugg: I,
|
|
|
|
) where
|
|
|
|
I: IntoIterator<Item = (Span, String)>,
|
|
|
|
{
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 10:55:21 +10:00
|
|
|
diag.multipart_suggestion(help_msg.to_string(), sugg.into_iter().collect(), applicability);
|
2019-02-24 14:11:05 +01:00
|
|
|
}
|