Skip to content

Commit a1d6135

Browse files
committed
feat(lints): Add where lint level was set
1 parent 426d526 commit a1d6135

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed

src/cargo/util/lints.rs

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,36 @@ pub trait LevelTrait {
6969
pkg_lints: &TomlToolLints,
7070
ws_lints: &TomlToolLints,
7171
edition: Edition,
72-
) -> (LintLevel, i8) {
73-
let unspecified_level = if let Some(level) = self
72+
) -> (LintLevel, LintLevelReason, i8) {
73+
let (unspecified_level, reason) = if let Some(level) = self
7474
.edition_lint_opts()
7575
.filter(|(e, _)| edition >= *e)
7676
.map(|(_, l)| l)
7777
{
78-
level
78+
(level, LintLevelReason::Edition(edition))
7979
} else {
80-
self.default_level()
80+
(self.default_level(), LintLevelReason::Default)
8181
};
8282

8383
// Don't allow the group to be overridden if the level is `Forbid`
8484
if unspecified_level == LintLevel::Forbid {
85-
return (unspecified_level, 0);
85+
return (unspecified_level, reason, 0);
8686
}
8787

8888
if let Some(defined_level) = pkg_lints.get(self.name()) {
89-
(defined_level.level().into(), defined_level.priority())
89+
(
90+
defined_level.level().into(),
91+
LintLevelReason::Package,
92+
defined_level.priority(),
93+
)
9094
} else if let Some(defined_level) = ws_lints.get(self.name()) {
91-
(defined_level.level().into(), defined_level.priority())
95+
(
96+
defined_level.level().into(),
97+
LintLevelReason::Workspace,
98+
defined_level.priority(),
99+
)
92100
} else {
93-
(unspecified_level, 0)
101+
(unspecified_level, reason, 0)
94102
}
95103
}
96104
}
@@ -153,16 +161,16 @@ impl Lint {
153161
lints: &TomlToolLints,
154162
ws_lints: &TomlToolLints,
155163
edition: Edition,
156-
) -> LintLevel {
164+
) -> (LintLevel, LintLevelReason) {
157165
self.groups
158166
.iter()
159167
.map(|g| (g.name, g.level_priority(lints, ws_lints, edition)))
160168
.chain(std::iter::once((
161169
self.name,
162170
self.level_priority(lints, ws_lints, edition),
163171
)))
164-
.max_by_key(|(n, (l, p))| (l == &LintLevel::Forbid, *p, std::cmp::Reverse(*n)))
165-
.map(|(_, (l, _))| l)
172+
.max_by_key(|(n, (l, _, p))| (l == &LintLevel::Forbid, *p, std::cmp::Reverse(*n)))
173+
.map(|(_, (l, r, _))| (l, r))
166174
.unwrap()
167175
}
168176
}
@@ -208,6 +216,25 @@ impl From<TomlLintLevel> for LintLevel {
208216
}
209217
}
210218

