@@ -81,6 +81,9 @@ enum Cmd {
8181 /// Only report files under this project-relative prefix
8282 #[ arg( long) ]
8383 only : Option < String > ,
84+ /// Print at most N findings (highest confidence first); 0 = all. Totals and the exit code always cover every finding.
85+ #[ arg( long, default_value_t = 200 ) ]
86+ max_findings : usize ,
8487 /// Exit with status 1 when a finding of at least this level exists
8588 #[ arg( long, value_enum, default_value = "none" ) ]
8689 fail_on : FailOn ,
@@ -100,6 +103,9 @@ enum Cmd {
100103 no_check_declared : bool ,
101104 #[ arg( long) ]
102105 only : Option < String > ,
106+ /// Print at most N findings (highest confidence first); 0 = all. The exit code always covers every finding.
107+ #[ arg( long, default_value_t = 200 ) ]
108+ max_findings : usize ,
103109 #[ arg( long, value_enum, default_value = "none" ) ]
104110 fail_on : FailOn ,
105111 } ,
@@ -315,6 +321,51 @@ fn print_json<T: serde::Serialize>(v: &T) -> Result<()> {
315321 Ok ( ( ) )
316322}
317323
324+ /// Highest-confidence-first view of at most `max` findings (0 = all) plus how many were left out.
325+ fn capped ( findings : & [ Finding ] , max : usize ) -> ( Vec < & Finding > , usize ) {
326+ let mut v: Vec < & Finding > = findings. iter ( ) . collect ( ) ;
327+ if max == 0 || v. len ( ) <= max {
328+ return ( v, 0 ) ;
329+ }
330+ v. sort_by ( |a, b| {
331+ b. confidence
332+ . partial_cmp ( & a. confidence )
333+ . unwrap_or ( std:: cmp:: Ordering :: Equal )
334+ } ) ;
335+ let hidden = v. len ( ) - max;
336+ v. truncate ( max) ;
337+ ( v, hidden)
338+ }
339+
340+ fn note_hidden ( hidden : usize ) {
341+ if hidden > 0 {
342+ println ! ( "\n ... {hidden} more finding(s) not shown (lowest confidence last); use --max-findings 0 to print all, or --only <path> to narrow down" ) ;
343+ }
344+ }
345+
346+ /// JSON output with `findings` capped; `summary`/totals still describe everything and a
347+ /// `truncated` object says what was left out.
348+ fn print_json_capped < T : serde:: Serialize > ( r : & T , max : usize ) -> Result < ( ) > {
349+ let mut v = serde_json:: to_value ( r) ?;
350+ if let Some ( arr) = v. get_mut ( "findings" ) . and_then ( |f| f. as_array_mut ( ) ) {
351+ let total = arr. len ( ) ;
352+ if max > 0 && total > max {
353+ arr. sort_by ( |a, b| {
354+ let c = |x : & serde_json:: Value | x[ "confidence" ] . as_f64 ( ) . unwrap_or ( 0.0 ) ;
355+ c ( b) . partial_cmp ( & c ( a) ) . unwrap_or ( std:: cmp:: Ordering :: Equal )
356+ } ) ;
357+ arr. truncate ( max) ;
358+ v[ "truncated" ] = serde_json:: json!( {
359+ "shown" : max,
360+ "total" : total,
361+ "hint" : "output capped, lowest confidence findings omitted; pass --max-findings 0 for everything or --only <path> to narrow down"
362+ } ) ;
363+ }
364+ }
365+ println ! ( "{}" , serde_json:: to_string_pretty( & v) ?) ;
366+ Ok ( ( ) )
367+ }
368+
318369fn render ( f : & Finding ) {
319370 let loc = match f. column {
320371 Some ( c) => format ! ( "{}:{}:{}" , f. file, f. line, c) ,
@@ -385,6 +436,7 @@ fn run(cli: Cli) -> Result<bool> {
385436 no_test_only,
386437 only,
387438 fail_on,
439+ max_findings,
388440 scip,
389441 } => {
390442 let p = DeadCodeParams {
@@ -398,11 +450,13 @@ fn run(cli: Cli) -> Result<bool> {
398450 } ;
399451 let r = ops:: run_dead_code ( & target ( path) , & p) ?;
400452 if json {
401- print_json ( & r) ?;
453+ print_json_capped ( & r, max_findings ) ?;
402454 } else {
403- for f in & r. findings {
455+ let ( shown, hidden) = capped ( & r. findings , max_findings) ;
456+ for f in & shown {
404457 render ( f) ;
405458 }
459+ note_hidden ( hidden) ;
406460 println ! (
407461 "\n {} finding(s): {} high, {} medium, {} low (indexed {} files, {} symbols)" ,
408462 r. summary. total,
@@ -432,6 +486,7 @@ fn run(cli: Cli) -> Result<bool> {
432486 no_check_declared,
433487 only,
434488 fail_on,
489+ max_findings,
435490 } => {
436491 let p = DepsParams {
437492 offline,
@@ -441,11 +496,13 @@ fn run(cli: Cli) -> Result<bool> {
441496 } ;
442497 let r = ops:: run_check_deps ( & target ( path) , & p, None ) ?;
443498 if json {
444- print_json ( & r) ?;
499+ print_json_capped ( & r, max_findings ) ?;
445500 } else {
446- for f in & r. findings {
501+ let ( shown, hidden) = capped ( & r. findings , max_findings) ;
502+ for f in & shown {
447503 render ( f) ;
448504 }
505+ note_hidden ( hidden) ;
449506 let unknown = r
450507 . lookups
451508 . iter ( )
@@ -959,6 +1016,21 @@ nothing was executed. `plumb enrich` runs SCIP indexers; `plumb verify --run` ru
9591016}
9601017
9611018pub fn main_entry ( ) -> ExitCode {
1019+ // Rust ignores SIGPIPE, so `plumb ... | head` makes println! panic with a backtrace hint.
1020+ // A closed stdout is the reader's choice, not an error: exit quietly like other Unix tools.
1021+ let default_hook = std:: panic:: take_hook ( ) ;
1022+ std:: panic:: set_hook ( Box :: new ( move |info| {
1023+ let msg = info
1024+ . payload ( )
1025+ . downcast_ref :: < String > ( )
1026+ . map ( |s| s. as_str ( ) )
1027+ . or_else ( || info. payload ( ) . downcast_ref :: < & str > ( ) . copied ( ) )
1028+ . unwrap_or ( "" ) ;
1029+ if msg. contains ( "failed printing to std" ) && msg. contains ( "Broken pipe" ) {
1030+ std:: process:: exit ( 141 ) ;
1031+ }
1032+ default_hook ( info) ;
1033+ } ) ) ;
9621034 let cli = Cli :: parse ( ) ;
9631035 match run ( cli) {
9641036 Ok ( false ) => ExitCode :: SUCCESS ,
0 commit comments