Skip to content

Commit eec0473

Browse files
committed
refactor: Make get_span return the key and value span
1 parent 7c4e67c commit eec0473

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/cargo/util/lints.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,36 +134,34 @@ fn verify_feature_enabled(
134134
);
135135

136136
let message = if let Some(span) =
137-
get_span(manifest.document(), &["lints", "cargo", lint_name], false)
137+
get_key_value_span(manifest.document(), &["lints", "cargo", lint_name])
138138
{
139139
Level::Error
140140
.title(&title)
141141
.snippet(
142142
Snippet::source(manifest.contents())
143143
.origin(&manifest_path)
144-
.annotation(Level::Error.span(span).label(&label))
144+
.annotation(Level::Error.span(span.key).label(&label))
145145
.fold(true),
146146
)
147147
.footer(Level::Help.title(&help))
148148
} else {
149-
let lint_span = get_span(
149+
let lint_span = get_key_value_span(
150150
ws_document,
151151
&["workspace", "lints", "cargo", lint_name],
152-
false,
153152
)
154153
.unwrap_or_else(|| {
155154
panic!("could not find `cargo::{lint_name}` in `[lints]`, or `[workspace.lints]` ")
156155
});
157156

158-
let inherited_note = if let (Some(inherit_span_key), Some(inherit_span_value)) = (
159-
get_span(manifest.document(), &["lints", "workspace"], false),
160-
get_span(manifest.document(), &["lints", "workspace"], true),
161-
) {
157+
let inherited_note = if let Some(inherit_span) =
158+
get_key_value_span(manifest.document(), &["lints", "workspace"])
159+
{
162160
Level::Note.title(&second_title).snippet(
163161
Snippet::source(manifest.contents())
164162
.origin(&manifest_path)
165163
.annotation(
166-
Level::Note.span(inherit_span_key.start..inherit_span_value.end),
164+
Level::Note.span(inherit_span.key.start..inherit_span.value.end),
167165
)
168166
.fold(true),
169167
)
@@ -176,7 +174,7 @@ fn verify_feature_enabled(
176174
.snippet(
177175
Snippet::source(ws_contents)
178176
.origin(&ws_path)
179-
.annotation(Level::Error.span(lint_span).label(&label))
177+
.annotation(Level::Error.span(lint_span.key).label(&label))
180178
.fold(true),
181179
)
182180
.footer(inherited_note)
@@ -189,22 +187,26 @@ fn verify_feature_enabled(
189187
Ok(())
190188
}
191189

192-
pub fn get_span(
190+
#[derive(Clone)]
191+
pub struct TomlSpan {
192+
pub key: Range<usize>,
193+
pub value: Range<usize>,
194+
}
195+
196+
pub fn get_key_value_span(
193197
document: &toml::Spanned<toml::de::DeTable<'static>>,
194198
path: &[&str],
195-
get_value: bool,
196-
) -> Option<Range<usize>> {
199+
) -> Option<TomlSpan> {
197200
let mut table = document.get_ref();
198201
let mut iter = path.into_iter().peekable();
199202
while let Some(key) = iter.next() {
200203
let key_s: &str = key.as_ref();
201204
let (key, item) = table.get_key_value(key_s)?;
202205
if iter.peek().is_none() {
203-
return if get_value {
204-
Some(item.span())
205-
} else {
206-
Some(key.span())
207-
};
206+
return Some(TomlSpan {
207+
key: key.span(),
208+
value: item.span(),
209+
});
208210
}
209211
if let Some(next_table) = item.get_ref().as_table() {
210212
table = next_table;
@@ -213,7 +215,10 @@ pub fn get_span(
213215
if let Some(array) = item.get_ref().as_array() {
214216
let next = iter.next().unwrap();
215217
return array.iter().find_map(|item| match item.get_ref() {
216-
toml::de::DeValue::String(s) if s == next => Some(item.span()),
218+
toml::de::DeValue::String(s) if s == next => Some(TomlSpan {
219+
key: key.span(),
220+
value: item.span(),
221+
}),
217222
_ => None,
218223
});
219224
}
@@ -455,14 +460,14 @@ pub fn check_im_a_teapot(
455460
let manifest_path = rel_cwd_manifest_path(path, gctx);
456461
let emitted_reason = IM_A_TEAPOT.emitted_source(lint_level, reason);
457462

458-
let key_span = get_span(manifest.document(), &["package", "im-a-teapot"], false).unwrap();
459-
let value_span = get_span(manifest.document(), &["package", "im-a-teapot"], true).unwrap();
463+
let span = get_key_value_span(manifest.document(), &["package", "im-a-teapot"]).unwrap();
464+
460465
let message = level
461466
.title(IM_A_TEAPOT.desc)
462467
.snippet(
463468
Snippet::source(manifest.contents())
464469
.origin(&manifest_path)
465-
.annotation(level.span(key_span.start..value_span.end))
470+
.annotation(level.span(span.key.start..span.value.end))
466471
.fold(true),
467472
)
468473
.footer(Level::Note.title(&emitted_reason));
@@ -542,33 +547,31 @@ fn output_unknown_lints(
542547
}
543548

544549
let mut message = if let Some(span) =
545-
get_span(manifest.document(), &["lints", "cargo", lint_name], false)
550+
get_key_value_span(manifest.document(), &["lints", "cargo", lint_name])
546551
{
547552
level.title(&title).snippet(
548553
Snippet::source(manifest.contents())
549554
.origin(&manifest_path)
550-
.annotation(Level::Error.span(span))
555+
.annotation(Level::Error.span(span.key))
551556
.fold(true),
552557
)
553558
} else {
554-
let lint_span = get_span(
559+
let lint_span = get_key_value_span(
555560
ws_document,
556561
&["workspace", "lints", "cargo", lint_name],
557-
false,
558562
)
559563
.unwrap_or_else(|| {
560564
panic!("could not find `cargo::{lint_name}` in `[lints]`, or `[workspace.lints]` ")
561565
});
562566

563-
let inherited_note = if let (Some(inherit_span_key), Some(inherit_span_value)) = (
564-
get_span(manifest.document(), &["lints", "workspace"], false),
565-
get_span(manifest.document(), &["lints", "workspace"], true),
566-
) {
567+
let inherited_note = if let Some(inherit_span) =
568+
get_key_value_span(manifest.document(), &["lints", "workspace"])
569+
{
567570
Level::Note.title(&second_title).snippet(
568571
Snippet::source(manifest.contents())
569572
.origin(&manifest_path)
570573
.annotation(
571-
Level::Note.span(inherit_span_key.start..inherit_span_value.end),
574+
Level::Note.span(inherit_span.key.start..inherit_span.value.end),
572575
)
573576
.fold(true),
574577
)
@@ -580,7 +583,7 @@ fn output_unknown_lints(
580583
level.title(&title).snippet(
581584
Snippet::source(ws_contents)
582585
.origin(&ws_path)
583-
.annotation(Level::Error.span(lint_span))
586+
.annotation(Level::Error.span(lint_span.key))
584587
.fold(true),
585588
)
586589
};

src/cargo/util/toml/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, Worksp
3232
use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY};
3333
use crate::util::errors::{CargoResult, ManifestError};
3434
use crate::util::interning::InternedString;
35-
use crate::util::lints::{get_span, rel_cwd_manifest_path};
35+
use crate::util::lints::{get_key_value_span, rel_cwd_manifest_path};
3636
use crate::util::{self, GlobalContext, IntoUrl, OptVersionReq, context::ConfigRelativePath};
3737

3838
mod embedded;
@@ -1867,8 +1867,8 @@ fn missing_dep_diagnostic(
18671867
) -> CargoResult<()> {
18681868
let dep_name = missing_dep.dep_name;
18691869
let manifest_path = rel_cwd_manifest_path(manifest_file, gctx);
1870-
let feature_value_span =
1871-
get_span(&document, &["features", missing_dep.feature.as_str()], true).unwrap();
1870+
let feature_span =
1871+
get_key_value_span(&document, &["features", missing_dep.feature.as_str()]).unwrap();
18721872

18731873
let title = format!(
18741874
"feature `{}` includes `{}`, but `{}` is not a dependency",
@@ -1883,7 +1883,7 @@ fn missing_dep_diagnostic(
18831883
let snippet = Snippet::source(&contents)
18841884
.origin(&manifest_path)
18851885
.fold(true)
1886-
.annotation(Level::Error.span(feature_value_span.start..feature_value_span.end));
1886+
.annotation(Level::Error.span(feature_span.value));
18871887
let message = if missing_dep.weak_optional {
18881888
let mut orig_deps = vec![
18891889
(
@@ -1918,10 +1918,10 @@ fn missing_dep_diagnostic(
19181918
.map(|s| *s)
19191919
.chain(std::iter::once(dep_name.as_str()))
19201920
.collect::<Vec<_>>();
1921-
let dep_span = get_span(&document, &toml_path, false).unwrap();
1921+
let dep_span = get_key_value_span(&document, &toml_path).unwrap();
19221922

19231923
message
1924-
.snippet(snippet.annotation(Level::Warning.span(dep_span).label(&info_label)))
1924+
.snippet(snippet.annotation(Level::Warning.span(dep_span.key).label(&info_label)))
19251925
.footer(Level::Help.title(&help))
19261926
} else {
19271927
message.snippet(snippet)

0 commit comments

Comments
 (0)