219+
#[derive(Copy, Clone, Debug)]
220+
pub enum LintLevelReason {
221+
Default,
222+
Edition(Edition),
223+
Package,
224+
Workspace,
225+
}
226+
227+
impl Display for LintLevelReason {
228+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
229+
match self {
230+
LintLevelReason::Default => write!(f, "by default"),
231+
LintLevelReason::Edition(edition) => write!(f, "in edition {}", edition),
232+
LintLevelReason::Package => write!(f, "in `[lints]`"),
233+
LintLevelReason::Workspace => write!(f, "in `[workspace.lints]`"),
234+
}
235+
}
236+
}
237+
211238
const IM_A_TEAPOT: Lint = Lint {
212239
name: "im_a_teapot",
213240
desc: "`im_a_teapot` is specified",
@@ -225,7 +252,7 @@ pub fn check_im_a_teapot(
225252
gctx: &GlobalContext,
226253
) -> CargoResult<()> {
227254
let manifest = pkg.manifest();
228-
let lint_level = IM_A_TEAPOT.level(pkg_lints, ws_lints, manifest.edition());
255+
let (lint_level, reason) = IM_A_TEAPOT.level(pkg_lints, ws_lints, manifest.edition());
229256
if lint_level == LintLevel::Allow {
230257
return Ok(());
231258
}
@@ -240,7 +267,10 @@ pub fn check_im_a_teapot(
240267
}
241268
let level = lint_level.to_diagnostic_level();
242269
let manifest_path = rel_cwd_manifest_path(path, gctx);
243-
let emitted_reason = format!("`cargo::{}` is set to `{lint_level}`", IM_A_TEAPOT.name);
270+
let emitted_reason = format!(
271+
"`cargo::{}` is set to `{lint_level}` {reason}",
272+
IM_A_TEAPOT.name
273+
);
244274

245275
let key_span = get_span(manifest.document(), &["package", "im-a-teapot"], false).unwrap();
246276
let value_span = get_span(manifest.document(), &["package", "im-a-teapot"], true).unwrap();
@@ -300,7 +330,7 @@ pub fn check_implicit_features(
300330
return Ok(());
301331
}
302332

303-
let lint_level = IMPLICIT_FEATURES.level(pkg_lints, ws_lints, edition);
333+
let (lint_level, reason) = IMPLICIT_FEATURES.level(pkg_lints, ws_lints, edition);
304334
if lint_level == LintLevel::Allow {
305335
return Ok(());
306336
}
@@ -345,7 +375,7 @@ pub fn check_implicit_features(
345375
);
346376
if emitted_source.is_none() {
347377
emitted_source = Some(format!(
348-
"`cargo::{}` is set to `{lint_level}`",
378+
"`cargo::{}` is set to `{lint_level}` {reason}",
349379
IMPLICIT_FEATURES.name
350380
));
351381
message = message.footer(Level::Note.title(emitted_source.as_ref().unwrap()));
@@ -383,7 +413,7 @@ pub fn unused_dependencies(
383413
return Ok(());
384414
}
385415

386-
let lint_level = UNUSED_OPTIONAL_DEPENDENCY.level(pkg_lints, ws_lints, edition);
416+
let (lint_level, reason) = UNUSED_OPTIONAL_DEPENDENCY.level(pkg_lints, ws_lints, edition);
387417
if lint_level == LintLevel::Allow {
388418
return Ok(());
389419
}
@@ -449,7 +479,7 @@ pub fn unused_dependencies(
449479
);
450480
if emitted_source.is_none() {
451481
emitted_source = Some(format!(
452-
"`cargo::{}` is set to `{lint_level}`",
482+
"`cargo::{}` is set to `{lint_level}` {reason}",
453483
UNUSED_OPTIONAL_DEPENDENCY.name
454484
));
455485
message =

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

Lines changed: 1 addition & 1 deletion
Loading

tests/testsuite/lints/unused_optional_dependencies/edition_2024/stderr.term.svg

Lines changed: 1 addition & 1 deletion
Loading

tests/testsuite/lints/unused_optional_dependencies/renamed_deps/stderr.term.svg

Lines changed: 1 addition & 1 deletion
Loading

tests/testsuite/lints_table.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ warning: `im_a_teapot` is specified
852852
9 | im-a-teapot = true
853853
| ------------------
854854
|
855-
= note: `cargo::im_a_teapot` is set to `warn`
855+
= note: `cargo::im_a_teapot` is set to `warn` in `[lints]`
856856
[CHECKING] foo v0.0.1 ([CWD])
857857
[FINISHED] [..]
858858
",
@@ -892,7 +892,7 @@ warning: `im_a_teapot` is specified
892892
9 | im-a-teapot = true
893893
| ------------------
894894
|
895-
= note: `cargo::im_a_teapot` is set to `warn`
895+
= note: `cargo::im_a_teapot` is set to `warn` in `[lints]`
896896
[CHECKING] foo v0.0.1 ([CWD])
897897
[FINISHED] [..]
898898
",
@@ -934,7 +934,7 @@ error: `im_a_teapot` is specified
934934
9 | im-a-teapot = true
935935
| ^^^^^^^^^^^^^^^^^^
936936
|
937-
= note: `cargo::im_a_teapot` is set to `forbid`
937+
= note: `cargo::im_a_teapot` is set to `forbid` in `[lints]`
938938
",
939939
)
940940
.run();
@@ -977,7 +977,7 @@ error: `im_a_teapot` is specified
977977
13 | im-a-teapot = true
978978
| ^^^^^^^^^^^^^^^^^^
979979
|
980-
= note: `cargo::im_a_teapot` is set to `forbid`
980+
= note: `cargo::im_a_teapot` is set to `forbid` in `[workspace.lints]`
981981
",
982982
)
983983
.run();

0 commit comments

Comments
 (0)