Skip to content

Commit 3d9dcf4

Browse files
Un-spaghettify the control flow of generate_lint_output
Co-authored-by: Eric Huss <[email protected]>
1 parent 9cbd177 commit 3d9dcf4

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

Diff for: src/tools/lint-docs/src/lib.rs

+49-40
Original file line numberDiff line numberDiff line change
@@ -473,55 +473,64 @@ impl<'a> LintExtractor<'a> {
473473
.filter(|line| line.starts_with('{'))
474474
.map(serde_json::from_str)
475475
.collect::<Result<Vec<serde_json::Value>, _>>()?;
476+
476477
// First try to find the messages with the `code` field set to our lint.
477478
let matches: Vec<_> = msgs
478479
.iter()
479480
.filter(|msg| matches!(&msg["code"]["code"], serde_json::Value::String(s) if s==name))
480481
.map(|msg| msg["rendered"].as_str().expect("rendered field should exist").to_string())
481482
.collect();
482-
if matches.is_empty() {
483-
// Try to detect if an unstable lint forgot to enable a `#![feature(..)]`.
484-
if name != "test_unstable_lint" && msgs.iter().any(|msg| {
483+
if !matches.is_empty() {
484+
return Ok(matches.join("\n"));
485+
}
486+
487+
// Try to detect if an unstable lint forgot to enable a `#![feature(..)]`.
488+
// Specifically exclude `test_unstable_lint` which exercises this on purpose.
489+
if name != "test_unstable_lint"
490+
&& msgs.iter().any(|msg| {
485491
matches!(&msg["code"]["code"], serde_json::Value::String(s) if s=="unknown_lints")
486492
&& matches!(&msg["message"], serde_json::Value::String(s) if s.contains(name))
487-
}) {
488-
let rendered: Vec<&str> =
489-
msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
490-
let non_json: Vec<&str> =
491-
stderr.lines().filter(|line| !line.starts_with('{')).collect();
492-
Err(format!(
493-
"lint `{}` is unstable and must be enabled in example. see:\n{}\n{}",
494-
name,
495-
rendered.join("\n"),
496-
non_json.join("\n"),
497-
)
498-
.into())
499-
} else {
500-
// Some lints override their code to something else (E0566).
501-
// Try to find something that looks like it could be our lint.
502-
let matches: Vec<_> = msgs.iter().filter(|msg|
503-
matches!(&msg["rendered"], serde_json::Value::String(s) if s.contains(name)))
504-
.map(|msg| msg["rendered"].as_str().expect("rendered field should exist").to_string())
505-
.collect();
506-
if matches.is_empty() {
507-
let rendered: Vec<&str> =
508-
msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
509-
let non_json: Vec<&str> =
510-
stderr.lines().filter(|line| !line.starts_with('{')).collect();
511-
Err(format!(
512-
"did not find lint `{}` in output of example, got:\n{}\n{}",
513-
name,
514-
non_json.join("\n"),
515-
rendered.join("\n")
516-
)
517-
.into())
518-
} else {
519-
Ok(matches.join("\n"))
520-
}
521-
}
522-
} else {
523-
Ok(matches.join("\n"))
493+
})
494+
{
495+
let rendered: Vec<&str> =
496+
msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
497+
let non_json: Vec<&str> =
498+
stderr.lines().filter(|line| !line.starts_with('{')).collect();
499+
return Err(format!(
500+
"did not find lint `{}` in output of example (got unknown_lints)\n\
501+
Is the lint possibly misspelled, or does it need a `#![feature(...)]`?\n\
502+
Output was:\n\
503+
{}\n{}",
504+
name,
505+
rendered.join("\n"),
506+
non_json.join("\n"),
507+
)
508+
.into());
524509
}
510+
511+
// Some lints override their code to something else (E0566).
512+
// Try to find something that looks like it could be our lint.
513+
let matches: Vec<_> = msgs
514+
.iter()
515+
.filter(
516+
|msg| matches!(&msg["rendered"], serde_json::Value::String(s) if s.contains(name)),
517+
)
518+
.map(|msg| msg["rendered"].as_str().expect("rendered field should exist").to_string())
519+
.collect();
520+
if !matches.is_empty() {
521+
return Ok(matches.join("\n"));
522+
}
523+
524+
// Otherwise, give a descriptive error.
525+
let rendered: Vec<&str> = msgs.iter().filter_map(|msg| msg["rendered"].as_str()).collect();
526+
let non_json: Vec<&str> = stderr.lines().filter(|line| !line.starts_with('{')).collect();
527+
Err(format!(
528+
"did not find lint `{}` in output of example, got:\n{}\n{}",
529+
name,
530+
non_json.join("\n"),
531+
rendered.join("\n")
532+
)
533+
.into())
525534
}
526535

527536
/// Saves the mdbook lint chapters at the given path.

0 commit comments

Comments
 (0)