1 //! Clippy wrappers around rustc's diagnostic functions.
4 use rustc::lint::{LateContext, Lint, LintContext};
5 use rustc_errors::{Applicability, CodeSuggestion, Substitution, SubstitutionPart, SuggestionStyle};
7 use syntax::errors::DiagnosticBuilder;
8 use syntax::source_map::{MultiSpan, Span};
10 /// Wrapper around `DiagnosticBuilder` that adds a link to Clippy documentation for the emitted lint
11 struct DiagnosticWrapper<'a>(DiagnosticBuilder<'a>);
13 impl<'a> Drop for DiagnosticWrapper<'a> {
19 impl<'a> DiagnosticWrapper<'a> {
20 fn docs_link(&mut self, lint: &'static Lint) {
21 if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() {
23 "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}",
24 &option_env!("RUST_RELEASE_NUM").map_or("master".to_string(), |n| {
25 // extract just major + minor version and ignore patch versions
26 format!("rust-{}", n.rsplitn(2, '.').nth(1).unwrap())
28 lint.name_lower().replacen("clippy::", "", 1)
34 /// Emit a basic lint message with a `msg` and a `span`.
36 /// This is the most primitive of our lint emission methods and can
37 /// be a good way to get a new lint started.
39 /// Usually it's nicer to provide more context for lint messages.
40 /// Be sure the output is understandable when you use this method.
45 /// error: usage of mem::forget on Drop type
46 /// --> $DIR/mem_forget.rs:17:5
48 /// 17 | std::mem::forget(seven);
49 /// | ^^^^^^^^^^^^^^^^^^^^^^^
51 pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
52 DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)).docs_link(lint);
55 /// Same as `span_lint` but with an extra `help` message.
57 /// Use this if you want to provide some general help but
58 /// can't provide a specific machine applicable suggestion.
60 /// The `help` message is not attached to any `Span`.
65 /// error: constant division of 0.0 with 0.0 will always result in NaN
66 /// --> $DIR/zero_div_zero.rs:6:25
68 /// 6 | let other_f64_nan = 0.0f64 / 0.0;
71 /// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN
73 pub fn span_help_and_lint<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {
74 let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg));
79 /// Like `span_lint` but with a `note` section instead of a `help` message.
81 /// The `note` message is presented separately from the main lint message
82 /// and is attached to a specific span:
87 /// error: calls to `std::mem::forget` with a reference instead of an owned value. Forgetting a reference does nothing.
88 /// --> $DIR/drop_forget_ref.rs:10:5
90 /// 10 | forget(&SomeStruct);
91 /// | ^^^^^^^^^^^^^^^^^^^
93 /// = note: `-D clippy::forget-ref` implied by `-D warnings`
94 /// note: argument has type &SomeStruct
95 /// --> $DIR/drop_forget_ref.rs:10:12
97 /// 10 | forget(&SomeStruct);
100 pub fn span_note_and_lint<'a, T: LintContext>(
108 let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg));
109 if note_span == span {
112 db.0.span_note(note_span, note);
117 pub fn span_lint_and_then<'a, T: LintContext, F>(cx: &'a T, lint: &'static Lint, sp: Span, msg: &str, f: F)
119 F: for<'b> FnOnce(&mut DiagnosticBuilder<'b>),
121 let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg));
126 pub fn span_lint_hir(cx: &LateContext<'_, '_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
127 DiagnosticWrapper(cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg)).docs_link(lint);
130 pub fn span_lint_hir_and_then(
131 cx: &LateContext<'_, '_>,
136 f: impl FnOnce(&mut DiagnosticBuilder<'_>),
138 let mut db = DiagnosticWrapper(cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg));
143 /// Add a span lint with a suggestion on how to fix it.
145 /// These suggestions can be parsed by rustfix to allow it to automatically fix your code.
146 /// In the example below, `help` is `"try"` and `sugg` is the suggested replacement `".any(|x| x >
150 /// error: This `.fold` can be more succinctly expressed as `.any`
151 /// --> $DIR/methods.rs:390:13
153 /// 390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
154 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
156 /// = note: `-D fold-any` implied by `-D warnings`
158 pub fn span_lint_and_sugg<'a, T: LintContext>(
165 applicability: Applicability,
167 span_lint_and_then(cx, lint, sp, msg, |db| {
168 db.span_suggestion(sp, help, sugg, applicability);
172 /// Create a suggestion made from several `span → replacement`.
174 /// Note: in the JSON format (used by `compiletest_rs`), the help message will
176 /// replacement. In human-readable format though, it only appears once before
177 /// the whole suggestion.
178 pub fn multispan_sugg<I>(db: &mut DiagnosticBuilder<'_>, help_msg: String, sugg: I)
180 I: IntoIterator<Item = (Span, String)>,
182 let sugg = CodeSuggestion {
183 substitutions: vec![Substitution {
186 .map(|(span, snippet)| SubstitutionPart { snippet, span })
190 style: SuggestionStyle::ShowCode,
191 applicability: Applicability::Unspecified,
193 db.suggestions.push(sugg);