2019-02-24 13:11:05 +00:00
|
|
|
//! Clippy wrappers around rustc's diagnostic functions.
|
|
|
|
|
2020-01-11 00:06:09 +00:00
|
|
|
use rustc_errors::{Applicability, CodeSuggestion, DiagnosticBuilder, Substitution, SubstitutionPart, SuggestionStyle};
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::HirId;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, Lint, LintContext};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::{MultiSpan, Span};
|
2019-02-24 13:11:05 +00:00
|
|
|
use std::env;
|
|
|
|
|
2020-04-17 06:08:00 +00:00
|
|
|
fn docs_link(diag: &mut DiagnosticBuilder<'_>, lint: &'static Lint) {
|
2020-02-10 14:59:14 +00:00
|
|
|
if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.help(&format!(
|
2020-02-10 14:59:14 +00:00
|
|
|
"for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}",
|
|
|
|
&option_env!("RUST_RELEASE_NUM").map_or("master".to_string(), |n| {
|
|
|
|
// extract just major + minor version and ignore patch versions
|
|
|
|
format!("rust-{}", n.rsplitn(2, '.').nth(1).unwrap())
|
|
|
|
}),
|
|
|
|
lint.name_lower().replacen("clippy::", "", 1)
|
|
|
|
));
|
2019-02-24 13:11:05 +00: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 03:07:48 +00:00
|
|
|
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
|
2020-04-17 06:08:00 +00:00
|
|
|
cx.struct_span_lint(lint, sp, |diag| {
|
|
|
|
let mut diag = diag.build(msg);
|
|
|
|
docs_link(&mut diag, lint);
|
|
|
|
diag.emit();
|
2020-02-10 14:59:14 +00:00
|
|
|
});
|
2019-02-24 13:11:05 +00: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.
|
|
|
|
///
|
|
|
|
/// The `help` message is not attached to any `Span`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// 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;
|
|
|
|
/// | ^^^^^^^^^^^^
|
|
|
|
/// |
|
2020-04-07 21:41:00 +00:00
|
|
|
/// = help: Consider using `f64::NAN` if you would like a constant representing NaN
|
2019-02-24 13:11:05 +00:00
|
|
|
/// ```
|
2020-01-27 01:56:22 +00:00
|
|
|
pub fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {
|
2020-04-17 06:08:00 +00:00
|
|
|
cx.struct_span_lint(lint, span, |diag| {
|
|
|
|
let mut diag = diag.build(msg);
|
|
|
|
diag.help(help);
|
|
|
|
docs_link(&mut diag, lint);
|
|
|
|
diag.emit();
|
2020-02-10 14:59:14 +00:00
|
|
|
});
|
2019-02-24 13:11:05 +00: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:
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// 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);
|
|
|
|
/// | ^^^^^^^^^^^
|
|
|
|
/// ```
|
2020-01-27 02:26:42 +00:00
|
|
|
pub fn span_lint_and_note<'a, T: LintContext>(
|
2019-02-24 13:11:05 +00:00
|
|
|
cx: &'a T,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: Span,
|
|
|
|
msg: &str,
|
|
|
|
note_span: Span,
|
|
|
|
note: &str,
|
|
|
|
) {
|
2020-04-17 06:08:00 +00:00
|
|
|
cx.struct_span_lint(lint, span, |diag| {
|
|
|
|
let mut diag = diag.build(msg);
|
2020-02-10 14:59:14 +00:00
|
|
|
if note_span == span {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.note(note);
|
2020-02-10 14:59:14 +00:00
|
|
|
} else {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_note(note_span, note);
|
2020-02-10 14:59:14 +00:00
|
|
|
}
|
2020-04-17 06:08:00 +00:00
|
|
|
docs_link(&mut diag, lint);
|
|
|
|
diag.emit();
|
2020-02-10 14:59:14 +00:00
|
|
|
});
|
2019-02-24 13:11:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 06:08:00 +00: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.
|
2019-06-12 03:07:48 +00:00
|
|
|
pub fn span_lint_and_then<'a, T: LintContext, F>(cx: &'a T, lint: &'static Lint, sp: Span, msg: &str, f: F)
|
|
|
|
where
|
2019-02-24 13:11:05 +00:00
|
|
|
F: for<'b> FnOnce(&mut DiagnosticBuilder<'b>),
|
|
|
|
{
|
2020-04-17 06:08:00 +00:00
|
|
|
cx.struct_span_lint(lint, sp, |diag| {
|
|
|
|
let mut diag = diag.build(msg);
|
|
|
|
f(&mut diag);
|
|
|
|
docs_link(&mut diag, lint);
|
|
|
|
diag.emit();
|
2020-02-10 14:59:14 +00:00
|
|
|
});
|
2019-02-24 13:11:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 07:01:21 +00:00
|
|
|
pub fn span_lint_hir(cx: &LateContext<'_, '_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
|
2020-04-17 06:08:00 +00:00
|
|
|
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |diag| {
|
|
|
|
let mut diag = diag.build(msg);
|
|
|
|
docs_link(&mut diag, lint);
|
|
|
|
diag.emit();
|
2020-02-10 14:59:14 +00:00
|
|
|
});
|
2019-02-24 13:11:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 07:01:21 +00:00
|
|
|
pub fn span_lint_hir_and_then(
|
2019-02-24 13:11:05 +00:00
|
|
|
cx: &LateContext<'_, '_>,
|
|
|
|
lint: &'static Lint,
|
2019-03-12 07:01:21 +00:00
|
|
|
hir_id: HirId,
|
2019-02-24 13:11:05 +00:00
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
f: impl FnOnce(&mut DiagnosticBuilder<'_>),
|
|
|
|
) {
|
2020-04-17 06:08:00 +00:00
|
|
|
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |diag| {
|
|
|
|
let mut diag = diag.build(msg);
|
|
|
|
f(&mut diag);
|
|
|
|
docs_link(&mut diag, lint);
|
|
|
|
diag.emit();
|
2020-02-10 14:59:14 +00:00
|
|
|
});
|
2019-02-24 13:11:05 +00: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)"`.
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// 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`
|
|
|
|
/// ```
|
2019-06-12 03:07:48 +00:00
|
|
|
pub fn span_lint_and_sugg<'a, T: LintContext>(
|
2019-02-24 13:11:05 +00:00
|
|
|
cx: &'a T,
|
|
|
|
lint: &'static Lint,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str,
|
|
|
|
help: &str,
|
|
|
|
sugg: String,
|
|
|
|
applicability: Applicability,
|
|
|
|
) {
|
2020-04-17 06:08:00 +00:00
|
|
|
span_lint_and_then(cx, lint, sp, msg, |diag| {
|
|
|
|
diag.span_suggestion(sp, help, sugg, applicability);
|
2019-02-24 13:11:05 +00: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.
|
2020-04-17 06:08:00 +00:00
|
|
|
pub fn multispan_sugg<I>(diag: &mut DiagnosticBuilder<'_>, help_msg: String, sugg: I)
|
2019-02-24 13:11:05 +00:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = (Span, String)>,
|
|
|
|
{
|
|
|
|
let sugg = CodeSuggestion {
|
|
|
|
substitutions: vec![Substitution {
|
|
|
|
parts: sugg
|
|
|
|
.into_iter()
|
|
|
|
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
|
|
|
.collect(),
|
|
|
|
}],
|
|
|
|
msg: help_msg,
|
|
|
|
style: SuggestionStyle::ShowCode,
|
|
|
|
applicability: Applicability::Unspecified,
|
|
|
|
};
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.suggestions.push(sugg);
|
2019-02-24 13:11:05 +00:00
|
|
|
}
|