Skip to content

Commit 0ee9af0

Browse files
committed
Allow global prefix when matching diagnostic codes.
1 parent e680dcb commit 0ee9af0

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub struct Config {
5959
pub run_only_ignored: bool,
6060
/// Filters must match exactly instead of just checking for substrings.
6161
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,
6265
}
6366

6467
impl Config {
@@ -103,6 +106,7 @@ impl Config {
103106
list: false,
104107
run_only_ignored: false,
105108
filter_exact: false,
109+
diagnostic_code_prefix: String::new(),
106110
}
107111
}
108112

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,13 @@ fn check_annotations(
11251125
ErrorMatchKind::Code(code) => {
11261126
let found = msgs.iter().position(|msg| {
11271127
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)
11291133
});
1134+
11301135
if let Some(found) = found {
11311136
msgs.remove(found);
11321137
continue;
@@ -1141,7 +1146,10 @@ fn check_annotations(
11411146
expected_line: Some(line),
11421147
},
11431148
ErrorMatchKind::Code(code) => Error::CodeNotFound {
1144-
code: code.clone(),
1149+
code: Spanned::new(
1150+
format!("{}{}", config.diagnostic_code_prefix, **code),
1151+
code.span(),
1152+
),
11451153
expected_line: Some(line),
11461154
},
11471155
});

src/tests.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,76 @@ fn main() {
462462
}
463463
}
464464
}
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+
}

0 commit comments

Comments
 (0)