]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/internal_lints.rs
Rename TypeckTables to TypeckResults.
[rust.git] / clippy_lints / src / utils / internal_lints.rs
1 use crate::utils::SpanlessEq;
2 use crate::utils::{
3     is_expn_of, match_def_path, match_qpath, match_type, method_calls, paths, run_lints, snippet, span_lint,
4     span_lint_and_help, span_lint_and_sugg, walk_ptrs_ty,
5 };
6 use if_chain::if_chain;
7 use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, NodeId};
8 use rustc_ast::visit::FnKind;
9 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
10 use rustc_errors::Applicability;
11 use rustc_hir as hir;
12 use rustc_hir::def::{DefKind, Res};
13 use rustc_hir::hir_id::CRATE_HIR_ID;
14 use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
15 use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Path, StmtKind, Ty, TyKind};
16 use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
17 use rustc_middle::hir::map::Map;
18 use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
19 use rustc_span::source_map::{Span, Spanned};
20 use rustc_span::symbol::{Symbol, SymbolStr};
21
22 use std::borrow::{Borrow, Cow};
23
24 declare_clippy_lint! {
25     /// **What it does:** Checks for various things we like to keep tidy in clippy.
26     ///
27     /// **Why is this bad?** We like to pretend we're an example of tidy code.
28     ///
29     /// **Known problems:** None.
30     ///
31     /// **Example:** Wrong ordering of the util::paths constants.
32     pub CLIPPY_LINTS_INTERNAL,
33     internal,
34     "various things that will negatively affect your clippy experience"
35 }
36
37 declare_clippy_lint! {
38     /// **What it does:** Ensures every lint is associated to a `LintPass`.
39     ///
40     /// **Why is this bad?** The compiler only knows lints via a `LintPass`. Without
41     /// putting a lint to a `LintPass::get_lints()`'s return, the compiler will not
42     /// know the name of the lint.
43     ///
44     /// **Known problems:** Only checks for lints associated using the
45     /// `declare_lint_pass!`, `impl_lint_pass!`, and `lint_array!` macros.
46     ///
47     /// **Example:**
48     /// ```rust,ignore
49     /// declare_lint! { pub LINT_1, ... }
50     /// declare_lint! { pub LINT_2, ... }
51     /// declare_lint! { pub FORGOTTEN_LINT, ... }
52     /// // ...
53     /// declare_lint_pass!(Pass => [LINT_1, LINT_2]);
54     /// // missing FORGOTTEN_LINT
55     /// ```
56     pub LINT_WITHOUT_LINT_PASS,
57     internal,
58     "declaring a lint without associating it in a LintPass"
59 }
60
61 declare_clippy_lint! {
62     /// **What it does:** Checks for calls to `cx.span_lint*` and suggests to use the `utils::*`
63     /// variant of the function.
64     ///
65     /// **Why is this bad?** The `utils::*` variants also add a link to the Clippy documentation to the
66     /// warning/error messages.
67     ///
68     /// **Known problems:** None.
69     ///
70     /// **Example:**
71     /// Bad:
72     /// ```rust,ignore
73     /// cx.span_lint(LINT_NAME, "message");
74     /// ```
75     ///
76     /// Good:
77     /// ```rust,ignore
78     /// utils::span_lint(cx, LINT_NAME, "message");
79     /// ```
80     pub COMPILER_LINT_FUNCTIONS,
81     internal,
82     "usage of the lint functions of the compiler instead of the utils::* variant"
83 }
84
85 declare_clippy_lint! {
86     /// **What it does:** Checks for calls to `cx.outer().expn_data()` and suggests to use
87     /// the `cx.outer_expn_data()`
88     ///
89     /// **Why is this bad?** `cx.outer_expn_data()` is faster and more concise.
90     ///
91     /// **Known problems:** None.
92     ///
93     /// **Example:**
94     /// Bad:
95     /// ```rust,ignore
96     /// expr.span.ctxt().outer().expn_data()
97     /// ```
98     ///
99     /// Good:
100     /// ```rust,ignore
101     /// expr.span.ctxt().outer_expn_data()
102     /// ```
103     pub OUTER_EXPN_EXPN_DATA,
104     internal,
105     "using `cx.outer_expn().expn_data()` instead of `cx.outer_expn_data()`"
106 }
107
108 declare_clippy_lint! {
109     /// **What it does:** Not an actual lint. This lint is only meant for testing our customized internal compiler
110     /// error message by calling `panic`.
111     ///
112     /// **Why is this bad?** ICE in large quantities can damage your teeth
113     ///
114     /// **Known problems:** None
115     ///
116     /// **Example:**
117     /// Bad:
118     /// ```rust,ignore
119     /// 🍦🍦🍦🍦🍦
120     /// ```
121     pub PRODUCE_ICE,
122     internal,
123     "this message should not appear anywhere as we ICE before and don't emit the lint"
124 }
125
126 declare_clippy_lint! {
127     /// **What it does:** Checks for cases of an auto-generated lint without an updated description,
128     /// i.e. `default lint description`.
129     ///
130     /// **Why is this bad?** Indicates that the lint is not finished.
131     ///
132     /// **Known problems:** None
133     ///
134     /// **Example:**
135     /// Bad:
136     /// ```rust,ignore
137     /// declare_lint! { pub COOL_LINT, nursery, "default lint description" }
138     /// ```
139     ///
140     /// Good:
141     /// ```rust,ignore
142     /// declare_lint! { pub COOL_LINT, nursery, "a great new lint" }
143     /// ```
144     pub DEFAULT_LINT,
145     internal,
146     "found 'default lint description' in a lint declaration"
147 }
148
149 declare_clippy_lint! {
150     /// **What it does:** Lints `span_lint_and_then` function calls, where the
151     /// closure argument has only one statement and that statement is a method
152     /// call to `span_suggestion`, `span_help`, `span_note` (using the same
153     /// span), `help` or `note`.
154     ///
155     /// These usages of `span_lint_and_then` should be replaced with one of the
156     /// wrapper functions `span_lint_and_sugg`, span_lint_and_help`, or
157     /// `span_lint_and_note`.
158     ///
159     /// **Why is this bad?** Using the wrapper `span_lint_and_*` functions, is more
160     /// convenient, readable and less error prone.
161     ///
162     /// **Known problems:** None
163     ///
164     /// *Example:**
165     /// Bad:
166     /// ```rust,ignore
167     /// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
168     ///     diag.span_suggestion(
169     ///         expr.span,
170     ///         help_msg,
171     ///         sugg.to_string(),
172     ///         Applicability::MachineApplicable,
173     ///     );
174     /// });
175     /// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
176     ///     diag.span_help(expr.span, help_msg);
177     /// });
178     /// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
179     ///     diag.help(help_msg);
180     /// });
181     /// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
182     ///     diag.span_note(expr.span, note_msg);
183     /// });
184     /// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
185     ///     diag.note(note_msg);
186     /// });
187     /// ```
188     ///
189     /// Good:
190     /// ```rust,ignore
191     /// span_lint_and_sugg(
192     ///     cx,
193     ///     TEST_LINT,
194     ///     expr.span,
195     ///     lint_msg,
196     ///     help_msg,
197     ///     sugg.to_string(),
198     ///     Applicability::MachineApplicable,
199     /// );
200     /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg);
201     /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg);
202     /// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg);
203     /// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, None, note_msg);
204     /// ```
205     pub COLLAPSIBLE_SPAN_LINT_CALLS,
206     internal,
207     "found collapsible `span_lint_and_then` calls"
208 }
209
210 declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);
211
212 impl EarlyLintPass for ClippyLintsInternal {
213     fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &AstCrate) {
214         if let Some(utils) = krate
215             .module
216             .items
217             .iter()
218             .find(|item| item.ident.name.as_str() == "utils")
219         {
220             if let ItemKind::Mod(ref utils_mod) = utils.kind {
221                 if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name.as_str() == "paths") {
222                     if let ItemKind::Mod(ref paths_mod) = paths.kind {
223                         let mut last_name: Option<SymbolStr> = None;
224                         for item in &*paths_mod.items {
225                             let name = item.ident.as_str();
226                             if let Some(ref last_name) = last_name {
227                                 if **last_name > *name {
228                                     span_lint(
229                                         cx,
230                                         CLIPPY_LINTS_INTERNAL,
231                                         item.span,
232                                         "this constant should be before the previous constant due to lexical \
233                                          ordering",
234                                     );
235                                 }
236                             }
237                             last_name = Some(name);
238                         }
239                     }
240                 }
241             }
242         }
243     }
244 }
245
246 #[derive(Clone, Debug, Default)]
247 pub struct LintWithoutLintPass {
248     declared_lints: FxHashMap<Symbol, Span>,
249     registered_lints: FxHashSet<Symbol>,
250 }
251
252 impl_lint_pass!(LintWithoutLintPass => [DEFAULT_LINT, LINT_WITHOUT_LINT_PASS]);
253
254 impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass {
255     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
256         if !run_lints(cx, &[DEFAULT_LINT], item.hir_id) {
257             return;
258         }
259
260         if let hir::ItemKind::Static(ref ty, Mutability::Not, body_id) = item.kind {
261             if is_lint_ref_type(cx, ty) {
262                 let expr = &cx.tcx.hir().body(body_id).value;
263                 if_chain! {
264                     if let ExprKind::AddrOf(_, _, ref inner_exp) = expr.kind;
265                     if let ExprKind::Struct(_, ref fields, _) = inner_exp.kind;
266                     let field = fields
267                         .iter()
268                         .find(|f| f.ident.as_str() == "desc")
269                         .expect("lints must have a description field");
270                     if let ExprKind::Lit(Spanned {
271                         node: LitKind::Str(ref sym, _),
272                         ..
273                     }) = field.expr.kind;
274                     if sym.as_str() == "default lint description";
275
276                     then {
277                         span_lint(
278                             cx,
279                             DEFAULT_LINT,
280                             item.span,
281                             &format!("the lint `{}` has the default lint description", item.ident.name),
282                         );
283                     }
284                 }
285                 self.declared_lints.insert(item.ident.name, item.span);
286             }
287         } else if is_expn_of(item.span, "impl_lint_pass").is_some()
288             || is_expn_of(item.span, "declare_lint_pass").is_some()
289         {
290             if let hir::ItemKind::Impl {
291                 of_trait: None,
292                 items: ref impl_item_refs,
293                 ..
294             } = item.kind
295             {
296                 let mut collector = LintCollector {
297                     output: &mut self.registered_lints,
298                     cx,
299                 };
300                 let body_id = cx.tcx.hir().body_owned_by(
301                     impl_item_refs
302                         .iter()
303                         .find(|iiref| iiref.ident.as_str() == "get_lints")
304                         .expect("LintPass needs to implement get_lints")
305                         .id
306                         .hir_id,
307                 );
308                 collector.visit_expr(&cx.tcx.hir().body(body_id).value);
309             }
310         }
311     }
312
313     fn check_crate_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Crate<'_>) {
314         if !run_lints(cx, &[LINT_WITHOUT_LINT_PASS], CRATE_HIR_ID) {
315             return;
316         }
317
318         for (lint_name, &lint_span) in &self.declared_lints {
319             // When using the `declare_tool_lint!` macro, the original `lint_span`'s
320             // file points to "<rustc macros>".
321             // `compiletest-rs` thinks that's an error in a different file and
322             // just ignores it. This causes the test in compile-fail/lint_pass
323             // not able to capture the error.
324             // Therefore, we need to climb the macro expansion tree and find the
325             // actual span that invoked `declare_tool_lint!`:
326             let lint_span = lint_span.ctxt().outer_expn_data().call_site;
327
328             if !self.registered_lints.contains(lint_name) {
329                 span_lint(
330                     cx,
331                     LINT_WITHOUT_LINT_PASS,
332                     lint_span,
333                     &format!("the lint `{}` is not added to any `LintPass`", lint_name),
334                 );
335             }
336         }
337     }
338 }
339
340 fn is_lint_ref_type<'tcx>(cx: &LateContext<'tcx>, ty: &Ty<'_>) -> bool {
341     if let TyKind::Rptr(
342         _,
343         MutTy {
344             ty: ref inner,
345             mutbl: Mutability::Not,
346         },
347     ) = ty.kind
348     {
349         if let TyKind::Path(ref path) = inner.kind {
350             if let Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, inner.hir_id) {
351                 return match_def_path(cx, def_id, &paths::LINT);
352             }
353         }
354     }
355
356     false
357 }
358
359 struct LintCollector<'a, 'tcx> {
360     output: &'a mut FxHashSet<Symbol>,
361     cx: &'a LateContext<'tcx>,
362 }
363
364 impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> {
365     type Map = Map<'tcx>;
366
367     fn visit_path(&mut self, path: &'tcx Path<'_>, _: HirId) {
368         if path.segments.len() == 1 {
369             self.output.insert(path.segments[0].ident.name);
370         }
371     }
372
373     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
374         NestedVisitorMap::All(self.cx.tcx.hir())
375     }
376 }
377
378 #[derive(Clone, Default)]
379 pub struct CompilerLintFunctions {
380     map: FxHashMap<&'static str, &'static str>,
381 }
382
383 impl CompilerLintFunctions {
384     #[must_use]
385     pub fn new() -> Self {
386         let mut map = FxHashMap::default();
387         map.insert("span_lint", "utils::span_lint");
388         map.insert("struct_span_lint", "utils::span_lint");
389         map.insert("lint", "utils::span_lint");
390         map.insert("span_lint_note", "utils::span_lint_and_note");
391         map.insert("span_lint_help", "utils::span_lint_and_help");
392         Self { map }
393     }
394 }
395
396 impl_lint_pass!(CompilerLintFunctions => [COMPILER_LINT_FUNCTIONS]);
397
398 impl<'tcx> LateLintPass<'tcx> for CompilerLintFunctions {
399     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
400         if !run_lints(cx, &[COMPILER_LINT_FUNCTIONS], expr.hir_id) {
401             return;
402         }
403
404         if_chain! {
405             if let ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind;
406             let fn_name = path.ident;
407             if let Some(sugg) = self.map.get(&*fn_name.as_str());
408             let ty = walk_ptrs_ty(cx.typeck_results().expr_ty(&args[0]));
409             if match_type(cx, ty, &paths::EARLY_CONTEXT)
410                 || match_type(cx, ty, &paths::LATE_CONTEXT);
411             then {
412                 span_lint_and_help(
413                     cx,
414                     COMPILER_LINT_FUNCTIONS,
415                     path.ident.span,
416                     "usage of a compiler lint function",
417                     None,
418                     &format!("please use the Clippy variant of this function: `{}`", sugg),
419                 );
420             }
421         }
422     }
423 }
424
425 declare_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]);
426
427 impl<'tcx> LateLintPass<'tcx> for OuterExpnDataPass {
428     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
429         if !run_lints(cx, &[OUTER_EXPN_EXPN_DATA], expr.hir_id) {
430             return;
431         }
432
433         let (method_names, arg_lists, spans) = method_calls(expr, 2);
434         let method_names: Vec<SymbolStr> = method_names.iter().map(|s| s.as_str()).collect();
435         let method_names: Vec<&str> = method_names.iter().map(|s| &**s).collect();
436         if_chain! {
437             if let ["expn_data", "outer_expn"] = method_names.as_slice();
438             let args = arg_lists[1];
439             if args.len() == 1;
440             let self_arg = &args[0];
441             let self_ty = walk_ptrs_ty(cx.typeck_results().expr_ty(self_arg));
442             if match_type(cx, self_ty, &paths::SYNTAX_CONTEXT);
443             then {
444                 span_lint_and_sugg(
445                     cx,
446                     OUTER_EXPN_EXPN_DATA,
447                     spans[1].with_hi(expr.span.hi()),
448                     "usage of `outer_expn().expn_data()`",
449                     "try",
450                     "outer_expn_data()".to_string(),
451                     Applicability::MachineApplicable,
452                 );
453             }
454         }
455     }
456 }
457
458 declare_lint_pass!(ProduceIce => [PRODUCE_ICE]);
459
460 impl EarlyLintPass for ProduceIce {
461     fn check_fn(&mut self, _: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
462         if is_trigger_fn(fn_kind) {
463             panic!("Would you like some help with that?");
464         }
465     }
466 }
467
468 fn is_trigger_fn(fn_kind: FnKind<'_>) -> bool {
469     match fn_kind {
470         FnKind::Fn(_, ident, ..) => ident.name.as_str() == "it_looks_like_you_are_trying_to_kill_clippy",
471         FnKind::Closure(..) => false,
472     }
473 }
474
475 declare_lint_pass!(CollapsibleCalls => [COLLAPSIBLE_SPAN_LINT_CALLS]);
476
477 impl<'tcx> LateLintPass<'tcx> for CollapsibleCalls {
478     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
479         if !run_lints(cx, &[COLLAPSIBLE_SPAN_LINT_CALLS], expr.hir_id) {
480             return;
481         }
482
483         if_chain! {
484             if let ExprKind::Call(ref func, ref and_then_args) = expr.kind;
485             if let ExprKind::Path(ref path) = func.kind;
486             if match_qpath(path, &["span_lint_and_then"]);
487             if and_then_args.len() == 5;
488             if let ExprKind::Closure(_, _, body_id, _, _) = &and_then_args[4].kind;
489             let body = cx.tcx.hir().body(*body_id);
490             if let ExprKind::Block(block, _) = &body.value.kind;
491             let stmts = &block.stmts;
492             if stmts.len() == 1 && block.expr.is_none();
493             if let StmtKind::Semi(only_expr) = &stmts[0].kind;
494             if let ExprKind::MethodCall(ref ps, _, ref span_call_args, _) = &only_expr.kind;
495             let and_then_snippets = get_and_then_snippets(cx, and_then_args);
496             let mut sle = SpanlessEq::new(cx).ignore_fn();
497             then {
498                 match &*ps.ident.as_str() {
499                     "span_suggestion" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
500                         suggest_suggestion(cx, expr, &and_then_snippets, &span_suggestion_snippets(cx, span_call_args));
501                     },
502                     "span_help" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
503                         let help_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
504                         suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), true);
505                     },
506                     "span_note" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
507                         let note_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
508                         suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), true);
509                     },
510                     "help" => {
511                         let help_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
512                         suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), false);
513                     }
514                     "note" => {
515                         let note_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
516                         suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), false);
517                     }
518                     _  => (),
519                 }
520             }
521         }
522     }
523 }
524
525 struct AndThenSnippets<'a> {
526     cx: Cow<'a, str>,
527     lint: Cow<'a, str>,
528     span: Cow<'a, str>,
529     msg: Cow<'a, str>,
530 }
531
532 fn get_and_then_snippets<'a, 'hir>(cx: &LateContext<'_>, and_then_snippets: &'hir [Expr<'hir>]) -> AndThenSnippets<'a> {
533     let cx_snippet = snippet(cx, and_then_snippets[0].span, "cx");
534     let lint_snippet = snippet(cx, and_then_snippets[1].span, "..");
535     let span_snippet = snippet(cx, and_then_snippets[2].span, "span");
536     let msg_snippet = snippet(cx, and_then_snippets[3].span, r#""...""#);
537
538     AndThenSnippets {
539         cx: cx_snippet,
540         lint: lint_snippet,
541         span: span_snippet,
542         msg: msg_snippet,
543     }
544 }
545
546 struct SpanSuggestionSnippets<'a> {
547     help: Cow<'a, str>,
548     sugg: Cow<'a, str>,
549     applicability: Cow<'a, str>,
550 }
551
552 fn span_suggestion_snippets<'a, 'hir>(
553     cx: &LateContext<'_>,
554     span_call_args: &'hir [Expr<'hir>],
555 ) -> SpanSuggestionSnippets<'a> {
556     let help_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
557     let sugg_snippet = snippet(cx, span_call_args[3].span, "..");
558     let applicability_snippet = snippet(cx, span_call_args[4].span, "Applicability::MachineApplicable");
559
560     SpanSuggestionSnippets {
561         help: help_snippet,
562         sugg: sugg_snippet,
563         applicability: applicability_snippet,
564     }
565 }
566
567 fn suggest_suggestion(
568     cx: &LateContext<'_>,
569     expr: &Expr<'_>,
570     and_then_snippets: &AndThenSnippets<'_>,
571     span_suggestion_snippets: &SpanSuggestionSnippets<'_>,
572 ) {
573     span_lint_and_sugg(
574         cx,
575         COLLAPSIBLE_SPAN_LINT_CALLS,
576         expr.span,
577         "this call is collapsible",
578         "collapse into",
579         format!(
580             "span_lint_and_sugg({}, {}, {}, {}, {}, {}, {})",
581             and_then_snippets.cx,
582             and_then_snippets.lint,
583             and_then_snippets.span,
584             and_then_snippets.msg,
585             span_suggestion_snippets.help,
586             span_suggestion_snippets.sugg,
587             span_suggestion_snippets.applicability
588         ),
589         Applicability::MachineApplicable,
590     );
591 }
592
593 fn suggest_help(
594     cx: &LateContext<'_>,
595     expr: &Expr<'_>,
596     and_then_snippets: &AndThenSnippets<'_>,
597     help: &str,
598     with_span: bool,
599 ) {
600     let option_span = if with_span {
601         format!("Some({})", and_then_snippets.span)
602     } else {
603         "None".to_string()
604     };
605
606     span_lint_and_sugg(
607         cx,
608         COLLAPSIBLE_SPAN_LINT_CALLS,
609         expr.span,
610         "this call is collapsible",
611         "collapse into",
612         format!(
613             "span_lint_and_help({}, {}, {}, {}, {}, {})",
614             and_then_snippets.cx,
615             and_then_snippets.lint,
616             and_then_snippets.span,
617             and_then_snippets.msg,
618             &option_span,
619             help
620         ),
621         Applicability::MachineApplicable,
622     );
623 }
624
625 fn suggest_note(
626     cx: &LateContext<'_>,
627     expr: &Expr<'_>,
628     and_then_snippets: &AndThenSnippets<'_>,
629     note: &str,
630     with_span: bool,
631 ) {
632     let note_span = if with_span {
633         format!("Some({})", and_then_snippets.span)
634     } else {
635         "None".to_string()
636     };
637
638     span_lint_and_sugg(
639         cx,
640         COLLAPSIBLE_SPAN_LINT_CALLS,
641         expr.span,
642         "this call is collspible",
643         "collapse into",
644         format!(
645             "span_lint_and_note({}, {}, {}, {}, {}, {})",
646             and_then_snippets.cx,
647             and_then_snippets.lint,
648             and_then_snippets.span,
649             and_then_snippets.msg,
650             note_span,
651             note
652         ),
653         Applicability::MachineApplicable,
654     );
655 }