Skip to content

Commit 81db0af

Browse files
authored
Linting system (#15865)
This PR is just a few small improvements to the linting system.
2 parents b2c0aea + 069723e commit 81db0af

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

src/cargo/util/lints.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ impl Lint {
307307
.map(|(_, (l, r, _))| (l, r))
308308
.unwrap()
309309
}
310+
311+
fn emitted_source(&self, lint_level: LintLevel, reason: LintLevelReason) -> String {
312+
format!("`cargo::{}` is set to `{lint_level}` {reason}", self.name,)
313+
}
310314
}
311315

312316
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -329,6 +333,10 @@ impl Display for LintLevel {
329333
}
330334

331335
impl LintLevel {
336+
pub fn is_error(&self) -> bool {
337+
self == &LintLevel::Forbid || self == &LintLevel::Deny
338+
}
339+
332340
pub fn to_diagnostic_level(self) -> Level {
333341
match self {
334342
LintLevel::Allow => unreachable!("allow does not map to a diagnostic level"),
@@ -440,15 +448,12 @@ pub fn check_im_a_teapot(
440448
.package()
441449
.is_some_and(|p| p.im_a_teapot.is_some())
442450
{
443-
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny {
451+
if lint_level.is_error() {
444452
*error_count += 1;
445453
}
446454
let level = lint_level.to_diagnostic_level();
447455
let manifest_path = rel_cwd_manifest_path(path, gctx);
448-
let emitted_reason = format!(
449-
"`cargo::{}` is set to `{lint_level}` {reason}",
450-
IM_A_TEAPOT.name
451-
);
456+
let emitted_reason = IM_A_TEAPOT.emitted_source(lint_level, reason);
452457

453458
let key_span = get_span(manifest.document(), &["package", "im-a-teapot"], false).unwrap();
454459
let value_span = get_span(manifest.document(), &["package", "im-a-teapot"], true).unwrap();
@@ -514,7 +519,7 @@ fn output_unknown_lints(
514519
let level = lint_level.to_diagnostic_level();
515520
let mut emitted_source = None;
516521
for lint_name in unknown_lints {
517-
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny {
522+
if lint_level.is_error() {
518523
*error_count += 1;
519524
}
520525
let title = format!("{}: `{lint_name}`", UNKNOWN_LINTS.desc);
@@ -530,6 +535,12 @@ fn output_unknown_lints(
530535
let help =
531536
matching.map(|(name, kind)| format!("there is a {kind} with a similar name: `{name}`"));
532537

538+
let mut footers = Vec::new();
539+
if emitted_source.is_none() {
540+
emitted_source = Some(UNKNOWN_LINTS.emitted_source(lint_level, reason));
541+
footers.push(Level::Note.title(emitted_source.as_ref().unwrap()));
542+
}
543+
533544
let mut message = if let Some(span) =
534545
get_span(manifest.document(), &["lints", "cargo", lint_name], false)
535546
{
@@ -564,28 +575,21 @@ fn output_unknown_lints(
564575
} else {
565576
Level::Note.title(&second_title)
566577
};
578+
footers.push(inherited_note);
567579

568-
level
569-
.title(&title)
570-
.snippet(
571-
Snippet::source(ws_contents)
572-
.origin(&ws_path)
573-
.annotation(Level::Error.span(lint_span))
574-
.fold(true),
575-
)
576-
.footer(inherited_note)
580+
level.title(&title).snippet(
581+
Snippet::source(ws_contents)
582+
.origin(&ws_path)
583+
.annotation(Level::Error.span(lint_span))
584+
.fold(true),
585+
)
577586
};
578587

579-
if emitted_source.is_none() {
580-
emitted_source = Some(format!(
581-
"`cargo::{}` is set to `{lint_level}` {reason}",
582-
UNKNOWN_LINTS.name
583-
));
584-
message = message.footer(Level::Note.title(emitted_source.as_ref().unwrap()));
585-
}
586-
587588
if let Some(help) = help.as_ref() {
588-
message = message.footer(Level::Help.title(help));
589+
footers.push(Level::Help.title(help));
590+
}
591+
for footer in footers {
592+
message = message.footer(footer);
589593
}
590594

591595
gctx.shell().print_message(message)?;

tests/testsuite/lints/unknown_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ workspace = true
7676
6 | this-lint-does-not-exist = "warn"
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^
7878
|
79+
= [NOTE] `cargo::unknown_lints` is set to `warn` by default
7980
[NOTE] `cargo::this-lint-does-not-exist` was inherited
8081
--> foo/Cargo.toml:9:1
8182
|
8283
9 | workspace = true
8384
| ----------------
8485
|
85-
= [NOTE] `cargo::unknown_lints` is set to `warn` by default
8686
[CHECKING] foo v0.0.1 ([ROOT]/foo/foo)
8787
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
8888

0 commit comments

Comments
 (0)