File tree 3 files changed +87
-2
lines changed 3 files changed +87
-2
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,9 @@ pub struct Config {
59
59
pub run_only_ignored : bool ,
60
60
/// Filters must match exactly instead of just checking for substrings.
61
61
pub filter_exact : bool ,
62
+ /// Prefix added to all diagnostic code matchers. Note this will make it impossible
63
+ /// match codes which do not contain this prefix.
64
+ pub diagnostic_code_prefix : String ,
62
65
}
63
66
64
67
impl Config {
@@ -103,6 +106,7 @@ impl Config {
103
106
list : false ,
104
107
run_only_ignored : false ,
105
108
filter_exact : false ,
109
+ diagnostic_code_prefix : String :: new ( ) ,
106
110
}
107
111
}
108
112
Original file line number Diff line number Diff line change @@ -1125,8 +1125,13 @@ fn check_annotations(
1125
1125
ErrorMatchKind :: Code ( code) => {
1126
1126
let found = msgs. iter ( ) . position ( |msg| {
1127
1127
msg. level == Level :: Error
1128
- && msg. code . as_ref ( ) . is_some_and ( |msg| * msg == * * code)
1128
+ && msg
1129
+ . code
1130
+ . as_ref ( )
1131
+ . and_then ( |code| code. strip_prefix ( & config. diagnostic_code_prefix ) )
1132
+ . is_some_and ( |msg| * msg == * * code)
1129
1133
} ) ;
1134
+
1130
1135
if let Some ( found) = found {
1131
1136
msgs. remove ( found) ;
1132
1137
continue ;
@@ -1141,7 +1146,10 @@ fn check_annotations(
1141
1146
expected_line : Some ( line) ,
1142
1147
} ,
1143
1148
ErrorMatchKind :: Code ( code) => Error :: CodeNotFound {
1144
- code : code. clone ( ) ,
1149
+ code : Spanned :: new (
1150
+ format ! ( "{}{}" , config. diagnostic_code_prefix, * * code) ,
1151
+ code. span ( ) ,
1152
+ ) ,
1145
1153
expected_line : Some ( line) ,
1146
1154
} ,
1147
1155
} ) ;
Original file line number Diff line number Diff line change @@ -462,3 +462,76 @@ fn main() {
462
462
}
463
463
}
464
464
}
465
+
466
+ #[ test]
467
+ fn find_code_with_prefix ( ) {
468
+ let s = r"
469
+ fn main() {
470
+ let _x: i32 = 0u32; //~ E0308
471
+ }
472
+ " ;
473
+ let comments = Comments :: parse ( s) . unwrap ( ) ;
474
+ let config = Config {
475
+ diagnostic_code_prefix : "prefix::" . into ( ) ,
476
+ ..config ( )
477
+ } ;
478
+ {
479
+ let messages = vec ! [
480
+ vec![ ] ,
481
+ vec![ ] ,
482
+ vec![ ] ,
483
+ vec![ Message {
484
+ message: "mismatched types" . to_string( ) ,
485
+ level: Level :: Error ,
486
+ line_col: None ,
487
+ code: Some ( "prefix::E0308" . into( ) ) ,
488
+ } ] ,
489
+ ] ;
490
+ let mut errors = vec ! [ ] ;
491
+ check_annotations (
492
+ messages,
493
+ vec ! [ ] ,
494
+ Path :: new ( "moobar" ) ,
495
+ & mut errors,
496
+ & config,
497
+ "" ,
498
+ & comments,
499
+ )
500
+ . unwrap ( ) ;
501
+ match & errors[ ..] {
502
+ [ ] => { }
503
+ _ => panic ! ( "{:#?}" , errors) ,
504
+ }
505
+ }
506
+
507
+ // missing prefix
508
+ {
509
+ let messages = vec ! [
510
+ vec![ ] ,
511
+ vec![ ] ,
512
+ vec![ ] ,
513
+ vec![ Message {
514
+ message: "mismatched types" . to_string( ) ,
515
+ level: Level :: Error ,
516
+ line_col: None ,
517
+ code: Some ( "E0308" . into( ) ) ,
518
+ } ] ,
519
+ ] ;
520
+ let mut errors = vec ! [ ] ;
521
+ check_annotations (
522
+ messages,
523
+ vec ! [ ] ,
524
+ Path :: new ( "moobar" ) ,
525
+ & mut errors,
526
+ & config,
527
+ "" ,
528
+ & comments,
529
+ )
530
+ . unwrap ( ) ;
531
+ match & errors[ ..] {
532
+ [ Error :: CodeNotFound { code, .. } , Error :: ErrorsWithoutPattern { msgs, .. } ]
533
+ if * * code == "prefix::E0308" && code. line ( ) . get ( ) == 3 && msgs. len ( ) == 1 => { }
534
+ _ => panic ! ( "{:#?}" , errors) ,
535
+ }
536
+ }
537
+ }
You can’t perform that action at this time.
0 commit comments