Skip to content

Commit 96aec67

Browse files
committed
refactor: Cleanup lint message creation
1 parent 2ef151b commit 96aec67

File tree

1 file changed

+48
-73
lines changed

1 file changed

+48
-73
lines changed

src/cargo/util/lints.rs

Lines changed: 48 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -133,53 +133,41 @@ fn verify_feature_enabled(
133133
dash_feature_name
134134
);
135135

136-
let message = if let Some(span) =
136+
let (contents, path, span) = if let Some(span) =
137137
get_key_value_span(manifest.document(), &["lints", "cargo", lint_name])
138138
{
139-
Level::Error
140-
.title(&title)
141-
.snippet(
142-
Snippet::source(manifest.contents())
143-
.origin(&manifest_path)
144-
.annotation(Level::Error.span(span.key).label(&label))
145-
.fold(true),
146-
)
147-
.footer(Level::Help.title(&help))
139+
(manifest.contents(), manifest_path, span)
140+
} else if let Some(lint_span) =
141+
get_key_value_span(ws_document, &["workspace", "lints", "cargo", lint_name])
142+
{
143+
(ws_contents, ws_path, lint_span)
148144
} else {
149-
let lint_span = get_key_value_span(
150-
ws_document,
151-
&["workspace", "lints", "cargo", lint_name],
145+
panic!("could not find `cargo::{lint_name}` in `[lints]`, or `[workspace.lints]` ")
146+
};
147+
148+
let mut message = Level::Error
149+
.title(&title)
150+
.snippet(
151+
Snippet::source(contents)
152+
.origin(path)
153+
.annotation(Level::Error.span(span.key).label(&label))
154+
.fold(true),
152155
)
153-
.unwrap_or_else(|| {
154-
panic!("could not find `cargo::{lint_name}` in `[lints]`, or `[workspace.lints]` ")
155-
});
156+
.footer(Level::Help.title(&help));
156157

157-
let inherited_note = if let Some(inherit_span) =
158-
get_key_value_span(manifest.document(), &["lints", "workspace"])
159-
{
158+
if let Some(inherit_span) = get_key_value_span(manifest.document(), &["lints", "workspace"])
159+
{
160+
message = message.footer(
160161
Level::Note.title(&second_title).snippet(
161162
Snippet::source(manifest.contents())
162163
.origin(&manifest_path)
163164
.annotation(
164165
Level::Note.span(inherit_span.key.start..inherit_span.value.end),
165166
)
166167
.fold(true),
167-
)
168-
} else {
169-
Level::Note.title(&second_title)
170-
};
171-
172-
Level::Error
173-
.title(&title)
174-
.snippet(
175-
Snippet::source(ws_contents)
176-
.origin(&ws_path)
177-
.annotation(Level::Error.span(lint_span.key).label(&label))
178-
.fold(true),
179-
)
180-
.footer(Level::Help.title(&help))
181-
.footer(inherited_note)
182-
};
168+
),
169+
);
170+
}
183171

184172
*error_count += 1;
185173
gctx.shell().print_message(message)?;
@@ -540,59 +528,46 @@ fn output_unknown_lints(
540528
let help =
541529
matching.map(|(name, kind)| format!("there is a {kind} with a similar name: `{name}`"));
542530

543-
let mut footers = Vec::new();
531+
let (contents, path, span) = if let Some(span) =
532+
get_key_value_span(manifest.document(), &["lints", "cargo", lint_name])
533+
{
534+
(manifest.contents(), manifest_path, span)
535+
} else if let Some(lint_span) =
536+
get_key_value_span(ws_document, &["workspace", "lints", "cargo", lint_name])
537+
{
538+
(ws_contents, ws_path, lint_span)
539+
} else {
540+
panic!("could not find `cargo::{lint_name}` in `[lints]`, or `[workspace.lints]` ")
541+
};
542+
543+
let mut message = level.title(&title).snippet(
544+
Snippet::source(contents)
545+
.origin(path)
546+
.annotation(Level::Error.span(span.key))
547+
.fold(true),
548+
);
549+
544550
if emitted_source.is_none() {
545551
emitted_source = Some(UNKNOWN_LINTS.emitted_source(lint_level, reason));
546-
footers.push(Level::Note.title(emitted_source.as_ref().unwrap()));
552+
message = message.footer(Level::Note.title(emitted_source.as_ref().unwrap()));
547553
}
554+
548555
if let Some(help) = help.as_ref() {
549-
footers.push(Level::Help.title(help));
556+
message = message.footer(Level::Help.title(help));
550557
}
551558

552-
let mut message = if let Some(span) =
553-
get_key_value_span(manifest.document(), &["lints", "cargo", lint_name])
559+
if let Some(inherit_span) = get_key_value_span(manifest.document(), &["lints", "workspace"])
554560
{
555-
level.title(&title).snippet(
556-
Snippet::source(manifest.contents())
557-
.origin(&manifest_path)
558-
.annotation(Level::Error.span(span.key))
559-
.fold(true),
560-
)
561-
} else {
562-
let lint_span = get_key_value_span(
563-
ws_document,
564-
&["workspace", "lints", "cargo", lint_name],
565-
)
566-
.unwrap_or_else(|| {
567-
panic!("could not find `cargo::{lint_name}` in `[lints]`, or `[workspace.lints]` ")
568-
});
569-
570-
let inherited_note = if let Some(inherit_span) =
571-
get_key_value_span(manifest.document(), &["lints", "workspace"])
572-
{
561+
message = message.footer(
573562
Level::Note.title(&second_title).snippet(
574563
Snippet::source(manifest.contents())
575564
.origin(&manifest_path)
576565
.annotation(
577566
Level::Note.span(inherit_span.key.start..inherit_span.value.end),
578567
)
579568
.fold(true),
580-
)
581-
} else {
582-
Level::Note.title(&second_title)
583-
};
584-
footers.push(inherited_note);
585-
586-
level.title(&title).snippet(
587-
Snippet::source(ws_contents)
588-
.origin(&ws_path)
589-
.annotation(Level::Error.span(lint_span.key))
590-
.fold(true),
569+
),
591570
)
592-
};
593-
594-
for footer in footers {
595-
message = message.footer(footer);
596571
}
597572

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

0 commit comments

Comments
 (0)