Skip to content

Commit b52bffc

Browse files
committed
fix: replace .unwrap() with .expect() in non-test scan paths (refs #275)
- semgrep_compat: refactor is_none()+unwrap() to let-else - semgrep_compat: expect("checked len == 1") on single-positive path - semgrep_taint: expect on find('.') guarded by prior rfind - scanner: expect on regex group 0 and Mutex::lock - terminal: expect on last_mut after push - foxguard_mcp: expect on json!() serialization
1 parent c91f715 commit b52bffc

5 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/bin/foxguard_mcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn format_scan_result(result: ScanResult) -> Value {
190190
"content": [
191191
{
192192
"type": "text",
193-
"text": serde_json::to_string(&output).unwrap()
193+
"text": serde_json::to_string(&output).expect("json!() is always serializable")
194194
}
195195
]
196196
})

src/engine/scanner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ fn block_comment_ignore_regex() -> &'static Regex {
343343

344344
fn parse_block_comment_ignore(line: &str) -> Option<(bool, InlineIgnoreSpec)> {
345345
let captures = block_comment_ignore_regex().captures(line)?;
346-
let full_match = captures.get(0).unwrap();
346+
let full_match = captures.get(0).expect("group 0 always present");
347347

348348
let mut spec = InlineIgnoreSpec::default();
349349
match captures.name("rules").map(|rules| rules.as_str().trim()) {
@@ -746,15 +746,15 @@ fn scan_files(
746746

747747
match std::fs::metadata(path) {
748748
Ok(m) if m.len() > max_file_size => {
749-
warnings.lock().unwrap().push(format!(
749+
warnings.lock().expect("lock poisoned").push(format!(
750750
"warning: skipping {} ({} bytes exceeds --max-file-size)",
751751
path.display(),
752752
m.len()
753753
));
754754
return Vec::new();
755755
}
756756
Err(_) => {
757-
warnings.lock().unwrap().push(format!(
757+
warnings.lock().expect("lock poisoned").push(format!(
758758
"warning: skipping {} (cannot read metadata)",
759759
path.display()
760760
));

src/report/terminal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn print_findings_full(
123123
current_file = &f.file;
124124
by_file.push((&f.file, Vec::new()));
125125
}
126-
by_file.last_mut().unwrap().1.push(f);
126+
by_file.last_mut().expect("just pushed").1.push(f);
127127
}
128128

129129
println!();

src/rules/semgrep_compat.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,9 @@ fn match_single_pattern(
390390
// Find the first meaningful child of the pattern (skip module/program wrapper)
391391
let pat_node = first_meaningful_node(pat_root, pattern);
392392

393-
if pat_node.is_none() {
393+
let Some(pat_node) = pat_node else {
394394
return results;
395-
}
396-
let pat_node = pat_node.unwrap();
395+
};
397396

398397
// Walk every node in the target tree and try matching
399398
walk_and_match(root, source, pat_node, pattern, &mut results);
@@ -890,7 +889,7 @@ fn build_matcher(yaml: &SemgrepRuleYaml) -> Result<PatternMatcher, String> {
890889
&& yaml.pattern_inside.is_none()
891890
&& yaml.pattern_not_inside.is_none()
892891
{
893-
return Ok(positives.into_iter().next().unwrap());
892+
return Ok(positives.into_iter().next().expect("checked len == 1"));
894893
}
895894

896895
if !positives.is_empty() {

src/rules/semgrep_taint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ fn compile_pattern(pattern: &str, role: MatcherRole) -> Option<GenericMatcher> {
641641
// `root.field` or `root.intermediate.field`. The engine only
642642
// supports one-level roots, so we take the leftmost segment as
643643
// the root and the outermost (last) segment as the field.
644-
let root = pat[..pat.find('.').unwrap()].to_string();
644+
let root = pat[..pat.find('.').expect("rfind guarantees at least one dot")].to_string();
645645
let field = pat[dot + 1..].to_string();
646646
if root.is_empty() || field.is_empty() {
647647
return None;

0 commit comments

Comments
 (0)