Source src/render/categories.rs
1//! Category counts and pages reuse the report's existing page shell and tables.2use super::*; warning: clippy::wildcard_imports34pub(super) struct CategoryRow {5 pub name: String,6 pub href: String,7 pub count: usize,8 pub kinds: usize,9}10#[derive(Template)]11#[template(path = "category.html")]12struct CategoryView {13 count: usize,14 lints: Vec<CountLink>,15}1617fn groups(report: &Report) -> BTreeMap<&str, Vec<&Finding>> {18 let mut groups = BTreeMap::<_, Vec<_>>::new();19 for finding in &report.findings {20 if let Some(category) = super::super::paths::category_key(finding) {21 groups.entry(category).or_default().push(finding);22 }23 }24 groups25}26pub(super) fn rows(report: &Report, pages: &PageMap, from: &Path) -> Vec<CategoryRow> {27 groups(report)28 .into_iter()29 .map(|(key, findings)| CategoryRow {30 name: if key == "unknown" {31 "Unknown".into()32 } else {33 key.into()34 },35 href: relative_href(from, &pages.category_pages[key], None),36 count: findings.len(),37 kinds: findings38 .iter()39 .map(|f| group_key(f))40 .collect::<std::collections::BTreeSet<_>>()41 .len(),42 })43 .collect()44}45pub(super) fn write(report: &Report, pages: &PageMap, destination: &Path) -> Result<(), String> {46 for (key, findings) in groups(report) {47 let path = &pages.category_pages[key];48 let mut kinds = BTreeMap::<_, Vec<_>>::new();49 for finding in &findings {50 kinds.entry(group_key(finding)).or_default().push(*finding);51 }52 let lints: Vec<_> = kinds53 .iter()54 .map(|(key, fs)| CountLink {55 link: Link {56 href: relative_href(path, &pages.lint_pages[key], None),57 text: fs[0]58 .code59 .clone()60 .unwrap_or_else(|| "General Clippy diagnostic".into()),61 },62 count: fs.len(),63 })64 .collect();65 let sidebar = lints66 .iter()67 .map(|row| Link {68 href: row.link.href.clone(),69 text: row.link.text.clone(),70 })71 .collect();72 write_page(73 destination,74 path,75 Page {76 title: if key == "unknown" { "Unknown" } else { key },77 section: "Diagnostic kinds",78 root: relative_href(path, Path::new(""), None),79 sidebar,80 content: render(&CategoryView {81 count: findings.len(),82 lints,83 })?,84 crate_context: crate_context(report, &findings),85 crates: crate_infos(report, &findings),86 heading_kind: "Category",87 source_page: false,88 standalone_page: false,89 generator_version: env!("CARGO_PKG_VERSION"),90 source_tree: String::new(),91 },92 )?;93 }94 Ok(())95}9697#[cfg(test)]98mod tests {99 use super::*;100 #[test]101 fn unknown_and_non_clippy_findings_have_distinct_reachable_groups() {102 let cargo_metadata::Message::CompilerMessage(message) =103 serde_json::from_value(crate::diagnostic::sample()).unwrap()104 else {105 panic!()106 };107 let mut known = crate::diagnostic::normalize::normalize(&message, Path::new("/demo"));108 let mut other_target = known.clone();109 other_target.targets[0].name = "another-target".into();110 let mut unknown = known.clone();111 unknown.code = Some("clippy::new_future_lint".into());112 let mut compiler = known.clone();113 compiler.origin = "Compiler".into();114 compiler.code = Some("E0308".into());115 let mut general = compiler.clone();116 general.origin = "General".into();117 general.code = None;118 let mut findings = crate::diagnostic::dedup::merge_findings(vec![119 known.clone(),120 other_target,121 unknown,122 compiler,123 general,124 ]);125 for finding in &mut findings {126 if finding.code.as_deref() == known.code.as_deref() {127 finding.category = Some("complexity".into());128 }129 }130 known = findings131 .iter()132 .find(|f| f.code == known.code)133 .unwrap()134 .clone();135 assert_eq!(known.targets.len(), 2);136 let report = Report {137 category_metadata: None,138 project: ProjectInfo {139 workspace_root: "/demo".into(),140 target_dir: "/demo/target".into(),141 build_dir: None,142 packages: vec![],143 },144 execution: Execution {145 code: Some(101),146 complete: true,147 requested_args: vec![],148 observed_packages: vec![],149 observed_targets: vec![],150 stderr: String::new(),151 stderr_truncated: false,152 text_stdout: String::new(),153 stdout_truncated: false,154 issues: vec![],155 },156 raw_count: 5,157 findings,158 };159 let sources = BTreeMap::from([(160 known.spans[0].path.clone(),161 SourceFile {162 display_path: "src/lib.rs".into(),163 text: Some("abc\n".into()),164 reason: None,165 },166 )]);167 let pages = super::super::super::paths::build_page_map(&report, &sources);168 assert_eq!(pages.category_pages.len(), 2);169 let rows = rows(&report, &pages, Path::new("index.html"));170 assert_eq!(rows.iter().map(|r| r.count).sum::<usize>(), 2);171 assert!(rows.iter().all(|r| r.count == 1 && r.kinds == 1));172 let output = tempfile::tempdir().unwrap();173 render_report(&report, &sources, &pages, output.path()).unwrap();174 validate_report(output.path()).unwrap();175 let unknown = std::fs::read_to_string(output.path().join("category/unknown.html")).unwrap();176 assert!(unknown.contains("new_future_lint"));177 assert!(!unknown.contains("E0308"));178 assert!(!unknown.contains("General diagnostics"));179 }180}181Findings in this file§
warning clippy::wildcard_imports §
warning clippy::wildcard_imports §
Targets (1)
cargo-clippy-html (bin)
Primary: src/render/categories.rs:2:5
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#wildcard_imports
help: try
Suggestion: try
--- "a/src/render/categories.rs"
+++ "b/src/render/categories.rs"
@@ -1,5 +1,5 @@
//! Category counts and pages reuse the report's existing page shell and tables.
-use super::*;
+use super::{Template, CountLink, Report, BTreeMap, Finding, PageMap, Path, relative_href, group_key, Link, write_page, Page, render, crate_context, crate_infos};
pub(super) struct CategoryRow {
pub name: String,
Rendered diagnostic (text)
warning: usage of wildcard import
--> src/render/categories.rs:2:5
|
2 | use super::*;
| ^^^^^^^^ help: try: `super::{Template, CountLink, Report, BTreeMap, Finding, PageMap, Path, relative_href, group_key, Link, write_page, Page, render, crate_context, crate_infos}`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#wildcard_imports