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::utils::internal_lints::{extract_clippy_version_value, is_lint_ref_type};
12 use clippy_utils::diagnostics::span_lint;
13 use clippy_utils::ty::{match_type, walk_ptrs_ty_depth};
14 use clippy_utils::{last_path_segment, match_def_path, match_function_call, match_path, paths};
15 use if_chain::if_chain;
17 use rustc_data_structures::fx::FxHashMap;
19 self as hir, def::DefKind, intravisit, intravisit::Visitor, ExprKind, Item, ItemKind, Mutability, QPath,
21 use rustc_lint::{CheckLintNameResult, LateContext, LateLintPass, LintContext, LintId};
22 use rustc_middle::hir::nested_filter;
23 use rustc_session::{declare_tool_lint, impl_lint_pass};
24 use rustc_span::symbol::Ident;
25 use rustc_span::{sym, Loc, Span, Symbol};
26 use serde::{ser::SerializeStruct, Serialize, Serializer};
27 use std::collections::BinaryHeap;
29 use std::fs::{self, OpenOptions};
30 use std::io::prelude::*;
33 /// This is the output file of the lint collector.
34 const OUTPUT_FILE: &str = "../util/gh-pages/lints.json";
35 /// These lints are excluded from the export.
36 const BLACK_LISTED_LINTS: [&str; 3] = ["lint_author", "deep_code_inspection", "internal_metadata_collector"];
37 /// These groups will be ignored by the lint group matcher. This is useful for collections like
39 const IGNORED_LINT_GROUPS: [&str; 1] = ["clippy::all"];
40 /// Lints within this group will be excluded from the collection. These groups
41 /// have to be defined without the `clippy::` prefix.
42 const EXCLUDED_LINT_GROUPS: [&str; 1] = ["internal"];
43 /// Collected deprecated lint will be assigned to this group in the JSON output
44 const DEPRECATED_LINT_GROUP_STR: &str = "deprecated";
45 /// This is the lint level for deprecated lints that will be displayed in the lint list
46 const DEPRECATED_LINT_LEVEL: &str = "none";
47 /// This array holds Clippy's lint groups with their corresponding default lint level. The
48 /// lint level for deprecated lints is set in `DEPRECATED_LINT_LEVEL`.
49 const DEFAULT_LINT_LEVELS: &[(&str, &str)] = &[
50 ("correctness", "deny"),
51 ("suspicious", "warn"),
52 ("restriction", "allow"),
54 ("pedantic", "allow"),
55 ("complexity", "warn"),
60 /// This prefix is in front of the lint groups in the lint store. The prefix will be trimmed
61 /// to only keep the actual lint group in the output.
62 const CLIPPY_LINT_GROUP_PREFIX: &str = "clippy::";
64 /// This template will be used to format the configuration section in the lint documentation.
65 /// The `configurations` parameter will be replaced with one or multiple formatted
66 /// `ClippyConfiguration` instances. See `CONFIGURATION_VALUE_TEMPLATE` for further customizations
67 macro_rules! CONFIGURATION_SECTION_TEMPLATE {
71 This lint has the following configuration variables:
77 /// This template will be used to format an individual `ClippyConfiguration` instance in the
78 /// lint documentation.
80 /// The format function will provide strings for the following parameters: `name`, `ty`, `doc` and
82 macro_rules! CONFIGURATION_VALUE_TEMPLATE {
84 "* `{name}`: `{ty}`: {doc} (defaults to `{default}`)\n"
88 const LINT_EMISSION_FUNCTIONS: [&[&str]; 8] = [
89 &["clippy_utils", "diagnostics", "span_lint"],
90 &["clippy_utils", "diagnostics", "span_lint_and_help"],
91 &["clippy_utils", "diagnostics", "span_lint_and_note"],
92 &["clippy_utils", "diagnostics", "span_lint_hir"],
93 &["clippy_utils", "diagnostics", "span_lint_and_sugg"],
94 &["clippy_utils", "diagnostics", "span_lint_and_then"],
95 &["clippy_utils", "diagnostics", "span_lint_hir_and_then"],
96 &["clippy_utils", "diagnostics", "span_lint_and_sugg_for_edges"],
98 const SUGGESTION_DIAGNOSTIC_BUILDER_METHODS: [(&str, bool); 9] = [
99 ("span_suggestion", false),
100 ("span_suggestion_short", false),
101 ("span_suggestion_verbose", false),
102 ("span_suggestion_hidden", false),
103 ("tool_only_span_suggestion", false),
104 ("multipart_suggestion", true),
105 ("multipart_suggestions", true),
106 ("tool_only_multipart_suggestion", true),
107 ("span_suggestions", true),
109 const SUGGESTION_FUNCTIONS: [&[&str]; 2] = [
110 &["clippy_utils", "diagnostics", "multispan_sugg"],
111 &["clippy_utils", "diagnostics", "multispan_sugg_with_applicability"],
113 const DEPRECATED_LINT_TYPE: [&str; 3] = ["clippy_lints", "deprecated_lints", "ClippyDeprecatedLint"];
115 /// The index of the applicability name of `paths::APPLICABILITY_VALUES`
116 const APPLICABILITY_NAME_INDEX: usize = 2;
117 /// This applicability will be set for unresolved applicability values.
118 const APPLICABILITY_UNRESOLVED_STR: &str = "Unresolved";
119 /// The version that will be displayed if none has been defined
120 const VERION_DEFAULT_STR: &str = "Unknown";
122 declare_clippy_lint! {
124 /// Collects metadata about clippy lints for the website.
126 /// This lint will be used to report problems of syntax parsing. You should hopefully never
127 /// see this but never say never I guess ^^
129 /// ### Why is this bad?
130 /// This is not a bad thing but definitely a hacky way to do it. See
131 /// issue [#4310](https://github.com/rust-lang/rust-clippy/issues/4310) for a discussion
132 /// about the implementation.
134 /// ### Known problems
135 /// Hopefully none. It would be pretty uncool to have a problem here :)
137 /// ### Example output
140 /// "id": "internal_metadata_collector",
142 /// "path": "clippy_lints/src/utils/internal_lints/metadata_collector.rs",
145 /// "group": "clippy::internal",
146 /// "docs": " ### What it does\nCollects metadata about clippy lints for the website. [...] "
149 #[clippy::version = "1.56.0"]
150 pub INTERNAL_METADATA_COLLECTOR,
152 "A busy bee collection metadata about lints"
155 impl_lint_pass!(MetadataCollector => [INTERNAL_METADATA_COLLECTOR]);
157 #[allow(clippy::module_name_repetitions)]
158 #[derive(Debug, Clone)]
159 pub struct MetadataCollector {
160 /// All collected lints
162 /// We use a Heap here to have the lints added in alphabetic order in the export
163 lints: BinaryHeap<LintMetadata>,
164 applicability_info: FxHashMap<String, ApplicabilityInfo>,
165 config: Vec<ClippyConfiguration>,
168 impl MetadataCollector {
169 pub fn new() -> Self {
171 lints: BinaryHeap::<LintMetadata>::default(),
172 applicability_info: FxHashMap::<String, ApplicabilityInfo>::default(),
173 config: collect_configs(),
177 fn get_lint_configs(&self, lint_name: &str) -> Option<String> {
180 .filter(|config| config.lints.iter().any(|lint| lint == lint_name))
181 .map(ToString::to_string)
182 .reduce(|acc, x| acc + &x)
183 .map(|configurations| format!(CONFIGURATION_SECTION_TEMPLATE!(), configurations = configurations))
187 impl Drop for MetadataCollector {
188 /// You might ask: How hacky is this?
191 // The metadata collector gets dropped twice, this makes sure that we only write
192 // when the list is full
193 if self.lints.is_empty() {
197 let mut applicability_info = std::mem::take(&mut self.applicability_info);
199 // Mapping the final data
200 let mut lints = std::mem::take(&mut self.lints).into_sorted_vec();
203 .for_each(|x| x.applicability = Some(applicability_info.remove(&x.id).unwrap_or_default()));
206 if Path::new(OUTPUT_FILE).exists() {
207 fs::remove_file(OUTPUT_FILE).unwrap();
209 let mut file = OpenOptions::new().write(true).create(true).open(OUTPUT_FILE).unwrap();
210 writeln!(file, "{}", serde_json::to_string_pretty(&lints).unwrap()).unwrap();
214 #[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
215 struct LintMetadata {
217 id_span: SerializableSpan,
222 /// This field is only used in the output and will only be
223 /// mapped shortly before the actual output.
224 applicability: Option<ApplicabilityInfo>,
230 id_span: SerializableSpan,
240 level: level.to_string(),
248 #[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
249 struct SerializableSpan {
254 impl std::fmt::Display for SerializableSpan {
255 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
256 write!(f, "{}:{}", self.path.rsplit('/').next().unwrap_or_default(), self.line)
260 impl SerializableSpan {
261 fn from_item(cx: &LateContext<'_>, item: &Item<'_>) -> Self {
262 Self::from_span(cx, item.ident.span)
265 fn from_span(cx: &LateContext<'_>, span: Span) -> Self {
266 let loc: Loc = cx.sess().source_map().lookup_char_pos(span.lo());
269 path: format!("{}", loc.file.name.prefer_remapped()),
275 #[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
276 struct ApplicabilityInfo {
277 /// Indicates if any of the lint emissions uses multiple spans. This is related to
278 /// [rustfix#141](https://github.com/rust-lang/rustfix/issues/141) as such suggestions can
279 /// currently not be applied automatically.
280 is_multi_part_suggestion: bool,
281 applicability: Option<usize>,
284 impl Serialize for ApplicabilityInfo {
285 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
289 let mut s = serializer.serialize_struct("ApplicabilityInfo", 2)?;
290 s.serialize_field("is_multi_part_suggestion", &self.is_multi_part_suggestion)?;
291 if let Some(index) = self.applicability {
294 &paths::APPLICABILITY_VALUES[index][APPLICABILITY_NAME_INDEX],
297 s.serialize_field("applicability", APPLICABILITY_UNRESOLVED_STR)?;
303 // ==================================================================
305 // ==================================================================
306 #[derive(Debug, Clone, Default)]
307 pub struct ClippyConfiguration {
309 config_type: &'static str,
314 deprecation_reason: Option<&'static str>,
317 impl ClippyConfiguration {
320 config_type: &'static str,
322 doc_comment: &'static str,
323 deprecation_reason: Option<&'static str>,
325 let (lints, doc) = parse_config_field_doc(doc_comment)
326 .unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string()));
329 name: to_kebab(name),
339 fn collect_configs() -> Vec<ClippyConfiguration> {
340 crate::utils::conf::metadata::get_configuration_metadata()
343 /// This parses the field documentation of the config struct.
346 /// parse_config_field_doc(cx, "Lint: LINT_NAME_1, LINT_NAME_2. Papa penguin, papa penguin")
351 /// Some(["lint_name_1", "lint_name_2"], "Papa penguin, papa penguin")
353 fn parse_config_field_doc(doc_comment: &str) -> Option<(Vec<String>, String)> {
354 const DOC_START: &str = " Lint: ";
356 if doc_comment.starts_with(DOC_START);
357 if let Some(split_pos) = doc_comment.find('.');
359 let mut doc_comment = doc_comment.to_string();
360 let mut documentation = doc_comment.split_off(split_pos);
363 doc_comment.make_ascii_lowercase();
364 let lints: Vec<String> = doc_comment.split_off(DOC_START.len()).split(", ").map(str::to_string).collect();
366 // Format documentation correctly
367 // split off leading `.` from lint name list and indent for correct formatting
368 documentation = documentation.trim_start_matches('.').trim().replace("\n ", "\n ");
370 Some((lints, documentation))
377 /// Transforms a given `snake_case_string` to a tasty `kebab-case-string`
378 fn to_kebab(config_name: &str) -> String {
379 config_name.replace('_', "-")
382 impl fmt::Display for ClippyConfiguration {
383 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
386 CONFIGURATION_VALUE_TEMPLATE!(),
388 ty = self.config_type,
390 default = self.default
395 // ==================================================================
397 // ==================================================================
398 impl<'hir> LateLintPass<'hir> for MetadataCollector {
399 /// Collecting lint declarations like:
401 /// declare_clippy_lint! {
402 /// /// ### What it does
403 /// /// Something IDK.
409 fn check_item(&mut self, cx: &LateContext<'hir>, item: &'hir Item<'_>) {
410 if let ItemKind::Static(ty, Mutability::Not, _) = item.kind {
414 if is_lint_ref_type(cx, ty);
416 let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
417 if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
418 // metadata extraction
419 if let Some((group, level)) = get_lint_group_and_level_or_lint(cx, &lint_name, item);
420 if let Some(mut docs) = extract_attr_docs_or_lint(cx, item);
422 if let Some(configuration_section) = self.get_lint_configs(&lint_name) {
423 docs.push_str(&configuration_section);
425 let version = get_lint_version(cx, item);
427 self.lints.push(LintMetadata::new(
429 SerializableSpan::from_item(cx, item),
439 if is_deprecated_lint(cx, ty);
441 let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
442 if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
443 // Metadata the little we can get from a deprecated lint
444 if let Some(docs) = extract_attr_docs_or_lint(cx, item);
446 let version = get_lint_version(cx, item);
448 self.lints.push(LintMetadata::new(
450 SerializableSpan::from_item(cx, item),
451 DEPRECATED_LINT_GROUP_STR.to_string(),
452 DEPRECATED_LINT_LEVEL,
461 /// Collecting constant applicability from the actual lint emissions
465 /// span_lint_and_sugg(
469 /// "Le lint message",
470 /// "Here comes help:",
471 /// "#![allow(clippy::all)]",
472 /// Applicability::MachineApplicable, // <-- Extracts this constant value
475 fn check_expr(&mut self, cx: &LateContext<'hir>, expr: &'hir hir::Expr<'_>) {
476 if let Some(args) = match_lint_emission(cx, expr) {
477 let emission_info = extract_emission_info(cx, args);
478 if emission_info.is_empty() {
480 // - src/misc.rs:734:9
481 // - src/methods/mod.rs:3545:13
482 // - src/methods/mod.rs:3496:13
483 // We are basically unable to resolve the lint name itself.
487 for (lint_name, applicability, is_multi_part) in emission_info {
488 let app_info = self.applicability_info.entry(lint_name).or_default();
489 app_info.applicability = applicability;
490 app_info.is_multi_part_suggestion = is_multi_part;
496 // ==================================================================
497 // Lint definition extraction
498 // ==================================================================
499 fn sym_to_string(sym: Symbol) -> String {
500 sym.as_str().to_string()
503 fn extract_attr_docs_or_lint(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> {
504 extract_attr_docs(cx, item).or_else(|| {
505 lint_collection_error_item(cx, item, "could not collect the lint documentation");
510 /// This function collects all documentation that has been added to an item using
511 /// `#[doc = r""]` attributes. Several attributes are aggravated using line breaks
514 /// #[doc = r"Hello world!"]
515 /// #[doc = r"=^.^="]
516 /// struct SomeItem {}
519 /// Would result in `Hello world!\n=^.^=\n`
523 /// This function may modify the doc comment to ensure that the string can be displayed using a
524 /// markdown viewer in Clippy's lint list. The following modifications could be applied:
525 /// * Removal of leading space after a new line. (Important to display tables)
526 /// * Ensures that code blocks only contain language information
527 fn extract_attr_docs(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> {
528 let attrs = cx.tcx.hir().attrs(item.hir_id());
529 let mut lines = attrs.iter().filter_map(ast::Attribute::doc_str);
530 let mut docs = String::from(&*lines.next()?.as_str());
531 let mut in_code_block = false;
532 let mut is_code_block_rust = false;
534 let line = line.as_str();
537 // Rustdoc hides code lines starting with `# ` and this removes them from Clippy's lint list :)
538 if is_code_block_rust && line.trim_start().starts_with("# ") {
542 // The line should be represented in the lint list, even if it's just an empty line
544 if let Some(info) = line.trim_start().strip_prefix("```") {
545 in_code_block = !in_code_block;
546 is_code_block_rust = false;
551 // remove rustdoc directives
552 .find(|&s| !matches!(s, "" | "ignore" | "no_run" | "should_panic"))
553 // if no language is present, fill in "rust"
555 docs.push_str("```");
558 is_code_block_rust = lang == "rust";
562 // This removes the leading space that the macro translation introduces
563 if let Some(stripped_doc) = line.strip_prefix(' ') {
564 docs.push_str(stripped_doc);
565 } else if !line.is_empty() {
572 fn get_lint_version(cx: &LateContext<'_>, item: &Item<'_>) -> String {
573 extract_clippy_version_value(cx, item).map_or_else(
574 || VERION_DEFAULT_STR.to_string(),
575 |version| version.as_str().to_string(),
579 fn get_lint_group_and_level_or_lint(
580 cx: &LateContext<'_>,
583 ) -> Option<(String, &'static str)> {
584 let result = cx.lint_store.check_lint_name(
587 &[Ident::with_dummy_span(sym::clippy)].into_iter().collect(),
589 if let CheckLintNameResult::Tool(Ok(lint_lst)) = result {
590 if let Some(group) = get_lint_group(cx, lint_lst[0]) {
591 if EXCLUDED_LINT_GROUPS.contains(&group.as_str()) {
595 if let Some(level) = get_lint_level_from_group(&group) {
598 lint_collection_error_item(
601 &format!("Unable to determine lint level for found group `{}`", group),
606 lint_collection_error_item(cx, item, "Unable to determine lint group");
610 lint_collection_error_item(cx, item, "Unable to find lint in lint_store");
615 fn get_lint_group(cx: &LateContext<'_>, lint_id: LintId) -> Option<String> {
616 for (group_name, lints, _) in cx.lint_store.get_lint_groups() {
617 if IGNORED_LINT_GROUPS.contains(&group_name) {
621 if lints.iter().any(|group_lint| *group_lint == lint_id) {
622 let group = group_name.strip_prefix(CLIPPY_LINT_GROUP_PREFIX).unwrap_or(group_name);
623 return Some((*group).to_string());
630 fn get_lint_level_from_group(lint_group: &str) -> Option<&'static str> {
633 .find_map(|(group_name, group_level)| (*group_name == lint_group).then(|| *group_level))
636 fn is_deprecated_lint(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
637 if let hir::TyKind::Path(ref path) = ty.kind {
638 if let hir::def::Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, ty.hir_id) {
639 return match_def_path(cx, def_id, &DEPRECATED_LINT_TYPE);
646 // ==================================================================
648 // ==================================================================
649 fn lint_collection_error_item(cx: &LateContext<'_>, item: &Item<'_>, message: &str) {
652 INTERNAL_METADATA_COLLECTOR,
654 &format!("metadata collection error for `{}`: {}", item.ident.name, message),
658 // ==================================================================
660 // ==================================================================
661 /// This function checks if a given expression is equal to a simple lint emission function call.
662 /// It will return the function arguments if the emission matched any function.
663 fn match_lint_emission<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'_>) -> Option<&'hir [hir::Expr<'hir>]> {
664 LINT_EMISSION_FUNCTIONS
666 .find_map(|emission_fn| match_function_call(cx, expr, emission_fn))
669 fn take_higher_applicability(a: Option<usize>, b: Option<usize>) -> Option<usize> {
670 a.map_or(b, |a| a.max(b.unwrap_or_default()).into())
673 fn extract_emission_info<'hir>(
674 cx: &LateContext<'hir>,
675 args: &'hir [hir::Expr<'hir>],
676 ) -> Vec<(String, Option<usize>, bool)> {
677 let mut lints = Vec::new();
678 let mut applicability = None;
679 let mut multi_part = false;
682 let (arg_ty, _) = walk_ptrs_ty_depth(cx.typeck_results().expr_ty(arg));
684 if match_type(cx, arg_ty, &paths::LINT) {
685 // If we found the lint arg, extract the lint name
686 let mut resolved_lints = resolve_lints(cx, arg);
687 lints.append(&mut resolved_lints);
688 } else if match_type(cx, arg_ty, &paths::APPLICABILITY) {
689 applicability = resolve_applicability(cx, arg);
690 } else if arg_ty.is_closure() {
691 multi_part |= check_is_multi_part(cx, arg);
692 applicability = applicability.or_else(|| resolve_applicability(cx, arg));
698 .map(|lint_name| (lint_name, applicability, multi_part))
702 /// Resolves the possible lints that this expression could reference
703 fn resolve_lints<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -> Vec<String> {
704 let mut resolver = LintResolver::new(cx);
705 resolver.visit_expr(expr);
709 /// This function tries to resolve the linked applicability to the given expression.
710 fn resolve_applicability<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -> Option<usize> {
711 let mut resolver = ApplicabilityResolver::new(cx);
712 resolver.visit_expr(expr);
716 fn check_is_multi_part<'hir>(cx: &LateContext<'hir>, closure_expr: &'hir hir::Expr<'hir>) -> bool {
717 if let ExprKind::Closure(_, _, body_id, _, _) = closure_expr.kind {
718 let mut scanner = IsMultiSpanScanner::new(cx);
719 intravisit::walk_body(&mut scanner, cx.tcx.hir().body(body_id));
720 return scanner.is_multi_part();
721 } else if let Some(local) = get_parent_local(cx, closure_expr) {
722 if let Some(local_init) = local.init {
723 return check_is_multi_part(cx, local_init);
730 struct LintResolver<'a, 'hir> {
731 cx: &'a LateContext<'hir>,
735 impl<'a, 'hir> LintResolver<'a, 'hir> {
736 fn new(cx: &'a LateContext<'hir>) -> Self {
739 lints: Vec::<String>::default(),
744 impl<'a, 'hir> intravisit::Visitor<'hir> for LintResolver<'a, 'hir> {
745 type NestedFilter = nested_filter::All;
747 fn nested_visit_map(&mut self) -> Self::Map {
751 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
753 if let ExprKind::Path(qpath) = &expr.kind;
754 if let QPath::Resolved(_, path) = qpath;
756 let (expr_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(expr));
757 if match_type(self.cx, expr_ty, &paths::LINT);
759 if let hir::def::Res::Def(DefKind::Static, _) = path.res {
760 let lint_name = last_path_segment(qpath).ident.name;
761 self.lints.push(sym_to_string(lint_name).to_ascii_lowercase());
762 } else if let Some(local) = get_parent_local(self.cx, expr) {
763 if let Some(local_init) = local.init {
764 intravisit::walk_expr(self, local_init);
770 intravisit::walk_expr(self, expr);
774 /// This visitor finds the highest applicability value in the visited expressions
775 struct ApplicabilityResolver<'a, 'hir> {
776 cx: &'a LateContext<'hir>,
777 /// This is the index of hightest `Applicability` for `paths::APPLICABILITY_VALUES`
778 applicability_index: Option<usize>,
781 impl<'a, 'hir> ApplicabilityResolver<'a, 'hir> {
782 fn new(cx: &'a LateContext<'hir>) -> Self {
785 applicability_index: None,
789 fn add_new_index(&mut self, new_index: usize) {
790 self.applicability_index = take_higher_applicability(self.applicability_index, Some(new_index));
793 fn complete(self) -> Option<usize> {
794 self.applicability_index
798 impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> {
799 type NestedFilter = nested_filter::All;
801 fn nested_visit_map(&mut self) -> Self::Map {
805 fn visit_path(&mut self, path: &'hir hir::Path<'hir>, _id: hir::HirId) {
806 for (index, enum_value) in paths::APPLICABILITY_VALUES.iter().enumerate() {
807 if match_path(path, enum_value) {
808 self.add_new_index(index);
814 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
815 let (expr_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(expr));
818 if match_type(self.cx, expr_ty, &paths::APPLICABILITY);
819 if let Some(local) = get_parent_local(self.cx, expr);
820 if let Some(local_init) = local.init;
822 intravisit::walk_expr(self, local_init);
826 intravisit::walk_expr(self, expr);
830 /// This returns the parent local node if the expression is a reference one
831 fn get_parent_local<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -> Option<&'hir hir::Local<'hir>> {
832 if let ExprKind::Path(QPath::Resolved(_, path)) = expr.kind {
833 if let hir::def::Res::Local(local_hir) = path.res {
834 return get_parent_local_hir_id(cx, local_hir);
841 fn get_parent_local_hir_id<'hir>(cx: &LateContext<'hir>, hir_id: hir::HirId) -> Option<&'hir hir::Local<'hir>> {
842 let map = cx.tcx.hir();
844 match map.find(map.get_parent_node(hir_id)) {
845 Some(hir::Node::Local(local)) => Some(local),
846 Some(hir::Node::Pat(pattern)) => get_parent_local_hir_id(cx, pattern.hir_id),
851 /// This visitor finds the highest applicability value in the visited expressions
852 struct IsMultiSpanScanner<'a, 'hir> {
853 cx: &'a LateContext<'hir>,
854 suggestion_count: usize,
857 impl<'a, 'hir> IsMultiSpanScanner<'a, 'hir> {
858 fn new(cx: &'a LateContext<'hir>) -> Self {
865 /// Add a new single expression suggestion to the counter
866 fn add_single_span_suggestion(&mut self) {
867 self.suggestion_count += 1;
870 /// Signals that a suggestion with possible multiple spans was found
871 fn add_multi_part_suggestion(&mut self) {
872 self.suggestion_count += 2;
875 /// Checks if the suggestions include multiple spanns
876 fn is_multi_part(&self) -> bool {
877 self.suggestion_count > 1
881 impl<'a, 'hir> intravisit::Visitor<'hir> for IsMultiSpanScanner<'a, 'hir> {
882 type NestedFilter = nested_filter::All;
884 fn nested_visit_map(&mut self) -> Self::Map {
888 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
889 // Early return if the lint is already multi span
890 if self.is_multi_part() {
895 ExprKind::Call(fn_expr, _args) => {
896 let found_function = SUGGESTION_FUNCTIONS
898 .any(|func_path| match_function_call(self.cx, fn_expr, func_path).is_some());
900 // These functions are all multi part suggestions
901 self.add_single_span_suggestion();
904 ExprKind::MethodCall(path, arg, _arg_span) => {
905 let (self_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(&arg[0]));
906 if match_type(self.cx, self_ty, &paths::DIAGNOSTIC_BUILDER) {
907 let called_method = path.ident.name.as_str().to_string();
908 for (method_name, is_multi_part) in &SUGGESTION_DIAGNOSTIC_BUILDER_METHODS {
909 if *method_name == called_method {
911 self.add_multi_part_suggestion();
913 self.add_single_span_suggestion();
923 intravisit::walk_expr(self, expr);