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