Skip to content

Commit 8d676dd

Browse files
committed
feat(implicit_features): Show why lint was emiited
1 parent 7aac7f8 commit 8d676dd

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

src/cargo/util/lints.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use annotate_snippets::{Level, Renderer, Snippet};
66
use cargo_util_schemas::manifest::{TomlLintLevel, TomlToolLints};
77
use pathdiff::diff_paths;
88
use std::collections::HashSet;
9+
use std::fmt::Display;
910
use std::ops::Range;
1011
use std::path::Path;
1112
use toml_edit::ImDocument;
@@ -107,6 +108,17 @@ pub enum LintLevel {
107108
Forbid,
108109
}
109110

111+
impl Display for LintLevel {
112+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113+
match self {
114+
LintLevel::Allow => write!(f, "allow"),
115+
LintLevel::Warn => write!(f, "warn"),
116+
LintLevel::Deny => write!(f, "deny"),
117+
LintLevel::Forbid => write!(f, "forbid"),
118+
}
119+
}
120+
}
121+
110122
impl LintLevel {
111123
pub fn to_diagnostic_level(self) -> Level {
112124
match self {
@@ -184,6 +196,7 @@ pub fn check_implicit_features(
184196
})
185197
.unwrap_or_default();
186198

199+
let mut emitted_source = None;
187200
for dep in manifest.dependencies() {
188201
let dep_name_in_toml = dep.name_in_toml();
189202
if !dep.is_optional() || activated_opt_deps.contains(dep_name_in_toml.as_str()) {
@@ -200,12 +213,19 @@ pub fn check_implicit_features(
200213
}
201214
let level = lint_level.to_diagnostic_level();
202215
let manifest_path = rel_cwd_manifest_path(path, gctx);
203-
let message = level.title(IMPLICIT_FEATURES.desc).snippet(
216+
let mut message = level.title(IMPLICIT_FEATURES.desc).snippet(
204217
Snippet::source(manifest.contents())
205218
.origin(&manifest_path)
206219
.annotation(level.span(get_span(manifest.document(), &toml_path, false).unwrap()))
207220
.fold(true),
208221
);
222+
if emitted_source.is_none() {
223+
emitted_source = Some(format!(
224+
"`cargo::{}` is set to `{lint_level}`",
225+
IMPLICIT_FEATURES.name
226+
));
227+
message = message.footer(Level::Note.title(emitted_source.as_ref().unwrap()));
228+
}
209229
let renderer = Renderer::styled().term_width(
210230
gctx.shell()
211231
.err_width()

tests/testsuite/lints/implicit_features/edition_2021_warn/stderr.term.svg

Lines changed: 21 additions & 18 deletions
Loading

tests/testsuite/lints_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ warning: implicit features for optional dependencies is deprecated and will be u
880880
12 | bar = { version = \"0.1.0\", optional = true }
881881
| ---
882882
|
883+
= note: `cargo::implicit_features` is set to `warn`
883884
[UPDATING] `dummy-registry` index
884885
[LOCKING] [..]
885886
[CHECKING] foo v0.0.1 ([CWD])

0 commit comments

Comments
 (0)