Skip to content

Commit ab2508a

Browse files
committed
Collect all matching messages for a lint.
Some examples may contain multiple lines which trigger the lint. Previously it would only display the first message. This updates it so that all matching instances of the lint are displayed.
1 parent 15450b1 commit ab2508a

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/tools/lint-docs/src/lib.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -422,37 +422,36 @@ impl<'a> LintExtractor<'a> {
422422
.filter(|line| line.starts_with('{'))
423423
.map(serde_json::from_str)
424424
.collect::<Result<Vec<serde_json::Value>, _>>()?;
425-
match msgs
425+
// First try to find the messages with the `code` field set to our lint.
426+
let matches: Vec<_> = msgs
426427
.iter()
427-
.find(|msg| matches!(&msg["code"]["code"], serde_json::Value::String(s) if s==name))
428-
{
429-
Some(msg) => {
430-
let rendered = msg["rendered"].as_str().expect("rendered field should exist");
431-
Ok(rendered.to_string())
432-
}
433-
None => {
434-
match msgs.iter().find(
435-
|msg| matches!(&msg["rendered"], serde_json::Value::String(s) if s.contains(name)),
436-
) {
437-
Some(msg) => {
438-
let rendered = msg["rendered"].as_str().expect("rendered field should exist");
439-
Ok(rendered.to_string())
440-
}
441-
None => {
442-
let rendered: Vec<&str> =
443-
msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
444-
let non_json: Vec<&str> =
445-
stderr.lines().filter(|line| !line.starts_with('{')).collect();
446-
Err(format!(
447-
"did not find lint `{}` in output of example, got:\n{}\n{}",
448-
name,
449-
non_json.join("\n"),
450-
rendered.join("\n")
451-
)
452-
.into())
453-
}
454-
}
428+
.filter(|msg| matches!(&msg["code"]["code"], serde_json::Value::String(s) if s==name))
429+
.map(|msg| msg["rendered"].as_str().expect("rendered field should exist").to_string())
430+
.collect();
431+
if matches.is_empty() {
432+
// Some lints override their code to something else (E0566).
433+
// Try to find something that looks like it could be our lint.
434+
let matches: Vec<_> = msgs.iter().filter(|msg|
435+
matches!(&msg["rendered"], serde_json::Value::String(s) if s.contains(name)))
436+
.map(|msg| msg["rendered"].as_str().expect("rendered field should exist").to_string())
437+
.collect();
438+
if matches.is_empty() {
439+
let rendered: Vec<&str> =
440+
msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
441+
let non_json: Vec<&str> =
442+
stderr.lines().filter(|line| !line.starts_with('{')).collect();
443+
Err(format!(
444+
"did not find lint `{}` in output of example, got:\n{}\n{}",
445+
name,
446+
non_json.join("\n"),
447+
rendered.join("\n")
448+
)
449+
.into())
450+
} else {
451+
Ok(matches.join("\n"))
455452
}
453+
} else {
454+
Ok(matches.join("\n"))
456455
}
457456
}
458457

0 commit comments

Comments
 (0)