Skip to content

Commit

Permalink
Allow global prefix when matching diagnostic codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Oct 9, 2023
1 parent e680dcb commit 0ee9af0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -103,6 +106,7 @@ impl Config {
list: false,
run_only_ignored: false,
filter_exact: false,
diagnostic_code_prefix: String::new(),
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
},
});
Expand Down
73 changes: 73 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}

0 comments on commit 0ee9af0

Please sign in to comment.