Skip to content

Commit 583d887

Browse files
Copilothxuhack
andcommitted
Refactor extract_safety_doc to reduce code duplication
Co-authored-by: hxuhack <13302178+hxuhack@users.noreply.github.com>
1 parent d24bbb4 commit 583d887

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

rapx/src/analysis/audit/mod.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ pub struct UnsafeApiEntry {
2929
pub safety_doc: Option<String>,
3030
}
3131

32+
/// Returns true if `line` is a Markdown heading that should stop content
33+
/// collection for a `# Safety` section at the given `level` (1 or 2).
34+
fn is_heading_stop(line: &str, level: usize) -> bool {
35+
let is_h1 = line.starts_with("# ") || line == "#";
36+
if level == 1 {
37+
return is_h1;
38+
}
39+
// level == 2: stop at `#` or `##` headings
40+
is_h1 || line.starts_with("## ") || line == "##"
41+
}
42+
3243
/// Extract the `# Safety` or `## Safety` section from a Rust doc comment string.
3344
///
3445
/// The `doc` parameter should be the concatenation of all `#[doc = "..."]` attribute
@@ -59,21 +70,11 @@ pub fn extract_safety_doc(doc: &str) -> Option<String> {
5970
let mut content_lines: Vec<&str> = Vec::new();
6071
for line in lines.iter().skip(start) {
6172
let trimmed = line.trim();
62-
// Stop at any heading at the same or higher level
63-
match safety_level {
64-
1 => {
65-
if trimmed.starts_with("# ") || trimmed == "#" {
66-
break;
67-
}
68-
}
69-
2 => {
70-
if trimmed.starts_with("# ") || trimmed == "#"
71-
|| trimmed.starts_with("## ") || trimmed == "##"
72-
{
73-
break;
74-
}
75-
}
76-
_ => {}
73+
// Stop at any heading at the same or higher level.
74+
// For level 1 (`# Safety`), any `#` heading stops the section.
75+
// For level 2 (`## Safety`), any `#` or `##` heading stops the section.
76+
if is_heading_stop(trimmed, safety_level) {
77+
break;
7778
}
7879
content_lines.push(trimmed);
7980
}

0 commit comments

Comments
 (0)