Skip to content

Commit aa8306b

Browse files
authored
Add a config option to suppress unmatched-source (#782)
Thank you for the nice project! This is my first PR for this project. Therefore if I have any mistakes for contribution, please let me know... --- There are several reasons you might want this: 1. If the `allow` list represents the set sources for a project that have gone through some external approval process, such as vetting it with a legal department. 2. You're checking a single project in a workspace that shares its `deny.toml` for all members, but not all members have identical dependency sets. 3. You'd like to use `deny.toml` as part of a project template, and configure it with some default set of sources you find acceptable 4. ... others, for sure... As it is, this warning isn't a huge deal, but is annoying/unhelpful if you don't care about it. I suspect that it's useful for catching typos or keeping configuration tight (and so I think "warn" is the right default for it), but there are enough reasons to want to turn it off that it seems justified to me for it to be an option. It was easy to add support for a config property which controls the lint level for this check, so I just did that. I guess setting it to `deny` could be desirable in some cases, although it seems a little dodgy to me for various reasons... That said, I didn't see a reason to forbid that sort of thing, and allowing it to be configured as a `LintLevel` seemed more consistent. ### Additional Information - This PR is for #781 - This PR modification is very similar with #368 - config name, `unused-allowed-source` is inspired by `licenses.unused-allowed-licens` in #368
1 parent 6e82d44 commit aa8306b

File tree

7 files changed

+23
-1
lines changed

7 files changed

+23
-1
lines changed

docs/src/checks/sources/cfg.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,11 @@ Allows you to specify multiple `bitbucket.org` organizations to allow as git sou
122122
[sources.allow-org]
123123
bitbucket = ["YourCoolOrgGoesHere"]
124124
```
125+
126+
### The `unused-allowed-source` field (optional)
127+
128+
Determines what happens when one of the sources that appears in the `allow` list is not encountered in the dependency graph.
129+
130+
- `warn` (default) - A warning is emitted for each source that appears in `sources.allow` but which is not used in any crate.
131+
- `allow` - Unused sources in the `sources.allow` list are ignored.
132+
- `deny` - An unused source in the `sources.allow` list triggers an error, and cause the source check to fail.

docs/src/checks/sources/diags.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ A crate's source was not explicitly allowed.
2121

2222
An allowed source in [`sources.allow-git`](cfg.md#the-allow-git-field-optional) or [`sources.allow-registry`](cfg.md#the-allow-registry-field-optional) was not encountered.
2323

24+
This diagnostic can be silenced by configuring the [`sources.unused-allowed-source`](cfg.md#the-unused-allowed-source-field-optional) field to "allow".
25+
2426
### `unmatched-organization`
2527

2628
An allowed source in [`sources.allow-org`](cfg.md#the-allow-org-field-optional) was not encountered.

src/sources.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, sink: impl Into<ErrorSink>)
161161
}
162162

163163
pack.push(diags::UnmatchedAllowSource {
164+
severity: ctx.cfg.unused_allowed_source.into(),
164165
allow_src_cfg: CfgCoord {
165166
span: src.url.span,
166167
file: ctx.cfg.file_id,

src/sources/cfg.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub struct Config {
9292
/// The minimum specification required for git sources. Defaults to allowing
9393
/// any.
9494
pub required_git_spec: Option<Spanned<GitSpec>>,
95+
/// Determines the response to sources in th `allow`ed list which do not
96+
/// exist in the dependency tree.
97+
pub unused_allowed_source: LintLevel,
9598
}
9699

97100
impl<'de> Deserialize<'de> for Config {
@@ -106,6 +109,7 @@ impl<'de> Deserialize<'de> for Config {
106109
let allow_org = th.optional("allow-org").unwrap_or_default();
107110
let private = th.optional("private").unwrap_or_default();
108111
let required_git_spec = th.optional("required-git-spec");
112+
let unused_allowed_source = th.optional("unused-allowed-source").unwrap_or(LintLevel::Warn);
109113

110114
th.finalize(None)?;
111115

@@ -117,6 +121,7 @@ impl<'de> Deserialize<'de> for Config {
117121
allow_org,
118122
private,
119123
required_git_spec,
124+
unused_allowed_source,
120125
})
121126
}
122127
}
@@ -131,6 +136,7 @@ impl Default for Config {
131136
allow_org: Orgs::default(),
132137
private: Vec::new(),
133138
required_git_spec: None,
139+
unused_allowed_source: LintLevel::Warn,
134140
}
135141
}
136142
}
@@ -213,6 +219,7 @@ impl cfg::UnvalidatedConfig for Config {
213219
allowed_sources,
214220
allowed_orgs,
215221
required_git_spec: self.required_git_spec,
222+
unused_allowed_source: self.unused_allowed_source,
216223
}
217224
}
218225
}
@@ -235,6 +242,7 @@ pub struct ValidConfig {
235242
pub allowed_sources: Vec<UrlSource>,
236243
pub allowed_orgs: Vec<(OrgType, Spanned<String>)>,
237244
pub required_git_spec: Option<Spanned<GitSpec>>,
245+
pub unused_allowed_source: LintLevel,
238246
}
239247

240248
#[cfg(test)]

src/sources/diags.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ impl<'a> From<SourceNotExplicitlyAllowed<'a>> for Diag {
117117
}
118118

119119
pub(crate) struct UnmatchedAllowSource {
120+
pub(crate) severity: Severity,
120121
pub(crate) allow_src_cfg: CfgCoord,
121122
}
122123

123124
impl From<UnmatchedAllowSource> for Diag {
124125
fn from(uas: UnmatchedAllowSource) -> Self {
125-
Diagnostic::new(Severity::Warning)
126+
Diagnostic::new(uas.severity)
126127
.with_message("allowed source was not encountered")
127128
.with_code(Code::UnmatchedSource)
128129
.with_labels(vec![

src/sources/snapshots/cargo_deny__sources__cfg__test__deserializes_sources_cfg-2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ ValidConfig {
4545
required_git_spec: Some(
4646
Tag,
4747
),
48+
unused_allowed_source: Warn,
4849
}

tests/cfg/sources.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ allow-git = [
1212
private = [
1313
"https://internal-host/repos",
1414
]
15+
unused-allowed-source = "warn"
1516
[sources.allow-org]
1617
github = [
1718
"yourghid",

0 commit comments

Comments
 (0)