diff --git a/src/config.rs b/src/config.rs index c3837923..30fe6e65 100644 --- a/src/config.rs +++ b/src/config.rs @@ -59,6 +59,9 @@ pub struct Config { pub run_only_ignored: bool, /// Filters must match exactly instead of just checking for substrings. pub filter_exact: bool, + /// Prefix added to all diagnostic code matchers. Note this will make it impossible + /// match codes which do not contain this prefix. + pub diagnostic_code_prefix: String, } impl Config { @@ -103,6 +106,7 @@ impl Config { list: false, run_only_ignored: false, filter_exact: false, + diagnostic_code_prefix: String::new(), } } diff --git a/src/lib.rs b/src/lib.rs index 17bec10c..5f6f2a3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1125,8 +1125,13 @@ fn check_annotations( ErrorMatchKind::Code(code) => { let found = msgs.iter().position(|msg| { msg.level == Level::Error - && msg.code.as_ref().is_some_and(|msg| *msg == **code) + && msg + .code + .as_ref() + .and_then(|code| code.strip_prefix(&config.diagnostic_code_prefix)) + .is_some_and(|msg| *msg == **code) }); + if let Some(found) = found { msgs.remove(found); continue; @@ -1141,7 +1146,10 @@ fn check_annotations( expected_line: Some(line), }, ErrorMatchKind::Code(code) => Error::CodeNotFound { - code: code.clone(), + code: Spanned::new( + format!("{}{}", config.diagnostic_code_prefix, **code), + code.span(), + ), expected_line: Some(line), }, }); diff --git a/src/tests.rs b/src/tests.rs index 2de55290..0e6c07f9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -462,3 +462,76 @@ fn main() { } } } + +#[test] +fn find_code_with_prefix() { + let s = r" +fn main() { + let _x: i32 = 0u32; //~ E0308 +} + "; + let comments = Comments::parse(s).unwrap(); + let config = Config { + diagnostic_code_prefix: "prefix::".into(), + ..config() + }; + { + let messages = vec![ + vec![], + vec![], + vec![], + vec![Message { + message: "mismatched types".to_string(), + level: Level::Error, + line_col: None, + code: Some("prefix::E0308".into()), + }], + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ) + .unwrap(); + match &errors[..] { + [] => {} + _ => panic!("{:#?}", errors), + } + } + + // missing prefix + { + let messages = vec![ + vec![], + vec![], + vec![], + vec![Message { + message: "mismatched types".to_string(), + level: Level::Error, + line_col: None, + code: Some("E0308".into()), + }], + ]; + let mut errors = vec![]; + check_annotations( + messages, + vec![], + Path::new("moobar"), + &mut errors, + &config, + "", + &comments, + ) + .unwrap(); + match &errors[..] { + [Error::CodeNotFound { code, .. }, Error::ErrorsWithoutPattern { msgs, .. }] + if **code == "prefix::E0308" && code.line().get() == 3 && msgs.len() == 1 => {} + _ => panic!("{:#?}", errors), + } + } +}