Skip to content

Commit ba58b1e

Browse files
authored
Fix feature ref diagnostic to include plain dep enablements (#355)
Resolves #354 The "used in feature" diagnostic now includes plain dep enablements, like "foo" instead of "dep:foo". ``` shear/unused_optional_dependency ⚠ unused optional dependency `smol` ╭─[sqlx-macros-core/Cargo.toml:64:1] 63 │ async-std = { workspace = true, optional = true } 64 │ smol = { workspace = true, optional = true } · ──┬─ · ╰── not used in code 65 │ tokio = { workspace = true, optional = true } ╰──── Advice: ☞ removing an optional dependency may be a breaking change Advice: ☞ used in feature `_rt-smol` ╭─[sqlx-macros-core/Cargo.toml:17:13] 16 │ _rt-async-std = ["async-std", "sqlx-core/_rt-async-std"] 17 │ _rt-smol = ["smol", "sqlx-core/_rt-smol"] · ───┬── · ╰── enabled here 18 │ _rt-tokio = ["tokio", "sqlx-core/_rt-tokio"] ╰──── ```
1 parent 6d29872 commit ba58b1e

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/dependency_analyzer.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ pub enum FeatureRef {
4040
/// [features]
4141
/// feature = ["dep:foo"]
4242
/// ```
43+
///
44+
/// ```toml
45+
/// [features]
46+
/// feature = ["foo"]
47+
/// ```
4348
Explicit { feature: Spanned<String>, value: Spanned<String> },
4449

4550
/// Dependency feature enablement.
@@ -60,14 +65,11 @@ pub enum FeatureRef {
6065
}
6166

6267
impl FeatureRef {
63-
fn parse(feature: &Spanned<String>, value: &Spanned<String>) -> Option<(String, Self)> {
68+
fn parse(feature: &Spanned<String>, value: &Spanned<String>) -> (String, Self) {
6469
// Handle `dep:foo` syntax
6570
if let Some(dep) = value.as_ref().strip_prefix("dep:") {
6671
let import = dep.replace('-', "_");
67-
return Some((
68-
import,
69-
Self::Explicit { feature: feature.clone(), value: value.clone() },
70-
));
72+
return (import, Self::Explicit { feature: feature.clone(), value: value.clone() });
7173
}
7274

7375
// Handle `foo/bar` and `foo?/bar` syntax
@@ -83,11 +85,13 @@ impl FeatureRef {
8385
Self::DepFeature { feature: feature.clone(), value: value.clone() }
8486
};
8587

86-
return Some((import, feature));
88+
return (import, feature);
8789
}
8890

89-
// Assume this is enabling another feature
90-
None
91+
// Assume this is enabling another feature.
92+
// May be a dependency, so worth tracking.
93+
let import = value.as_ref().replace('-', "_");
94+
(import, Self::Explicit { feature: feature.clone(), value: value.clone() })
9195
}
9296
}
9397

@@ -285,9 +289,8 @@ impl DependencyAnalyzer {
285289
fn analyze_features(categorized: &mut CategorizedImports, manifest: &Manifest) {
286290
for (feature, values) in &manifest.features {
287291
for value in values {
288-
if let Some((import, feature)) = FeatureRef::parse(feature, value) {
289-
categorized.features.entry(import).or_default().push(feature);
290-
}
292+
let (import, feature) = FeatureRef::parse(feature, value);
293+
categorized.features.entry(import).or_default().push(feature);
291294
}
292295
}
293296

0 commit comments

Comments
 (0)