1+ //! JSON output and comparison functionality for Clippy warnings.
2+ //!
3+ //! This module handles serialization of Clippy warnings to JSON format,
4+ //! loading warnings from JSON files, and generating human-readable diffs
5+ //! between different linting runs.
6+
17use std:: fs;
28use std:: path:: Path ;
39
@@ -8,8 +14,10 @@ use crate::ClippyWarning;
814
915/// This is the total number. 300 warnings results in 100 messages per section.
1016const DEFAULT_LIMIT_PER_LINT : usize = 300 ;
17+ /// Target for total warnings to display across all lints when truncating output.
1118const TRUNCATION_TOTAL_TARGET : usize = 1000 ;
1219
20+ /// Representation of a single Clippy warning for JSON serialization.
1321#[ derive( Debug , Deserialize , Serialize ) ]
1422struct LintJson {
1523 /// The lint name e.g. `clippy::bytes_nth`
@@ -21,10 +29,12 @@ struct LintJson {
2129}
2230
2331impl LintJson {
32+ /// Returns a tuple of name and `file_line` for sorting and comparison.
2433 fn key ( & self ) -> impl Ord + ' _ {
2534 ( self . name . as_str ( ) , self . file_line . as_str ( ) )
2635 }
2736
37+ /// Formats the warning information with an action verb for display.
2838 fn info_text ( & self , action : & str ) -> String {
2939 format ! ( "{action} `{}` at [`{}`]({})" , self . name, self . file_line, self . file_url)
3040 }
@@ -53,12 +63,17 @@ pub(crate) fn output(clippy_warnings: Vec<ClippyWarning>) -> String {
5363 serde_json:: to_string ( & lints) . unwrap ( )
5464}
5565
66+ /// Loads lint warnings from a JSON file at the given path.
5667fn load_warnings ( path : & Path ) -> Vec < LintJson > {
5768 let file = fs:: read ( path) . unwrap_or_else ( |e| panic ! ( "failed to read {}: {e}" , path. display( ) ) ) ;
5869
5970 serde_json:: from_slice ( & file) . unwrap_or_else ( |e| panic ! ( "failed to deserialize {}: {e}" , path. display( ) ) )
6071}
6172
73+ /// Generates and prints a diff between two sets of lint warnings.
74+ ///
75+ /// Compares warnings from `old_path` and `new_path`, then displays a summary table
76+ /// and detailed information about added, removed, and changed warnings.
6277pub ( crate ) fn diff ( old_path : & Path , new_path : & Path , truncate : bool ) {
6378 let old_warnings = load_warnings ( old_path) ;
6479 let new_warnings = load_warnings ( new_path) ;
@@ -116,6 +131,7 @@ pub(crate) fn diff(old_path: &Path, new_path: &Path, truncate: bool) {
116131 }
117132}
118133
134+ /// Container for grouped lint warnings organized by status (added/removed/changed).
119135#[ derive( Debug ) ]
120136struct LintWarnings {
121137 name : String ,
@@ -124,6 +140,7 @@ struct LintWarnings {
124140 changed : Vec < ( LintJson , LintJson ) > ,
125141}
126142
143+ /// Prints a formatted report for a single lint type with its warnings.
127144fn print_lint_warnings ( lint : & LintWarnings , truncate_after : usize ) {
128145 let name = & lint. name ;
129146 let html_id = to_html_id ( name) ;
@@ -145,6 +162,7 @@ fn print_lint_warnings(lint: &LintWarnings, truncate_after: usize) {
145162 print_changed_diff ( & lint. changed , truncate_after / 3 ) ;
146163}
147164
165+ /// Prints a summary table of all lints with counts of added, removed, and changed warnings.
148166fn print_summary_table ( lints : & [ LintWarnings ] ) {
149167 println ! ( "| Lint | Added | Removed | Changed |" ) ;
150168 println ! ( "| ------------------------------------------ | ------: | ------: | ------: |" ) ;
@@ -160,6 +178,7 @@ fn print_summary_table(lints: &[LintWarnings]) {
160178 }
161179}
162180
181+ /// Prints a section of warnings with a header and formatted code blocks.
163182fn print_warnings ( title : & str , warnings : & [ LintJson ] , truncate_after : usize ) {
164183 if warnings. is_empty ( ) {
165184 return ;
@@ -180,6 +199,7 @@ fn print_warnings(title: &str, warnings: &[LintJson], truncate_after: usize) {
180199 }
181200}
182201
202+ /// Prints a section of changed warnings with unified diff format.
183203fn print_changed_diff ( changed : & [ ( LintJson , LintJson ) ] , truncate_after : usize ) {
184204 if changed. is_empty ( ) {
185205 return ;
@@ -213,6 +233,7 @@ fn print_changed_diff(changed: &[(LintJson, LintJson)], truncate_after: usize) {
213233 }
214234}
215235
236+ /// Truncates a list to a maximum number of items and prints a message about truncation.
216237fn truncate < T > ( list : & [ T ] , truncate_after : usize ) -> & [ T ] {
217238 if list. len ( ) > truncate_after {
218239 println ! (
@@ -227,6 +248,7 @@ fn truncate<T>(list: &[T], truncate_after: usize) -> &[T] {
227248 }
228249}
229250
251+ /// Prints a level 3 heading with an appropriate HTML ID for linking.
230252fn print_h3 ( lint : & str , title : & str ) {
231253 let html_id = to_html_id ( lint) ;
232254 // We have to use HTML here to be able to manually add an id.
0 commit comments