1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
3 use rustc_hir::CRATE_HIR_ID;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::DUMMY_SP;
10 /// Checks for feature names with prefix `use-`, `with-` or suffix `-support`
12 /// ### Why is this bad?
13 /// These prefixes and suffixes have no significant meaning.
17 /// # The `Cargo.toml` with feature name redundancy
19 /// default = ["use-abc", "with-def", "ghi-support"]
20 /// use-abc = [] // redundant
21 /// with-def = [] // redundant
22 /// ghi-support = [] // redundant
28 /// default = ["abc", "def", "ghi"]
34 pub REDUNDANT_FEATURE_NAMES,
36 "usage of a redundant feature name"
39 declare_clippy_lint! {
41 /// Checks for negative feature names with prefix `no-` or `not-`
43 /// ### Why is this bad?
44 /// Features are supposed to be additive, and negatively-named features violate it.
48 /// # The `Cargo.toml` with negative feature names
58 /// default = ["abc", "def"]
63 pub NEGATIVE_FEATURE_NAMES,
65 "usage of a negative feature name"
68 declare_lint_pass!(FeatureName => [REDUNDANT_FEATURE_NAMES, NEGATIVE_FEATURE_NAMES]);
70 static PREFIXES: [&str; 8] = ["no-", "no_", "not-", "not_", "use-", "use_", "with-", "with_"];
71 static SUFFIXES: [&str; 2] = ["-support", "_support"];
73 fn is_negative_prefix(s: &str) -> bool {
77 fn lint(cx: &LateContext<'_>, feature: &str, substring: &str, is_prefix: bool) {
78 let is_negative = is_prefix && is_negative_prefix(substring);
82 NEGATIVE_FEATURE_NAMES
84 REDUNDANT_FEATURE_NAMES
88 "the \"{}\" {} in the feature name \"{}\" is {}",
90 if is_prefix { "prefix" } else { "suffix" },
92 if is_negative { "negative" } else { "redundant" }
96 "consider renaming the feature to \"{}\"{}",
98 feature.strip_prefix(substring)
100 feature.strip_suffix(substring)
104 ", but make sure the feature adds functionality"
112 impl LateLintPass<'_> for FeatureName {
113 fn check_crate(&mut self, cx: &LateContext<'_>) {
114 if is_lint_allowed(cx, REDUNDANT_FEATURE_NAMES, CRATE_HIR_ID)
115 && is_lint_allowed(cx, NEGATIVE_FEATURE_NAMES, CRATE_HIR_ID)
120 let metadata = unwrap_cargo_metadata!(cx, REDUNDANT_FEATURE_NAMES, false);
122 for package in metadata.packages {
123 let mut features: Vec<&String> = package.features.keys().collect();
125 for feature in features {
127 let i = PREFIXES.partition_point(|prefix| prefix < &feature.as_str());
128 if i > 0 && feature.starts_with(PREFIXES[i - 1]) {
129 Some(PREFIXES[i - 1])
134 if let Some(prefix) = prefix_opt {
135 lint(cx, feature, prefix, true);
138 let suffix_opt: Option<&str> = {
139 let i = SUFFIXES.partition_point(|suffix| {
140 suffix.bytes().rev().cmp(feature.bytes().rev()) == std::cmp::Ordering::Less
142 if i > 0 && feature.ends_with(SUFFIXES[i - 1]) {
143 Some(SUFFIXES[i - 1])
148 if let Some(suffix) = suffix_opt {
149 lint(cx, feature, suffix, false);
157 fn test_prefixes_sorted() {
158 let mut sorted_prefixes = PREFIXES;
159 sorted_prefixes.sort_unstable();
160 assert_eq!(PREFIXES, sorted_prefixes);
161 let mut sorted_suffixes = SUFFIXES;
162 sorted_suffixes.sort_by(|a, b| a.bytes().rev().cmp(b.bytes().rev()));
163 assert_eq!(SUFFIXES, sorted_suffixes);