|
| 1 | +use rustc_ast::ast::{Attribute, Crate, MacCall, MetaItem, MetaItemKind}; |
| 2 | +use rustc_data_structures::fx::FxHashSet; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_lint::{EarlyContext, EarlyLintPass}; |
| 5 | +use rustc_parse::{self, MACRO_ARGUMENTS}; |
| 6 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 7 | +use rustc_span::source_map::DUMMY_SP; |
| 8 | + |
| 9 | +use crate::utils::{span_lint, span_lint_and_then}; |
| 10 | +use cargo_metadata::MetadataCommand; |
| 11 | +use strsim::normalized_damerau_levenshtein; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// **What it does:** Finds references to features not defined in the cargo manifest file. |
| 15 | + /// |
| 16 | + /// **Why is this bad?** The referred feature will not be recognised and the related item will not be included |
| 17 | + /// by the conditional compilation engine. |
| 18 | + /// |
| 19 | + /// **Known problems:** None. |
| 20 | + /// |
| 21 | + /// **Example:** |
| 22 | + /// |
| 23 | + /// ```rust |
| 24 | + /// #[cfg(feature = "unknown")] |
| 25 | + /// fn example() { } |
| 26 | + /// ``` |
| 27 | + pub UNKNOWN_FEATURES, |
| 28 | + cargo, |
| 29 | + "usage of features not defined in the cargo manifest file" |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Default)] |
| 33 | +pub struct UnknownFeatures { |
| 34 | + features: FxHashSet<String>, |
| 35 | +} |
| 36 | + |
| 37 | +impl_lint_pass!(UnknownFeatures => [UNKNOWN_FEATURES]); |
| 38 | + |
| 39 | +impl EarlyLintPass for UnknownFeatures { |
| 40 | + fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) { |
| 41 | + fn transform_feature(name: &str, pkg: &str, local_pkg: &str) -> String { |
| 42 | + if pkg == local_pkg { |
| 43 | + name.into() |
| 44 | + } else { |
| 45 | + format!("{}/{}", pkg, name) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + let metadata = if let Ok(metadata) = MetadataCommand::new().exec() { |
| 50 | + metadata |
| 51 | + } else { |
| 52 | + span_lint(cx, UNKNOWN_FEATURES, DUMMY_SP, "could not read cargo metadata"); |
| 53 | + return; |
| 54 | + }; |
| 55 | + |
| 56 | + if let Some(local_pkg) = &cx.sess.opts.crate_name { |
| 57 | + for pkg in metadata.packages { |
| 58 | + self.features.extend( |
| 59 | + pkg.features |
| 60 | + .keys() |
| 61 | + .map(|name| transform_feature(name, &pkg.name, local_pkg)), |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) { |
| 68 | + if attr.check_name(sym!(cfg)) { |
| 69 | + if let Some(item) = &attr.meta() { |
| 70 | + self.walk_cfg_metas(cx, item); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) { |
| 76 | + if mac.path == sym!(cfg) { |
| 77 | + let tts = mac.args.inner_tokens(); |
| 78 | + let mut parser = rustc_parse::stream_to_parser(&cx.sess.parse_sess, tts, MACRO_ARGUMENTS); |
| 79 | + if let Ok(item) = parser.parse_meta_item() { |
| 80 | + self.walk_cfg_metas(cx, &item); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl UnknownFeatures { |
| 87 | + fn walk_cfg_metas(&mut self, cx: &EarlyContext<'_>, item: &MetaItem) { |
| 88 | + match &item.kind { |
| 89 | + MetaItemKind::List(items) => { |
| 90 | + for nested in items { |
| 91 | + if let Some(item) = nested.meta_item() { |
| 92 | + self.walk_cfg_metas(cx, item); |
| 93 | + } |
| 94 | + } |
| 95 | + }, |
| 96 | + MetaItemKind::NameValue(lit) if item.name_or_empty().as_str() == "feature" => { |
| 97 | + if let Some(value) = item.value_str() { |
| 98 | + let feature = &*value.as_str(); |
| 99 | + if !self.features.contains(feature) { |
| 100 | + let message = format!("unknown feature `{}`", feature); |
| 101 | + span_lint_and_then(cx, UNKNOWN_FEATURES, lit.span, &message, |diag| { |
| 102 | + if let Some(similar_name) = self.find_similar_name(feature) { |
| 103 | + diag.span_suggestion( |
| 104 | + lit.span, |
| 105 | + "a feature with a similar name exists", |
| 106 | + format!("\"{}\"", similar_name), |
| 107 | + Applicability::MaybeIncorrect, |
| 108 | + ); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + }, |
| 114 | + _ => {}, |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn find_similar_name(&self, name: &str) -> Option<String> { |
| 119 | + let mut similar: Vec<_> = self |
| 120 | + .features |
| 121 | + .iter() |
| 122 | + .map(|f| (f, normalized_damerau_levenshtein(name, f))) |
| 123 | + .filter(|(_, sim)| *sim >= 0.7) |
| 124 | + .collect(); |
| 125 | + |
| 126 | + similar.sort_by(|(_, a), (_, b)| b.partial_cmp(a).unwrap()); |
| 127 | + similar.into_iter().next().map(|(f, _)| f.clone()) |
| 128 | + } |
| 129 | +} |
0 commit comments