Skip to content

Commit 3857349

Browse files
authored
refactor(verilog): use if let Some in for-loops instead of ? (#1155)
The `?` operator inside a `for` loop short-circuits the entire enclosing function on the first `None`, not just the current iteration. In practice `node.child(i)` returns `Some` for every `i < child_count()`, so this was harmless, but `if let Some(child) = node.child(i)` is more defensively correct and matches the pattern already used in `handle_package_import` and `handle_include_directive`. Closes #1118
1 parent 07d2c78 commit 3857349

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

crates/codegraph-core/src/extractors/verilog.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ fn find_class_name(node: &Node, source: &[u8]) -> Option<String> {
148148
return Some(text.to_string());
149149
}
150150
for i in 0..node.child_count() {
151-
let child = node.child(i)?;
152-
if child.kind() == "class_identifier" {
153-
return Some(extract_identifier_text(&child, source));
151+
if let Some(child) = node.child(i) {
152+
if child.kind() == "class_identifier" {
153+
return Some(extract_identifier_text(&child, source));
154+
}
154155
}
155156
}
156157
None
@@ -161,12 +162,13 @@ fn find_class_name(node: &Node, source: &[u8]) -> Option<String> {
161162
/// `class_identifier > simple_identifier`.
162163
fn find_class_superclass(node: &Node, source: &[u8]) -> Option<String> {
163164
for i in 0..node.child_count() {
164-
let child = node.child(i)?;
165-
if child.kind() == "class_type" {
166-
if let Some(id) = find_child(&child, "class_identifier") {
167-
return Some(extract_identifier_text(&id, source));
165+
if let Some(child) = node.child(i) {
166+
if child.kind() == "class_type" {
167+
if let Some(id) = find_child(&child, "class_identifier") {
168+
return Some(extract_identifier_text(&id, source));
169+
}
170+
return Some(node_text(&child, source).trim().to_string());
168171
}
169-
return Some(node_text(&child, source).trim().to_string());
170172
}
171173
}
172174
None

0 commit comments

Comments
 (0)