|
| 1 | +use cargo::util::command_prelude::{flag, ArgMatchesExt}; |
| 2 | +use cargo::util::lints::{Lint, LintLevel}; |
| 3 | +use itertools::Itertools; |
| 4 | +use std::fmt::Write; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +fn cli() -> clap::Command { |
| 8 | + clap::Command::new("xtask-lint-docs").arg(flag("check", "Check that the docs are up-to-date")) |
| 9 | +} |
| 10 | + |
| 11 | +fn main() -> anyhow::Result<()> { |
| 12 | + let args = cli().get_matches(); |
| 13 | + let check = args.flag("check"); |
| 14 | + |
| 15 | + let mut allow = Vec::new(); |
| 16 | + let mut warn = Vec::new(); |
| 17 | + let mut deny = Vec::new(); |
| 18 | + let mut forbid = Vec::new(); |
| 19 | + |
| 20 | + let mut lint_docs = String::new(); |
| 21 | + for lint in cargo::util::lints::LINTS |
| 22 | + .iter() |
| 23 | + .sorted_by_key(|lint| lint.name) |
| 24 | + { |
| 25 | + if lint.docs.is_some() { |
| 26 | + let sectipn = match lint.default_level { |
| 27 | + LintLevel::Allow => &mut allow, |
| 28 | + LintLevel::Warn => &mut warn, |
| 29 | + LintLevel::Deny => &mut deny, |
| 30 | + LintLevel::Forbid => &mut forbid, |
| 31 | + }; |
| 32 | + sectipn.push(lint.name); |
| 33 | + add_lint(lint, &mut lint_docs)?; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + let mut buf = String::new(); |
| 38 | + writeln!(buf, "# Lints\n")?; |
| 39 | + writeln!( |
| 40 | + buf, |
| 41 | + "Note: [Cargo's linting system is unstable](unstable.md#lintscargo) and can only be used on nightly toolchains" |
| 42 | + )?; |
| 43 | + writeln!(buf)?; |
| 44 | + |
| 45 | + if !allow.is_empty() { |
| 46 | + add_level_section(LintLevel::Allow, &allow, &mut buf)?; |
| 47 | + } |
| 48 | + if !warn.is_empty() { |
| 49 | + add_level_section(LintLevel::Warn, &warn, &mut buf)?; |
| 50 | + } |
| 51 | + if !deny.is_empty() { |
| 52 | + add_level_section(LintLevel::Deny, &deny, &mut buf)?; |
| 53 | + } |
| 54 | + if !forbid.is_empty() { |
| 55 | + add_level_section(LintLevel::Forbid, &forbid, &mut buf)?; |
| 56 | + } |
| 57 | + |
| 58 | + buf.push_str(&lint_docs); |
| 59 | + |
| 60 | + if check { |
| 61 | + let old = std::fs::read_to_string(lint_docs_path())?; |
| 62 | + if old != buf { |
| 63 | + anyhow::bail!( |
| 64 | + "The lints documentation is out-of-date. Run `cargo lint-docs` to update it." |
| 65 | + ); |
| 66 | + } |
| 67 | + } else { |
| 68 | + std::fs::write(lint_docs_path(), buf)?; |
| 69 | + } |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | + |
| 73 | +fn add_lint(lint: &Lint, buf: &mut String) -> std::fmt::Result { |
| 74 | + writeln!(buf, "## `{}`", lint.name)?; |
| 75 | + writeln!(buf, "Set to `{}` by default", lint.default_level)?; |
| 76 | + writeln!(buf, "{}\n", lint.docs.as_ref().unwrap()) |
| 77 | +} |
| 78 | + |
| 79 | +fn add_level_section(level: LintLevel, lint_names: &[&str], buf: &mut String) -> std::fmt::Result { |
| 80 | + let title = match level { |
| 81 | + LintLevel::Allow => "Allowed-by-default", |
| 82 | + LintLevel::Warn => "Warn-by-default", |
| 83 | + LintLevel::Deny => "Deny-by-default", |
| 84 | + LintLevel::Forbid => "Forbid-by-default", |
| 85 | + }; |
| 86 | + writeln!(buf, "## {title}\n")?; |
| 87 | + writeln!( |
| 88 | + buf, |
| 89 | + "These lints are all set to the '{}' level by default.", |
| 90 | + level |
| 91 | + )?; |
| 92 | + |
| 93 | + for name in lint_names { |
| 94 | + writeln!(buf, "- [`{}`](#{})", name, name)?; |
| 95 | + } |
| 96 | + writeln!(buf)?; |
| 97 | + Ok(()) |
| 98 | +} |
| 99 | + |
| 100 | +fn lint_docs_path() -> PathBuf { |
| 101 | + let pkg_root = env!("CARGO_MANIFEST_DIR"); |
| 102 | + let ws_root = PathBuf::from(format!("{pkg_root}/../..")); |
| 103 | + let path = { |
| 104 | + let path = ws_root.join("src/doc/src/reference/lints.md"); |
| 105 | + path.canonicalize().unwrap_or(path) |
| 106 | + }; |
| 107 | + path |
| 108 | +} |
0 commit comments