feat(implicit_features): Show why lint was emiited

This commit is contained in:
Scott Schafer 2024-04-19 11:41:28 -06:00
parent 7aac7f875c
commit 8d676dde40
No known key found for this signature in database
3 changed files with 43 additions and 19 deletions

View File

@ -6,6 +6,7 @@ use annotate_snippets::{Level, Renderer, Snippet};
use cargo_util_schemas::manifest::{TomlLintLevel, TomlToolLints};
use pathdiff::diff_paths;
use std::collections::HashSet;
use std::fmt::Display;
use std::ops::Range;
use std::path::Path;
use toml_edit::ImDocument;
@ -107,6 +108,17 @@ pub enum LintLevel {
Forbid,
}
impl Display for LintLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LintLevel::Allow => write!(f, "allow"),
LintLevel::Warn => write!(f, "warn"),
LintLevel::Deny => write!(f, "deny"),
LintLevel::Forbid => write!(f, "forbid"),
}
}
}
impl LintLevel {
pub fn to_diagnostic_level(self) -> Level {
match self {
@ -184,6 +196,7 @@ pub fn check_implicit_features(
})
.unwrap_or_default();
let mut emitted_source = None;
for dep in manifest.dependencies() {
let dep_name_in_toml = dep.name_in_toml();
if !dep.is_optional() || activated_opt_deps.contains(dep_name_in_toml.as_str()) {
@ -200,12 +213,19 @@ pub fn check_implicit_features(
}
let level = lint_level.to_diagnostic_level();
let manifest_path = rel_cwd_manifest_path(path, gctx);
let message = level.title(IMPLICIT_FEATURES.desc).snippet(
let mut message = level.title(IMPLICIT_FEATURES.desc).snippet(
Snippet::source(manifest.contents())
.origin(&manifest_path)
.annotation(level.span(get_span(manifest.document(), &toml_path, false).unwrap()))
.fold(true),
);
if emitted_source.is_none() {
emitted_source = Some(format!(
"`cargo::{}` is set to `{lint_level}`",
IMPLICIT_FEATURES.name
));
message = message.footer(Level::Note.title(emitted_source.as_ref().unwrap()));
}
let renderer = Renderer::styled().term_width(
gctx.shell()
.err_width()

View File

@ -1,8 +1,9 @@
<svg width="944px" height="434px" xmlns="http://www.w3.org/2000/svg">
<svg width="944px" height="452px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
.fg-bright-blue { fill: #5555FF }
.fg-bright-green { fill: #55FF55 }
.fg-green { fill: #00AA00 }
.fg-yellow { fill: #AA5500 }
.container {
@ -32,39 +33,41 @@
</tspan>
<tspan x="10px" y="118px"><tspan class="fg-bright-blue bold"> |</tspan>
</tspan>
<tspan x="10px" y="136px"><tspan class="fg-yellow bold">warning</tspan><tspan>: </tspan><tspan class="bold">implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition</tspan>
<tspan x="10px" y="136px"><tspan> </tspan><tspan class="fg-bright-blue bold">=</tspan><tspan> </tspan><tspan class="fg-bright-green bold">note</tspan><tspan>: `cargo::implicit_features` is set to `warn`</tspan>
</tspan>
<tspan x="10px" y="154px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:11:1</tspan>
<tspan x="10px" y="154px"><tspan class="fg-yellow bold">warning</tspan><tspan>: </tspan><tspan class="bold">implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition</tspan>
</tspan>
<tspan x="10px" y="172px"><tspan class="fg-bright-blue bold"> |</tspan>
<tspan x="10px" y="172px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:11:1</tspan>
</tspan>
<tspan x="10px" y="190px"><tspan class="fg-bright-blue bold">11 |</tspan><tspan> baz = { version = "0.1.0", optional = true }</tspan>
<tspan x="10px" y="190px"><tspan class="fg-bright-blue bold"> |</tspan>
</tspan>
<tspan x="10px" y="208px"><tspan class="fg-bright-blue bold"> |</tspan><tspan class="fg-yellow bold"> ---</tspan>
<tspan x="10px" y="208px"><tspan class="fg-bright-blue bold">11 |</tspan><tspan> baz = { version = "0.1.0", optional = true }</tspan>
</tspan>
<tspan x="10px" y="226px"><tspan class="fg-bright-blue bold"> |</tspan>
<tspan x="10px" y="226px"><tspan class="fg-bright-blue bold"> |</tspan><tspan class="fg-yellow bold"> ---</tspan>
</tspan>
<tspan x="10px" y="244px"><tspan class="fg-yellow bold">warning</tspan><tspan>: </tspan><tspan class="bold">implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition</tspan>
<tspan x="10px" y="244px"><tspan class="fg-bright-blue bold"> |</tspan>
</tspan>
<tspan x="10px" y="262px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:14:1</tspan>
<tspan x="10px" y="262px"><tspan class="fg-yellow bold">warning</tspan><tspan>: </tspan><tspan class="bold">implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition</tspan>
</tspan>
<tspan x="10px" y="280px"><tspan class="fg-bright-blue bold"> |</tspan>
<tspan x="10px" y="280px"><tspan> </tspan><tspan class="fg-bright-blue bold">--&gt;</tspan><tspan> Cargo.toml:14:1</tspan>
</tspan>
<tspan x="10px" y="298px"><tspan class="fg-bright-blue bold">14 |</tspan><tspan> target-dep = { version = "0.1.0", optional = true }</tspan>
<tspan x="10px" y="298px"><tspan class="fg-bright-blue bold"> |</tspan>
</tspan>
<tspan x="10px" y="316px"><tspan class="fg-bright-blue bold"> |</tspan><tspan class="fg-yellow bold"> ----------</tspan>
<tspan x="10px" y="316px"><tspan class="fg-bright-blue bold">14 |</tspan><tspan> target-dep = { version = "0.1.0", optional = true }</tspan>
</tspan>
<tspan x="10px" y="334px"><tspan class="fg-bright-blue bold"> |</tspan>
<tspan x="10px" y="334px"><tspan class="fg-bright-blue bold"> |</tspan><tspan class="fg-yellow bold"> ----------</tspan>
</tspan>
<tspan x="10px" y="352px"><tspan class="fg-green bold"> Updating</tspan><tspan> `dummy-registry` index</tspan>
<tspan x="10px" y="352px"><tspan class="fg-bright-blue bold"> |</tspan>
</tspan>
<tspan x="10px" y="370px"><tspan class="fg-green bold"> Locking</tspan><tspan> 4 packages to latest compatible versions</tspan>
<tspan x="10px" y="370px"><tspan class="fg-green bold"> Updating</tspan><tspan> `dummy-registry` index</tspan>
</tspan>
<tspan x="10px" y="388px"><tspan class="fg-green bold"> Checking</tspan><tspan> foo v0.1.0 ([ROOT]/foo)</tspan>
<tspan x="10px" y="388px"><tspan class="fg-green bold"> Locking</tspan><tspan> 4 packages to latest compatible versions</tspan>
</tspan>
<tspan x="10px" y="406px"><tspan class="fg-green bold"> Finished</tspan><tspan> [..]</tspan>
<tspan x="10px" y="406px"><tspan class="fg-green bold"> Checking</tspan><tspan> foo v0.1.0 ([ROOT]/foo)</tspan>
</tspan>
<tspan x="10px" y="424px">
<tspan x="10px" y="424px"><tspan class="fg-green bold"> Finished</tspan><tspan> [..]</tspan>
</tspan>
<tspan x="10px" y="442px">
</tspan>
</text>

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

@ -880,6 +880,7 @@ warning: implicit features for optional dependencies is deprecated and will be u
12 | bar = { version = \"0.1.0\", optional = true }
| ---
|
= note: `cargo::implicit_features` is set to `warn`
[UPDATING] `dummy-registry` index
[LOCKING] [..]
[CHECKING] foo v0.0.1 ([CWD])