Skip to content

Commit d474f89

Browse files
committed
fix: use diagnostic name by default
Corresponds to cpp-linter/cpp-linter#173. This should never happen in the wild, but just in case there's an underlying problem with parsing clang-tidy output... This fixes a problem about hyperlinking a clang-tidy diagnostic name to the clang-tidy docs. Specifically when the diagnostic name does not satisfy the following conditions: - starts with "clang-analyzer-" - starts with "clang-diagnostic-" - contains at least 1 hyphen (`-`) This also removes the `.unwrap()` calls (bad practice) and adds a test for code coverage.
1 parent a4575ec commit d474f89

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

cpp-linter/src/clang_tools/clang_tidy.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,26 @@ pub struct TidyNotification {
7474

7575
impl TidyNotification {
7676
pub fn diagnostic_link(&self) -> String {
77-
if self.diagnostic.starts_with("clang-diagnostic") {
77+
if self.diagnostic.starts_with("clang-diagnostic-") {
7878
return self.diagnostic.clone();
7979
}
80-
let (category, name) = if self.diagnostic.starts_with("clang-analyzer-") {
81-
(
82-
"clang-analyzer",
83-
self.diagnostic.strip_prefix("clang-analyzer-").unwrap(),
80+
if let Some((category, name)) = if self.diagnostic.starts_with("clang-analyzer-") {
81+
self.diagnostic
82+
.strip_prefix("clang-analyzer-")
83+
.map(|n| ("clang-analyzer", n))
84+
} else {
85+
self.diagnostic.split_once('-')
86+
} {
87+
// In production, both category and name should be non-empty strings.
88+
// Clang does not actually have a diagnostic name whose category or name is empty.
89+
debug_assert!(!category.is_empty() && !name.is_empty());
90+
format!(
91+
"[{}](https://clang.llvm.org/extra/clang-tidy/checks/{category}/{name}.html)",
92+
self.diagnostic
8493
)
8594
} else {
86-
self.diagnostic.split_once('-').unwrap()
87-
};
88-
format!(
89-
"[{}](https://clang.llvm.org/extra/clang-tidy/checks/{category}/{name}.html)",
90-
self.diagnostic
91-
)
95+
self.diagnostic.clone()
96+
}
9297
}
9398
}
9499

@@ -392,6 +397,22 @@ mod test {
392397
assert_eq!(note.diagnostic_link(), expected);
393398
}
394399

400+
#[test]
401+
fn invalid_diagnostic_link() {
402+
let expected = "no_diagnostic_name".to_string();
403+
let note = TidyNotification {
404+
filename: String::from("some_src.cpp"),
405+
line: 1504,
406+
cols: 9,
407+
rationale: String::from("some rationale"),
408+
severity: String::from("warning"),
409+
diagnostic: expected.clone(),
410+
suggestion: vec![],
411+
fixed_lines: vec![],
412+
};
413+
assert_eq!(note.diagnostic_link(), expected);
414+
}
415+
395416
// ***************** test for regex parsing of clang-tidy stdout
396417

397418
#[test]

0 commit comments

Comments
 (0)