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)
4 //! This module and therefore the entire lint is guarded by a feature flag called `internal`
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
10 use crate::renamed_lints::RENAMED_LINTS;
11 use crate::utils::internal_lints::{extract_clippy_version_value, is_lint_ref_type};
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;
18 use rustc_data_structures::fx::FxHashMap;
20 self as hir, def::DefKind, intravisit, intravisit::Visitor, ExprKind, Item, ItemKind, Mutability, QPath,
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;
30 use std::fmt::Write as _;
31 use std::fs::{self, OpenOptions};
32 use std::io::prelude::*;
34 use std::path::PathBuf;
35 use std::process::Command;
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
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"),
58 ("pedantic", "allow"),
59 ("complexity", "warn"),
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::";
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 {
75 This lint has the following configuration variables:
81 /// This template will be used to format an individual `ClippyConfiguration` instance in the
82 /// lint documentation.
84 /// The format function will provide strings for the following parameters: `name`, `ty`, `doc` and
86 macro_rules! CONFIGURATION_VALUE_TEMPLATE {
88 "* `{name}`: `{ty}`: {doc} (defaults to `{default}`)\n"
92 macro_rules! RENAMES_SECTION_TEMPLATE {
101 macro_rules! RENAME_VALUE_TEMPLATE {
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"],
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),
127 const SUGGESTION_FUNCTIONS: [&[&str]; 2] = [
128 &["clippy_utils", "diagnostics", "multispan_sugg"],
129 &["clippy_utils", "diagnostics", "multispan_sugg_with_applicability"],
131 const DEPRECATED_LINT_TYPE: [&str; 3] = ["clippy_lints", "deprecated_lints", "ClippyDeprecatedLint"];
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";
140 declare_clippy_lint! {
142 /// Collects metadata about clippy lints for the website.
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 ^^
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.
152 /// ### Known problems
153 /// Hopefully none. It would be pretty uncool to have a problem here :)
155 /// ### Example output
158 /// "id": "internal_metadata_collector",
160 /// "path": "clippy_lints/src/utils/internal_lints/metadata_collector.rs",
163 /// "group": "clippy::internal",
164 /// "docs": " ### What it does\nCollects metadata about clippy lints for the website. [...] "
167 #[clippy::version = "1.56.0"]
168 pub INTERNAL_METADATA_COLLECTOR,
170 "A busy bee collection metadata about lints"
173 impl_lint_pass!(MetadataCollector => [INTERNAL_METADATA_COLLECTOR]);
175 #[allow(clippy::module_name_repetitions)]
176 #[derive(Debug, Clone)]
177 pub struct MetadataCollector {
178 /// All collected lints
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,
187 impl MetadataCollector {
188 pub fn new() -> 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(),
197 fn get_lint_configs(&self, lint_name: &str) -> Option<String> {
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))
207 impl Drop for MetadataCollector {
208 /// You might ask: How hacky is this?
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() {
217 let mut applicability_info = std::mem::take(&mut self.applicability_info);
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);
226 collect_renames(&mut lints);
229 if Path::new(OUTPUT_FILE).exists() {
230 fs::remove_file(OUTPUT_FILE).unwrap();
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();
237 #[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
238 struct LintMetadata {
240 id_span: SerializableSpan,
245 /// This field is only used in the output and will only be
246 /// mapped shortly before the actual output.
247 applicability: Option<ApplicabilityInfo>,
253 id_span: SerializableSpan,
263 level: level.to_string(),
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();
276 // Find the start of the example
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("```")) {
289 panic!("lint `{}` has an unterminated code block", lint_name)
294 Some(line) if line.trim_start() == "{{produces}}" => {
296 "lint `{}` has marker {{{{produces}}}} with an ignored or missing code block",
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:") {
307 None => break 'outer,
311 // Collect the example
312 let mut example = Vec::new();
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),
321 // Find the {{produces}} and attempt to generate the output
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);
331 <summary>Produces</summary>\n\
343 // No {{produces}}, we can move on to the next example
345 None => break 'outer,
350 *docs = cleanup_docs(&doc_lines);
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");
357 let mut source = String::new();
358 let unhidden = example
360 .map(|line| line.trim_start().strip_prefix("# ").unwrap_or(line));
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);
374 let needs_main = !example.iter().any(|line| line.contains("fn main"));
376 source.push_str("fn main() {\n");
380 source.push_str(line);
385 source.push_str("}\n");
388 if let Err(e) = fs::write(&file, &source) {
389 panic!("failed to write to `{}`: {e}", file.as_path().to_string_lossy());
392 let prefixed_name = format!("{}{lint_name}", CLIPPY_LINT_GROUP_PREFIX);
394 let mut cmd = Command::new("cargo");
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"]);
415 .unwrap_or_else(|e| panic!("failed to run `{:?}`: {e}", cmd));
417 let tmp_file_path = file.to_string_lossy();
418 let stderr = std::str::from_utf8(&output.stderr).unwrap();
421 .filter(|line| line.starts_with('{'))
422 .map(|line| serde_json::from_str(line).unwrap())
423 .collect::<Vec<serde_json::Value>>();
425 let mut rendered = String::new();
428 .filter(|msg| matches!(&msg["code"]["code"], serde_json::Value::String(s) if s == &prefixed_name));
430 for message in iter {
431 let rendered_part = message["rendered"].as_str().expect("rendered field should exist");
432 rendered.push_str(rendered_part);
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();
439 "did not find lint `{}` in output of example, got:\n{}\n{}",
446 // The reader doesn't need to see `/tmp/.tmpfiy2Qd/lint_example.rs` :)
447 rendered.trim_end().replace(&*tmp_file_path, "lint_example.rs")
450 #[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
451 struct SerializableSpan {
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)
462 impl SerializableSpan {
463 fn from_item(cx: &LateContext<'_>, item: &Item<'_>) -> Self {
464 Self::from_span(cx, item.ident.span)
467 fn from_span(cx: &LateContext<'_>, span: Span) -> Self {
468 let loc: Loc = cx.sess().source_map().lookup_char_pos(span.lo());
471 path: format!("{}", loc.file.name.prefer_remapped()),
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>,
486 impl Serialize for ApplicabilityInfo {
487 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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 {
496 &paths::APPLICABILITY_VALUES[index][APPLICABILITY_NAME_INDEX],
499 s.serialize_field("applicability", APPLICABILITY_UNRESOLVED_STR)?;
505 // ==================================================================
507 // ==================================================================
508 #[derive(Debug, Clone, Default)]
509 pub struct ClippyConfiguration {
511 config_type: &'static str,
516 deprecation_reason: Option<&'static str>,
519 impl ClippyConfiguration {
522 config_type: &'static str,
524 doc_comment: &'static str,
525 deprecation_reason: Option<&'static str>,
527 let (lints, doc) = parse_config_field_doc(doc_comment)
528 .unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string()));
531 name: to_kebab(name),
541 fn collect_configs() -> Vec<ClippyConfiguration> {
542 crate::utils::conf::metadata::get_configuration_metadata()
545 /// This parses the field documentation of the config struct.
548 /// parse_config_field_doc(cx, "Lint: LINT_NAME_1, LINT_NAME_2. Papa penguin, papa penguin")
553 /// Some(["lint_name_1", "lint_name_2"], "Papa penguin, papa penguin")
555 fn parse_config_field_doc(doc_comment: &str) -> Option<(Vec<String>, String)> {
556 const DOC_START: &str = " Lint: ";
558 if doc_comment.starts_with(DOC_START);
559 if let Some(split_pos) = doc_comment.find('.');
561 let mut doc_comment = doc_comment.to_string();
562 let mut documentation = doc_comment.split_off(split_pos);
565 doc_comment.make_ascii_lowercase();
566 let lints: Vec<String> = doc_comment.split_off(DOC_START.len()).split(", ").map(str::to_string).collect();
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 ");
572 Some((lints, documentation))
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('_', "-")
584 impl fmt::Display for ClippyConfiguration {
585 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
588 CONFIGURATION_VALUE_TEMPLATE!(),
590 ty = self.config_type,
592 default = self.default
597 // ==================================================================
599 // ==================================================================
600 impl<'hir> LateLintPass<'hir> for MetadataCollector {
601 /// Collecting lint declarations like:
603 /// declare_clippy_lint! {
604 /// /// ### What it does
605 /// /// Something IDK.
611 fn check_item(&mut self, cx: &LateContext<'hir>, item: &'hir Item<'_>) {
612 if let ItemKind::Static(ty, Mutability::Not, _) = item.kind {
616 if is_lint_ref_type(cx, ty);
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);
624 if let Some(configuration_section) = self.get_lint_configs(&lint_name) {
625 raw_docs.push_str(&configuration_section);
627 let version = get_lint_version(cx, item);
629 self.lints.push(LintMetadata::new(
631 SerializableSpan::from_item(cx, item),
641 if is_deprecated_lint(cx, ty);
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);
648 let version = get_lint_version(cx, item);
650 self.lints.push(LintMetadata::new(
652 SerializableSpan::from_item(cx, item),
653 DEPRECATED_LINT_GROUP_STR.to_string(),
654 DEPRECATED_LINT_LEVEL,
663 /// Collecting constant applicability from the actual lint emissions
667 /// span_lint_and_sugg(
671 /// "Le lint message",
672 /// "Here comes help:",
673 /// "#![allow(clippy::all)]",
674 /// Applicability::MachineApplicable, // <-- Extracts this constant value
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() {
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.
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;
698 // ==================================================================
699 // Lint definition extraction
700 // ==================================================================
701 fn sym_to_string(sym: Symbol) -> String {
702 sym.as_str().to_string()
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");
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
716 /// #[doc = r"Hello world!"]
717 /// #[doc = r"=^.^="]
718 /// struct SomeItem {}
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);
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);
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;
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("# ") {
749 // The line should be represented in the lint list, even if it's just an empty line
751 if let Some(info) = line.trim_start().strip_prefix("```") {
752 in_code_block = !in_code_block;
753 is_code_block_rust = false;
758 // remove rustdoc directives
759 .find(|&s| !matches!(s, "" | "ignore" | "no_run" | "should_panic"))
760 // if no language is present, fill in "rust"
762 docs.push_str("```");
765 is_code_block_rust = lang == "rust";
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() {
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(),
787 fn get_lint_group_and_level_or_lint(
788 cx: &LateContext<'_>,
791 ) -> Option<(String, &'static str)> {
792 let result = cx.lint_store.check_lint_name(
795 &[Ident::with_dummy_span(sym::clippy)].into_iter().collect(),
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()) {
803 if let Some(level) = get_lint_level_from_group(&group) {
806 lint_collection_error_item(
809 &format!("Unable to determine lint level for found group `{}`", group),
814 lint_collection_error_item(cx, item, "Unable to determine lint group");
818 lint_collection_error_item(cx, item, "Unable to find lint in lint_store");
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) {
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());
838 fn get_lint_level_from_group(lint_group: &str) -> Option<&'static str> {
841 .find_map(|(group_name, group_level)| (*group_name == lint_group).then(|| *group_level))
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);
854 fn collect_renames(lints: &mut Vec<LintMetadata>) {
856 let mut collected = String::new();
857 let mut names = vec![lint.id.clone()];
860 if let Some(lint_name) = names.pop() {
861 for (k, v) in RENAMED_LINTS {
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);
867 write!(collected, RENAME_VALUE_TEMPLATE!(), name = past_name).unwrap();
868 names.push(past_name.to_string());
879 if !collected.is_empty() {
880 write!(&mut lint.docs, RENAMES_SECTION_TEMPLATE!(), names = collected).unwrap();
885 // ==================================================================
887 // ==================================================================
888 fn lint_collection_error_item(cx: &LateContext<'_>, item: &Item<'_>, message: &str) {
891 INTERNAL_METADATA_COLLECTOR,
893 &format!("metadata collection error for `{}`: {}", item.ident.name, message),
897 // ==================================================================
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
905 .find_map(|emission_fn| match_function_call(cx, expr, emission_fn))
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())
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;
921 let (arg_ty, _) = walk_ptrs_ty_depth(cx.typeck_results().expr_ty(arg));
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));
937 .map(|lint_name| (lint_name, applicability, multi_part))
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);
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);
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);
969 struct LintResolver<'a, 'hir> {
970 cx: &'a LateContext<'hir>,
974 impl<'a, 'hir> LintResolver<'a, 'hir> {
975 fn new(cx: &'a LateContext<'hir>) -> Self {
978 lints: Vec::<String>::default(),
983 impl<'a, 'hir> intravisit::Visitor<'hir> for LintResolver<'a, 'hir> {
984 type NestedFilter = nested_filter::All;
986 fn nested_visit_map(&mut self) -> Self::Map {
990 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
992 if let ExprKind::Path(qpath) = &expr.kind;
993 if let QPath::Resolved(_, path) = qpath;
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);
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);
1009 intravisit::walk_expr(self, expr);
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>,
1020 impl<'a, 'hir> ApplicabilityResolver<'a, 'hir> {
1021 fn new(cx: &'a LateContext<'hir>) -> Self {
1024 applicability_index: None,
1028 fn add_new_index(&mut self, new_index: usize) {
1029 self.applicability_index = take_higher_applicability(self.applicability_index, Some(new_index));
1032 fn complete(self) -> Option<usize> {
1033 self.applicability_index
1037 impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> {
1038 type NestedFilter = nested_filter::All;
1040 fn nested_visit_map(&mut self) -> Self::Map {
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);
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));
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;
1061 intravisit::walk_expr(self, local_init);
1065 intravisit::walk_expr(self, expr);
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);
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();
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),
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,
1096 impl<'a, 'hir> IsMultiSpanScanner<'a, 'hir> {
1097 fn new(cx: &'a LateContext<'hir>) -> Self {
1100 suggestion_count: 0,
1104 /// Add a new single expression suggestion to the counter
1105 fn add_single_span_suggestion(&mut self) {
1106 self.suggestion_count += 1;
1109 /// Signals that a suggestion with possible multiple spans was found
1110 fn add_multi_part_suggestion(&mut self) {
1111 self.suggestion_count += 2;
1114 /// Checks if the suggestions include multiple spans
1115 fn is_multi_part(&self) -> bool {
1116 self.suggestion_count > 1
1120 impl<'a, 'hir> intravisit::Visitor<'hir> for IsMultiSpanScanner<'a, 'hir> {
1121 type NestedFilter = nested_filter::All;
1123 fn nested_visit_map(&mut self) -> Self::Map {
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() {
1134 ExprKind::Call(fn_expr, _args) => {
1135 let found_function = SUGGESTION_FUNCTIONS
1137 .any(|func_path| match_function_call(self.cx, fn_expr, func_path).is_some());
1139 // These functions are all multi part suggestions
1140 self.add_single_span_suggestion();
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 {
1150 self.add_multi_part_suggestion();
1152 self.add_single_span_suggestion();
1162 intravisit::walk_expr(self, expr);