]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/utils/internal_lints/metadata_collector.rs
2564099f4dbcac56a0d6e78859b255ce531968e7
[rust.git] / src / tools / clippy / clippy_lints / src / utils / internal_lints / metadata_collector.rs
1 //! This lint is used to collect metadata about clippy lints. This metadata is exported as a json
2 //! file and then used to generate the [clippy lint list](https://rust-lang.github.io/rust-clippy/master/index.html)
3 //!
4 //! This module and therefore the entire lint is guarded by a feature flag called `internal`
5 //!
6 //! The module transforms all lint names to ascii lowercase to ensure that we don't have mismatches
7 //! during any comparison or mapping. (Please take care of this, it's not fun to spend time on such
8 //! a simple mistake)
9
10 use crate::renamed_lints::RENAMED_LINTS;
11 use crate::utils::internal_lints::{extract_clippy_version_value, is_lint_ref_type};
12
13 use clippy_utils::diagnostics::span_lint;
14 use clippy_utils::ty::{match_type, walk_ptrs_ty_depth};
15 use clippy_utils::{last_path_segment, match_def_path, match_function_call, match_path, paths};
16 use if_chain::if_chain;
17 use rustc_ast as ast;
18 use rustc_data_structures::fx::FxHashMap;
19 use rustc_hir::{
20     self as hir, def::DefKind, intravisit, intravisit::Visitor, ExprKind, Item, ItemKind, Mutability, QPath,
21 };
22 use rustc_lint::{CheckLintNameResult, LateContext, LateLintPass, LintContext, LintId};
23 use rustc_middle::hir::nested_filter;
24 use rustc_session::{declare_tool_lint, impl_lint_pass};
25 use rustc_span::symbol::Ident;
26 use rustc_span::{sym, Loc, Span, Symbol};
27 use serde::{ser::SerializeStruct, Serialize, Serializer};
28 use std::collections::BinaryHeap;
29 use std::fmt;
30 use std::fmt::Write as _;
31 use std::fs::{self, OpenOptions};
32 use std::io::prelude::*;
33 use std::path::Path;
34 use std::path::PathBuf;
35 use std::process::Command;
36
37 /// This is the output file of the lint collector.
38 const OUTPUT_FILE: &str = "../util/gh-pages/lints.json";
39 /// These lints are excluded from the export.
40 const BLACK_LISTED_LINTS: &[&str] = &["lint_author", "dump_hir", "internal_metadata_collector"];
41 /// These groups will be ignored by the lint group matcher. This is useful for collections like
42 /// `clippy::all`
43 const IGNORED_LINT_GROUPS: [&str; 1] = ["clippy::all"];
44 /// Lints within this group will be excluded from the collection. These groups
45 /// have to be defined without the `clippy::` prefix.
46 const EXCLUDED_LINT_GROUPS: [&str; 1] = ["internal"];
47 /// Collected deprecated lint will be assigned to this group in the JSON output
48 const DEPRECATED_LINT_GROUP_STR: &str = "deprecated";
49 /// This is the lint level for deprecated lints that will be displayed in the lint list
50 const DEPRECATED_LINT_LEVEL: &str = "none";
51 /// This array holds Clippy's lint groups with their corresponding default lint level. The
52 /// lint level for deprecated lints is set in `DEPRECATED_LINT_LEVEL`.
53 const DEFAULT_LINT_LEVELS: &[(&str, &str)] = &[
54     ("correctness", "deny"),
55     ("suspicious", "warn"),
56     ("restriction", "allow"),
57     ("style", "warn"),
58     ("pedantic", "allow"),
59     ("complexity", "warn"),
60     ("perf", "warn"),
61     ("cargo", "allow"),
62     ("nursery", "allow"),
63 ];
64 /// This prefix is in front of the lint groups in the lint store. The prefix will be trimmed
65 /// to only keep the actual lint group in the output.
66 const CLIPPY_LINT_GROUP_PREFIX: &str = "clippy::";
67
68 /// This template will be used to format the configuration section in the lint documentation.
69 /// The `configurations` parameter will be replaced with one or multiple formatted
70 /// `ClippyConfiguration` instances. See `CONFIGURATION_VALUE_TEMPLATE` for further customizations
71 macro_rules! CONFIGURATION_SECTION_TEMPLATE {
72     () => {
73         r#"
74 ### Configuration
75 This lint has the following configuration variables:
76
77 {configurations}
78 "#
79     };
80 }
81 /// This template will be used to format an individual `ClippyConfiguration` instance in the
82 /// lint documentation.
83 ///
84 /// The format function will provide strings for the following parameters: `name`, `ty`, `doc` and
85 /// `default`
86 macro_rules! CONFIGURATION_VALUE_TEMPLATE {
87     () => {
88         "* `{name}`: `{ty}`: {doc} (defaults to `{default}`)\n"
89     };
90 }
91
92 macro_rules! RENAMES_SECTION_TEMPLATE {
93     () => {
94         r#"
95 ### Past names
96
97 {names}
98 "#
99     };
100 }
101 macro_rules! RENAME_VALUE_TEMPLATE {
102     () => {
103         "* `{name}`\n"
104     };
105 }
106
107 const LINT_EMISSION_FUNCTIONS: [&[&str]; 8] = [
108     &["clippy_utils", "diagnostics", "span_lint"],
109     &["clippy_utils", "diagnostics", "span_lint_and_help"],
110     &["clippy_utils", "diagnostics", "span_lint_and_note"],
111     &["clippy_utils", "diagnostics", "span_lint_hir"],
112     &["clippy_utils", "diagnostics", "span_lint_and_sugg"],
113     &["clippy_utils", "diagnostics", "span_lint_and_then"],
114     &["clippy_utils", "diagnostics", "span_lint_hir_and_then"],
115 ];
116 const SUGGESTION_DIAGNOSTIC_BUILDER_METHODS: [(&str, bool); 9] = [
117     ("span_suggestion", false),
118     ("span_suggestion_short", false),
119     ("span_suggestion_verbose", false),
120     ("span_suggestion_hidden", false),
121     ("tool_only_span_suggestion", false),
122     ("multipart_suggestion", true),
123     ("multipart_suggestions", true),
124     ("tool_only_multipart_suggestion", true),
125     ("span_suggestions", true),
126 ];
127 const SUGGESTION_FUNCTIONS: [&[&str]; 2] = [
128     &["clippy_utils", "diagnostics", "multispan_sugg"],
129     &["clippy_utils", "diagnostics", "multispan_sugg_with_applicability"],
130 ];
131 const DEPRECATED_LINT_TYPE: [&str; 3] = ["clippy_lints", "deprecated_lints", "ClippyDeprecatedLint"];
132
133 /// The index of the applicability name of `paths::APPLICABILITY_VALUES`
134 const APPLICABILITY_NAME_INDEX: usize = 2;
135 /// This applicability will be set for unresolved applicability values.
136 const APPLICABILITY_UNRESOLVED_STR: &str = "Unresolved";
137 /// The version that will be displayed if none has been defined
138 const VERSION_DEFAULT_STR: &str = "Unknown";
139
140 declare_clippy_lint! {
141     /// ### What it does
142     /// Collects metadata about clippy lints for the website.
143     ///
144     /// This lint will be used to report problems of syntax parsing. You should hopefully never
145     /// see this but never say never I guess ^^
146     ///
147     /// ### Why is this bad?
148     /// This is not a bad thing but definitely a hacky way to do it. See
149     /// issue [#4310](https://github.com/rust-lang/rust-clippy/issues/4310) for a discussion
150     /// about the implementation.
151     ///
152     /// ### Known problems
153     /// Hopefully none. It would be pretty uncool to have a problem here :)
154     ///
155     /// ### Example output
156     /// ```json,ignore
157     /// {
158     ///     "id": "internal_metadata_collector",
159     ///     "id_span": {
160     ///         "path": "clippy_lints/src/utils/internal_lints/metadata_collector.rs",
161     ///         "line": 1
162     ///     },
163     ///     "group": "clippy::internal",
164     ///     "docs": " ### What it does\nCollects metadata about clippy lints for the website. [...] "
165     /// }
166     /// ```
167     #[clippy::version = "1.56.0"]
168     pub INTERNAL_METADATA_COLLECTOR,
169     internal_warn,
170     "A busy bee collection metadata about lints"
171 }
172
173 impl_lint_pass!(MetadataCollector => [INTERNAL_METADATA_COLLECTOR]);
174
175 #[allow(clippy::module_name_repetitions)]
176 #[derive(Debug, Clone)]
177 pub struct MetadataCollector {
178     /// All collected lints
179     ///
180     /// We use a Heap here to have the lints added in alphabetic order in the export
181     lints: BinaryHeap<LintMetadata>,
182     applicability_info: FxHashMap<String, ApplicabilityInfo>,
183     config: Vec<ClippyConfiguration>,
184     clippy_project_root: PathBuf,
185 }
186
187 impl MetadataCollector {
188     pub fn new() -> Self {
189         Self {
190             lints: BinaryHeap::<LintMetadata>::default(),
191             applicability_info: FxHashMap::<String, ApplicabilityInfo>::default(),
192             config: collect_configs(),
193             clippy_project_root: clippy_dev::clippy_project_root(),
194         }
195     }
196
197     fn get_lint_configs(&self, lint_name: &str) -> Option<String> {
198         self.config
199             .iter()
200             .filter(|config| config.lints.iter().any(|lint| lint == lint_name))
201             .map(ToString::to_string)
202             .reduce(|acc, x| acc + &x)
203             .map(|configurations| format!(CONFIGURATION_SECTION_TEMPLATE!(), configurations = configurations))
204     }
205 }
206
207 impl Drop for MetadataCollector {
208     /// You might ask: How hacky is this?
209     /// My answer:     YES
210     fn drop(&mut self) {
211         // The metadata collector gets dropped twice, this makes sure that we only write
212         // when the list is full
213         if self.lints.is_empty() {
214             return;
215         }
216
217         let mut applicability_info = std::mem::take(&mut self.applicability_info);
218
219         // Mapping the final data
220         let mut lints = std::mem::take(&mut self.lints).into_sorted_vec();
221         for x in &mut lints {
222             x.applicability = Some(applicability_info.remove(&x.id).unwrap_or_default());
223             replace_produces(&x.id, &mut x.docs, &self.clippy_project_root);
224         }
225
226         collect_renames(&mut lints);
227
228         // Outputting
229         if Path::new(OUTPUT_FILE).exists() {
230             fs::remove_file(OUTPUT_FILE).unwrap();
231         }
232         let mut file = OpenOptions::new().write(true).create(true).open(OUTPUT_FILE).unwrap();
233         writeln!(file, "{}", serde_json::to_string_pretty(&lints).unwrap()).unwrap();
234     }
235 }
236
237 #[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
238 struct LintMetadata {
239     id: String,
240     id_span: SerializableSpan,
241     group: String,
242     level: String,
243     docs: String,
244     version: String,
245     /// This field is only used in the output and will only be
246     /// mapped shortly before the actual output.
247     applicability: Option<ApplicabilityInfo>,
248 }
249
250 impl LintMetadata {
251     fn new(
252         id: String,
253         id_span: SerializableSpan,
254         group: String,
255         level: &'static str,
256         version: String,
257         docs: String,
258     ) -> Self {
259         Self {
260             id,
261             id_span,
262             group,
263             level: level.to_string(),
264             version,
265             docs,
266             applicability: None,
267         }
268     }
269 }
270
271 fn replace_produces(lint_name: &str, docs: &mut String, clippy_project_root: &Path) {
272     let mut doc_lines = docs.lines().map(ToString::to_string).collect::<Vec<_>>();
273     let mut lines = doc_lines.iter_mut();
274
275     'outer: loop {
276         // Find the start of the example
277
278         // ```rust
279         loop {
280             match lines.next() {
281                 Some(line) if line.trim_start().starts_with("```rust") => {
282                     if line.contains("ignore") || line.contains("no_run") {
283                         // A {{produces}} marker may have been put on a ignored code block by mistake,
284                         // just seek to the end of the code block and continue checking.
285                         if lines.any(|line| line.trim_start().starts_with("```")) {
286                             continue;
287                         }
288
289                         panic!("lint `{}` has an unterminated code block", lint_name)
290                     }
291
292                     break;
293                 },
294                 Some(line) if line.trim_start() == "{{produces}}" => {
295                     panic!(
296                         "lint `{}` has marker {{{{produces}}}} with an ignored or missing code block",
297                         lint_name
298                     )
299                 },
300                 Some(line) => {
301                     let line = line.trim();
302                     // These are the two most common markers of the corrections section
303                     if line.eq_ignore_ascii_case("Use instead:") || line.eq_ignore_ascii_case("Could be written as:") {
304                         break 'outer;
305                     }
306                 },
307                 None => break 'outer,
308             }
309         }
310
311         // Collect the example
312         let mut example = Vec::new();
313         loop {
314             match lines.next() {
315                 Some(line) if line.trim_start() == "```" => break,
316                 Some(line) => example.push(line),
317                 None => panic!("lint `{}` has an unterminated code block", lint_name),
318             }
319         }
320
321         // Find the {{produces}} and attempt to generate the output
322         loop {
323             match lines.next() {
324                 Some(line) if line.is_empty() => {},
325                 Some(line) if line.trim() == "{{produces}}" => {
326                     let output = get_lint_output(lint_name, &example, clippy_project_root);
327                     line.replace_range(
328                         ..,
329                         &format!(
330                             "<details>\
331                             <summary>Produces</summary>\n\
332                             \n\
333                             ```text\n\
334                             {}\n\
335                             ```\n\
336                         </details>",
337                             output
338                         ),
339                     );
340
341                     break;
342                 },
343                 // No {{produces}}, we can move on to the next example
344                 Some(_) => break,
345                 None => break 'outer,
346             }
347         }
348     }
349
350     *docs = cleanup_docs(&doc_lines);
351 }
352
353 fn get_lint_output(lint_name: &str, example: &[&mut String], clippy_project_root: &Path) -> String {
354     let dir = tempfile::tempdir().unwrap_or_else(|e| panic!("failed to create temp dir: {e}"));
355     let file = dir.path().join("lint_example.rs");
356
357     let mut source = String::new();
358     let unhidden = example
359         .iter()
360         .map(|line| line.trim_start().strip_prefix("# ").unwrap_or(line));
361
362     // Get any attributes
363     let mut lines = unhidden.peekable();
364     while let Some(line) = lines.peek() {
365         if line.starts_with("#!") {
366             source.push_str(line);
367             source.push('\n');
368             lines.next();
369         } else {
370             break;
371         }
372     }
373
374     let needs_main = !example.iter().any(|line| line.contains("fn main"));
375     if needs_main {
376         source.push_str("fn main() {\n");
377     }
378
379     for line in lines {
380         source.push_str(line);
381         source.push('\n');
382     }
383
384     if needs_main {
385         source.push_str("}\n");
386     }
387
388     if let Err(e) = fs::write(&file, &source) {
389         panic!("failed to write to `{}`: {e}", file.as_path().to_string_lossy());
390     }
391
392     let prefixed_name = format!("{}{lint_name}", CLIPPY_LINT_GROUP_PREFIX);
393
394     let mut cmd = Command::new("cargo");
395
396     cmd.current_dir(clippy_project_root)
397         .env("CARGO_INCREMENTAL", "0")
398         .env("CLIPPY_ARGS", "")
399         .env("CLIPPY_DISABLE_DOCS_LINKS", "1")
400         // We need to disable this to enable all lints
401         .env("ENABLE_METADATA_COLLECTION", "0")
402         .args(["run", "--bin", "clippy-driver"])
403         .args(["--target-dir", "./clippy_lints/target"])
404         .args(["--", "--error-format=json"])
405         .args(["--edition", "2021"])
406         .arg("-Cdebuginfo=0")
407         .args(["-A", "clippy::all"])
408         .args(["-W", &prefixed_name])
409         .args(["-L", "./target/debug"])
410         .args(["-Z", "no-codegen"]);
411
412     let output = cmd
413         .arg(file.as_path())
414         .output()
415         .unwrap_or_else(|e| panic!("failed to run `{:?}`: {e}", cmd));
416
417     let tmp_file_path = file.to_string_lossy();
418     let stderr = std::str::from_utf8(&output.stderr).unwrap();
419     let msgs = stderr
420         .lines()
421         .filter(|line| line.starts_with('{'))
422         .map(|line| serde_json::from_str(line).unwrap())
423         .collect::<Vec<serde_json::Value>>();
424
425     let mut rendered = String::new();
426     let iter = msgs
427         .iter()
428         .filter(|msg| matches!(&msg["code"]["code"], serde_json::Value::String(s) if s == &prefixed_name));
429
430     for message in iter {
431         let rendered_part = message["rendered"].as_str().expect("rendered field should exist");
432         rendered.push_str(rendered_part);
433     }
434
435     if rendered.is_empty() {
436         let rendered: Vec<&str> = msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
437         let non_json: Vec<&str> = stderr.lines().filter(|line| !line.starts_with('{')).collect();
438         panic!(
439             "did not find lint `{}` in output of example, got:\n{}\n{}",
440             lint_name,
441             non_json.join("\n"),
442             rendered.join("\n")
443         );
444     }
445
446     // The reader doesn't need to see `/tmp/.tmpfiy2Qd/lint_example.rs` :)
447     rendered.trim_end().replace(&*tmp_file_path, "lint_example.rs")
448 }
449
450 #[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
451 struct SerializableSpan {
452     path: String,
453     line: usize,
454 }
455
456 impl fmt::Display for SerializableSpan {
457     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
458         write!(f, "{}:{}", self.path.rsplit('/').next().unwrap_or_default(), self.line)
459     }
460 }
461
462 impl SerializableSpan {
463     fn from_item(cx: &LateContext<'_>, item: &Item<'_>) -> Self {
464         Self::from_span(cx, item.ident.span)
465     }
466
467     fn from_span(cx: &LateContext<'_>, span: Span) -> Self {
468         let loc: Loc = cx.sess().source_map().lookup_char_pos(span.lo());
469
470         Self {
471             path: format!("{}", loc.file.name.prefer_remapped()),
472             line: loc.line,
473         }
474     }
475 }
476
477 #[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
478 struct ApplicabilityInfo {
479     /// Indicates if any of the lint emissions uses multiple spans. This is related to
480     /// [rustfix#141](https://github.com/rust-lang/rustfix/issues/141) as such suggestions can
481     /// currently not be applied automatically.
482     is_multi_part_suggestion: bool,
483     applicability: Option<usize>,
484 }
485
486 impl Serialize for ApplicabilityInfo {
487     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
488     where
489         S: Serializer,
490     {
491         let mut s = serializer.serialize_struct("ApplicabilityInfo", 2)?;
492         s.serialize_field("is_multi_part_suggestion", &self.is_multi_part_suggestion)?;
493         if let Some(index) = self.applicability {
494             s.serialize_field(
495                 "applicability",
496                 &paths::APPLICABILITY_VALUES[index][APPLICABILITY_NAME_INDEX],
497             )?;
498         } else {
499             s.serialize_field("applicability", APPLICABILITY_UNRESOLVED_STR)?;
500         }
501         s.end()
502     }
503 }
504
505 // ==================================================================
506 // Configuration
507 // ==================================================================
508 #[derive(Debug, Clone, Default)]
509 pub struct ClippyConfiguration {
510     name: String,
511     config_type: &'static str,
512     default: String,
513     lints: Vec<String>,
514     doc: String,
515     #[allow(dead_code)]
516     deprecation_reason: Option<&'static str>,
517 }
518
519 impl ClippyConfiguration {
520     pub fn new(
521         name: &'static str,
522         config_type: &'static str,
523         default: String,
524         doc_comment: &'static str,
525         deprecation_reason: Option<&'static str>,
526     ) -> Self {
527         let (lints, doc) = parse_config_field_doc(doc_comment)
528             .unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string()));
529
530         Self {
531             name: to_kebab(name),
532             lints,
533             doc,
534             config_type,
535             default,
536             deprecation_reason,
537         }
538     }
539 }
540
541 fn collect_configs() -> Vec<ClippyConfiguration> {
542     crate::utils::conf::metadata::get_configuration_metadata()
543 }
544
545 /// This parses the field documentation of the config struct.
546 ///
547 /// ```rust, ignore
548 /// parse_config_field_doc(cx, "Lint: LINT_NAME_1, LINT_NAME_2. Papa penguin, papa penguin")
549 /// ```
550 ///
551 /// Would yield:
552 /// ```rust, ignore
553 /// Some(["lint_name_1", "lint_name_2"], "Papa penguin, papa penguin")
554 /// ```
555 fn parse_config_field_doc(doc_comment: &str) -> Option<(Vec<String>, String)> {
556     const DOC_START: &str = " Lint: ";
557     if_chain! {
558         if doc_comment.starts_with(DOC_START);
559         if let Some(split_pos) = doc_comment.find('.');
560         then {
561             let mut doc_comment = doc_comment.to_string();
562             let mut documentation = doc_comment.split_off(split_pos);
563
564             // Extract lints
565             doc_comment.make_ascii_lowercase();
566             let lints: Vec<String> = doc_comment.split_off(DOC_START.len()).split(", ").map(str::to_string).collect();
567
568             // Format documentation correctly
569             // split off leading `.` from lint name list and indent for correct formatting
570             documentation = documentation.trim_start_matches('.').trim().replace("\n ", "\n    ");
571
572             Some((lints, documentation))
573         } else {
574             None
575         }
576     }
577 }
578
579 /// Transforms a given `snake_case_string` to a tasty `kebab-case-string`
580 fn to_kebab(config_name: &str) -> String {
581     config_name.replace('_', "-")
582 }
583
584 impl fmt::Display for ClippyConfiguration {
585     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
586         write!(
587             f,
588             CONFIGURATION_VALUE_TEMPLATE!(),
589             name = self.name,
590             ty = self.config_type,
591             doc = self.doc,
592             default = self.default
593         )
594     }
595 }
596
597 // ==================================================================
598 // Lint pass
599 // ==================================================================
600 impl<'hir> LateLintPass<'hir> for MetadataCollector {
601     /// Collecting lint declarations like:
602     /// ```rust, ignore
603     /// declare_clippy_lint! {
604     ///     /// ### What it does
605     ///     /// Something IDK.
606     ///     pub SOME_LINT,
607     ///     internal,
608     ///     "Who am I?"
609     /// }
610     /// ```
611     fn check_item(&mut self, cx: &LateContext<'hir>, item: &'hir Item<'_>) {
612         if let ItemKind::Static(ty, Mutability::Not, _) = item.kind {
613             // Normal lint
614             if_chain! {
615                 // item validation
616                 if is_lint_ref_type(cx, ty);
617                 // blacklist check
618                 let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
619                 if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
620                 // metadata extraction
621                 if let Some((group, level)) = get_lint_group_and_level_or_lint(cx, &lint_name, item);
622                 if let Some(mut raw_docs) = extract_attr_docs_or_lint(cx, item);
623                 then {
624                     if let Some(configuration_section) = self.get_lint_configs(&lint_name) {
625                         raw_docs.push_str(&configuration_section);
626                     }
627                     let version = get_lint_version(cx, item);
628
629                     self.lints.push(LintMetadata::new(
630                         lint_name,
631                         SerializableSpan::from_item(cx, item),
632                         group,
633                         level,
634                         version,
635                         raw_docs,
636                     ));
637                 }
638             }
639
640             if_chain! {
641                 if is_deprecated_lint(cx, ty);
642                 // blacklist check
643                 let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
644                 if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
645                 // Metadata the little we can get from a deprecated lint
646                 if let Some(raw_docs) = extract_attr_docs_or_lint(cx, item);
647                 then {
648                     let version = get_lint_version(cx, item);
649
650                     self.lints.push(LintMetadata::new(
651                         lint_name,
652                         SerializableSpan::from_item(cx, item),
653                         DEPRECATED_LINT_GROUP_STR.to_string(),
654                         DEPRECATED_LINT_LEVEL,
655                         version,
656                         raw_docs,
657                     ));
658                 }
659             }
660         }
661     }
662
663     /// Collecting constant applicability from the actual lint emissions
664     ///
665     /// Example:
666     /// ```rust, ignore
667     /// span_lint_and_sugg(
668     ///     cx,
669     ///     SOME_LINT,
670     ///     item.span,
671     ///     "Le lint message",
672     ///     "Here comes help:",
673     ///     "#![allow(clippy::all)]",
674     ///     Applicability::MachineApplicable, // <-- Extracts this constant value
675     /// );
676     /// ```
677     fn check_expr(&mut self, cx: &LateContext<'hir>, expr: &'hir hir::Expr<'_>) {
678         if let Some(args) = match_lint_emission(cx, expr) {
679             let emission_info = extract_emission_info(cx, args);
680             if emission_info.is_empty() {
681                 // See:
682                 // - src/misc.rs:734:9
683                 // - src/methods/mod.rs:3545:13
684                 // - src/methods/mod.rs:3496:13
685                 // We are basically unable to resolve the lint name itself.
686                 return;
687             }
688
689             for (lint_name, applicability, is_multi_part) in emission_info {
690                 let app_info = self.applicability_info.entry(lint_name).or_default();
691                 app_info.applicability = applicability;
692                 app_info.is_multi_part_suggestion = is_multi_part;
693             }
694         }
695     }
696 }
697
698 // ==================================================================
699 // Lint definition extraction
700 // ==================================================================
701 fn sym_to_string(sym: Symbol) -> String {
702     sym.as_str().to_string()
703 }
704
705 fn extract_attr_docs_or_lint(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> {
706     extract_attr_docs(cx, item).or_else(|| {
707         lint_collection_error_item(cx, item, "could not collect the lint documentation");
708         None
709     })
710 }
711
712 /// This function collects all documentation that has been added to an item using
713 /// `#[doc = r""]` attributes. Several attributes are aggravated using line breaks
714 ///
715 /// ```ignore
716 /// #[doc = r"Hello world!"]
717 /// #[doc = r"=^.^="]
718 /// struct SomeItem {}
719 /// ```
720 ///
721 /// Would result in `Hello world!\n=^.^=\n`
722 fn extract_attr_docs(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> {
723     let attrs = cx.tcx.hir().attrs(item.hir_id());
724     let mut lines = attrs.iter().filter_map(ast::Attribute::doc_str);
725
726     if let Some(line) = lines.next() {
727         let raw_docs = lines.fold(String::from(line.as_str()) + "\n", |s, line| s + line.as_str() + "\n");
728         return Some(raw_docs);
729     }
730
731     None
732 }
733
734 /// This function may modify the doc comment to ensure that the string can be displayed using a
735 /// markdown viewer in Clippy's lint list. The following modifications could be applied:
736 /// * Removal of leading space after a new line. (Important to display tables)
737 /// * Ensures that code blocks only contain language information
738 fn cleanup_docs(docs_collection: &Vec<String>) -> String {
739     let mut in_code_block = false;
740     let mut is_code_block_rust = false;
741
742     let mut docs = String::new();
743     for line in docs_collection {
744         // Rustdoc hides code lines starting with `# ` and this removes them from Clippy's lint list :)
745         if is_code_block_rust && line.trim_start().starts_with("# ") {
746             continue;
747         }
748
749         // The line should be represented in the lint list, even if it's just an empty line
750         docs.push('\n');
751         if let Some(info) = line.trim_start().strip_prefix("```") {
752             in_code_block = !in_code_block;
753             is_code_block_rust = false;
754             if in_code_block {
755                 let lang = info
756                     .trim()
757                     .split(',')
758                     // remove rustdoc directives
759                     .find(|&s| !matches!(s, "" | "ignore" | "no_run" | "should_panic"))
760                     // if no language is present, fill in "rust"
761                     .unwrap_or("rust");
762                 docs.push_str("```");
763                 docs.push_str(lang);
764
765                 is_code_block_rust = lang == "rust";
766                 continue;
767             }
768         }
769         // This removes the leading space that the macro translation introduces
770         if let Some(stripped_doc) = line.strip_prefix(' ') {
771             docs.push_str(stripped_doc);
772         } else if !line.is_empty() {
773             docs.push_str(line);
774         }
775     }
776
777     docs
778 }
779
780 fn get_lint_version(cx: &LateContext<'_>, item: &Item<'_>) -> String {
781     extract_clippy_version_value(cx, item).map_or_else(
782         || VERSION_DEFAULT_STR.to_string(),
783         |version| version.as_str().to_string(),
784     )
785 }
786
787 fn get_lint_group_and_level_or_lint(
788     cx: &LateContext<'_>,
789     lint_name: &str,
790     item: &Item<'_>,
791 ) -> Option<(String, &'static str)> {
792     let result = cx.lint_store.check_lint_name(
793         lint_name,
794         Some(sym::clippy),
795         &[Ident::with_dummy_span(sym::clippy)].into_iter().collect(),
796     );
797     if let CheckLintNameResult::Tool(Ok(lint_lst)) = result {
798         if let Some(group) = get_lint_group(cx, lint_lst[0]) {
799             if EXCLUDED_LINT_GROUPS.contains(&group.as_str()) {
800                 return None;
801             }
802
803             if let Some(level) = get_lint_level_from_group(&group) {
804                 Some((group, level))
805             } else {
806                 lint_collection_error_item(
807                     cx,
808                     item,
809                     &format!("Unable to determine lint level for found group `{}`", group),
810                 );
811                 None
812             }
813         } else {
814             lint_collection_error_item(cx, item, "Unable to determine lint group");
815             None
816         }
817     } else {
818         lint_collection_error_item(cx, item, "Unable to find lint in lint_store");
819         None
820     }
821 }
822
823 fn get_lint_group(cx: &LateContext<'_>, lint_id: LintId) -> Option<String> {
824     for (group_name, lints, _) in cx.lint_store.get_lint_groups() {
825         if IGNORED_LINT_GROUPS.contains(&group_name) {
826             continue;
827         }
828
829         if lints.iter().any(|group_lint| *group_lint == lint_id) {
830             let group = group_name.strip_prefix(CLIPPY_LINT_GROUP_PREFIX).unwrap_or(group_name);
831             return Some((*group).to_string());
832         }
833     }
834
835     None
836 }
837
838 fn get_lint_level_from_group(lint_group: &str) -> Option<&'static str> {
839     DEFAULT_LINT_LEVELS
840         .iter()
841         .find_map(|(group_name, group_level)| (*group_name == lint_group).then(|| *group_level))
842 }
843
844 fn is_deprecated_lint(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
845     if let hir::TyKind::Path(ref path) = ty.kind {
846         if let hir::def::Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, ty.hir_id) {
847             return match_def_path(cx, def_id, &DEPRECATED_LINT_TYPE);
848         }
849     }
850
851     false
852 }
853
854 fn collect_renames(lints: &mut Vec<LintMetadata>) {
855     for lint in lints {
856         let mut collected = String::new();
857         let mut names = vec![lint.id.clone()];
858
859         loop {
860             if let Some(lint_name) = names.pop() {
861                 for (k, v) in RENAMED_LINTS {
862                     if_chain! {
863                         if let Some(name) = v.strip_prefix(CLIPPY_LINT_GROUP_PREFIX);
864                         if name == lint_name;
865                         if let Some(past_name) = k.strip_prefix(CLIPPY_LINT_GROUP_PREFIX);
866                         then {
867                             write!(collected, RENAME_VALUE_TEMPLATE!(), name = past_name).unwrap();
868                             names.push(past_name.to_string());
869                         }
870                     }
871                 }
872
873                 continue;
874             }
875
876             break;
877         }
878
879         if !collected.is_empty() {
880             write!(&mut lint.docs, RENAMES_SECTION_TEMPLATE!(), names = collected).unwrap();
881         }
882     }
883 }
884
885 // ==================================================================
886 // Lint emission
887 // ==================================================================
888 fn lint_collection_error_item(cx: &LateContext<'_>, item: &Item<'_>, message: &str) {
889     span_lint(
890         cx,
891         INTERNAL_METADATA_COLLECTOR,
892         item.ident.span,
893         &format!("metadata collection error for `{}`: {}", item.ident.name, message),
894     );
895 }
896
897 // ==================================================================
898 // Applicability
899 // ==================================================================
900 /// This function checks if a given expression is equal to a simple lint emission function call.
901 /// It will return the function arguments if the emission matched any function.
902 fn match_lint_emission<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'_>) -> Option<&'hir [hir::Expr<'hir>]> {
903     LINT_EMISSION_FUNCTIONS
904         .iter()
905         .find_map(|emission_fn| match_function_call(cx, expr, emission_fn))
906 }
907
908 fn take_higher_applicability(a: Option<usize>, b: Option<usize>) -> Option<usize> {
909     a.map_or(b, |a| a.max(b.unwrap_or_default()).into())
910 }
911
912 fn extract_emission_info<'hir>(
913     cx: &LateContext<'hir>,
914     args: &'hir [hir::Expr<'hir>],
915 ) -> Vec<(String, Option<usize>, bool)> {
916     let mut lints = Vec::new();
917     let mut applicability = None;
918     let mut multi_part = false;
919
920     for arg in args {
921         let (arg_ty, _) = walk_ptrs_ty_depth(cx.typeck_results().expr_ty(arg));
922
923         if match_type(cx, arg_ty, &paths::LINT) {
924             // If we found the lint arg, extract the lint name
925             let mut resolved_lints = resolve_lints(cx, arg);
926             lints.append(&mut resolved_lints);
927         } else if match_type(cx, arg_ty, &paths::APPLICABILITY) {
928             applicability = resolve_applicability(cx, arg);
929         } else if arg_ty.is_closure() {
930             multi_part |= check_is_multi_part(cx, arg);
931             applicability = applicability.or_else(|| resolve_applicability(cx, arg));
932         }
933     }
934
935     lints
936         .into_iter()
937         .map(|lint_name| (lint_name, applicability, multi_part))
938         .collect()
939 }
940
941 /// Resolves the possible lints that this expression could reference
942 fn resolve_lints<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -> Vec<String> {
943     let mut resolver = LintResolver::new(cx);
944     resolver.visit_expr(expr);
945     resolver.lints
946 }
947
948 /// This function tries to resolve the linked applicability to the given expression.
949 fn resolve_applicability<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -> Option<usize> {
950     let mut resolver = ApplicabilityResolver::new(cx);
951     resolver.visit_expr(expr);
952     resolver.complete()
953 }
954
955 fn check_is_multi_part<'hir>(cx: &LateContext<'hir>, closure_expr: &'hir hir::Expr<'hir>) -> bool {
956     if let ExprKind::Closure { body, .. } = closure_expr.kind {
957         let mut scanner = IsMultiSpanScanner::new(cx);
958         intravisit::walk_body(&mut scanner, cx.tcx.hir().body(body));
959         return scanner.is_multi_part();
960     } else if let Some(local) = get_parent_local(cx, closure_expr) {
961         if let Some(local_init) = local.init {
962             return check_is_multi_part(cx, local_init);
963         }
964     }
965
966     false
967 }
968
969 struct LintResolver<'a, 'hir> {
970     cx: &'a LateContext<'hir>,
971     lints: Vec<String>,
972 }
973
974 impl<'a, 'hir> LintResolver<'a, 'hir> {
975     fn new(cx: &'a LateContext<'hir>) -> Self {
976         Self {
977             cx,
978             lints: Vec::<String>::default(),
979         }
980     }
981 }
982
983 impl<'a, 'hir> intravisit::Visitor<'hir> for LintResolver<'a, 'hir> {
984     type NestedFilter = nested_filter::All;
985
986     fn nested_visit_map(&mut self) -> Self::Map {
987         self.cx.tcx.hir()
988     }
989
990     fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
991         if_chain! {
992             if let ExprKind::Path(qpath) = &expr.kind;
993             if let QPath::Resolved(_, path) = qpath;
994
995             let (expr_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(expr));
996             if match_type(self.cx, expr_ty, &paths::LINT);
997             then {
998                 if let hir::def::Res::Def(DefKind::Static(..), _) = path.res {
999                     let lint_name = last_path_segment(qpath).ident.name;
1000                     self.lints.push(sym_to_string(lint_name).to_ascii_lowercase());
1001                 } else if let Some(local) = get_parent_local(self.cx, expr) {
1002                     if let Some(local_init) = local.init {
1003                         intravisit::walk_expr(self, local_init);
1004                     }
1005                 }
1006             }
1007         }
1008
1009         intravisit::walk_expr(self, expr);
1010     }
1011 }
1012
1013 /// This visitor finds the highest applicability value in the visited expressions
1014 struct ApplicabilityResolver<'a, 'hir> {
1015     cx: &'a LateContext<'hir>,
1016     /// This is the index of hightest `Applicability` for `paths::APPLICABILITY_VALUES`
1017     applicability_index: Option<usize>,
1018 }
1019
1020 impl<'a, 'hir> ApplicabilityResolver<'a, 'hir> {
1021     fn new(cx: &'a LateContext<'hir>) -> Self {
1022         Self {
1023             cx,
1024             applicability_index: None,
1025         }
1026     }
1027
1028     fn add_new_index(&mut self, new_index: usize) {
1029         self.applicability_index = take_higher_applicability(self.applicability_index, Some(new_index));
1030     }
1031
1032     fn complete(self) -> Option<usize> {
1033         self.applicability_index
1034     }
1035 }
1036
1037 impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> {
1038     type NestedFilter = nested_filter::All;
1039
1040     fn nested_visit_map(&mut self) -> Self::Map {
1041         self.cx.tcx.hir()
1042     }
1043
1044     fn visit_path(&mut self, path: &'hir hir::Path<'hir>, _id: hir::HirId) {
1045         for (index, enum_value) in paths::APPLICABILITY_VALUES.iter().enumerate() {
1046             if match_path(path, enum_value) {
1047                 self.add_new_index(index);
1048                 return;
1049             }
1050         }
1051     }
1052
1053     fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
1054         let (expr_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(expr));
1055
1056         if_chain! {
1057             if match_type(self.cx, expr_ty, &paths::APPLICABILITY);
1058             if let Some(local) = get_parent_local(self.cx, expr);
1059             if let Some(local_init) = local.init;
1060             then {
1061                 intravisit::walk_expr(self, local_init);
1062             }
1063         };
1064
1065         intravisit::walk_expr(self, expr);
1066     }
1067 }
1068
1069 /// This returns the parent local node if the expression is a reference one
1070 fn get_parent_local<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -> Option<&'hir hir::Local<'hir>> {
1071     if let ExprKind::Path(QPath::Resolved(_, path)) = expr.kind {
1072         if let hir::def::Res::Local(local_hir) = path.res {
1073             return get_parent_local_hir_id(cx, local_hir);
1074         }
1075     }
1076
1077     None
1078 }
1079
1080 fn get_parent_local_hir_id<'hir>(cx: &LateContext<'hir>, hir_id: hir::HirId) -> Option<&'hir hir::Local<'hir>> {
1081     let map = cx.tcx.hir();
1082
1083     match map.find(map.get_parent_node(hir_id)) {
1084         Some(hir::Node::Local(local)) => Some(local),
1085         Some(hir::Node::Pat(pattern)) => get_parent_local_hir_id(cx, pattern.hir_id),
1086         _ => None,
1087     }
1088 }
1089
1090 /// This visitor finds the highest applicability value in the visited expressions
1091 struct IsMultiSpanScanner<'a, 'hir> {
1092     cx: &'a LateContext<'hir>,
1093     suggestion_count: usize,
1094 }
1095
1096 impl<'a, 'hir> IsMultiSpanScanner<'a, 'hir> {
1097     fn new(cx: &'a LateContext<'hir>) -> Self {
1098         Self {
1099             cx,
1100             suggestion_count: 0,
1101         }
1102     }
1103
1104     /// Add a new single expression suggestion to the counter
1105     fn add_single_span_suggestion(&mut self) {
1106         self.suggestion_count += 1;
1107     }
1108
1109     /// Signals that a suggestion with possible multiple spans was found
1110     fn add_multi_part_suggestion(&mut self) {
1111         self.suggestion_count += 2;
1112     }
1113
1114     /// Checks if the suggestions include multiple spans
1115     fn is_multi_part(&self) -> bool {
1116         self.suggestion_count > 1
1117     }
1118 }
1119
1120 impl<'a, 'hir> intravisit::Visitor<'hir> for IsMultiSpanScanner<'a, 'hir> {
1121     type NestedFilter = nested_filter::All;
1122
1123     fn nested_visit_map(&mut self) -> Self::Map {
1124         self.cx.tcx.hir()
1125     }
1126
1127     fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
1128         // Early return if the lint is already multi span
1129         if self.is_multi_part() {
1130             return;
1131         }
1132
1133         match &expr.kind {
1134             ExprKind::Call(fn_expr, _args) => {
1135                 let found_function = SUGGESTION_FUNCTIONS
1136                     .iter()
1137                     .any(|func_path| match_function_call(self.cx, fn_expr, func_path).is_some());
1138                 if found_function {
1139                     // These functions are all multi part suggestions
1140                     self.add_single_span_suggestion();
1141                 }
1142             },
1143             ExprKind::MethodCall(path, arg, _arg_span) => {
1144                 let (self_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(&arg[0]));
1145                 if match_type(self.cx, self_ty, &paths::DIAGNOSTIC_BUILDER) {
1146                     let called_method = path.ident.name.as_str().to_string();
1147                     for (method_name, is_multi_part) in &SUGGESTION_DIAGNOSTIC_BUILDER_METHODS {
1148                         if *method_name == called_method {
1149                             if *is_multi_part {
1150                                 self.add_multi_part_suggestion();
1151                             } else {
1152                                 self.add_single_span_suggestion();
1153                             }
1154                             break;
1155                         }
1156                     }
1157                 }
1158             },
1159             _ => {},
1160         }
1161
1162         intravisit::walk_expr(self, expr);
1163     }
1164 }