Skip to content

Commit

Permalink
fix handling of asserts in postprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
augustelalande committed Apr 16, 2024
1 parent c2c5b1d commit 4493aaf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Expand Up @@ -60,3 +60,12 @@ def func():
pass
else:
pass

def func():
for i in range(3):
if i == 2:
assert i is not None
break
else:
raise Exception()
x = 0
Expand Up @@ -355,3 +355,51 @@ flowchart TD
block1 --> block0
block0 --> return
```

## Function 11
### Source
```python
def func():
for i in range(3):
if i == 2:
assert i is not None
break
else:
raise Exception()
x = 0
```

### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["x = 0\n"]
block1["raise Exception()\n"]
block2[["Loop continue"]]
block3["break\n"]
block4[["Exception raised"]]
block5["assert i is not None\n"]
block6["if i == 2:
assert i is not None
break\n"]
block7["for i in range(3):
if i == 2:
assert i is not None
break
else:
raise Exception()\n"]
start --> block7
block7 -- "range(3)" --> block6
block7 -- "else" --> block1
block6 -- "i == 2" --> block5
block6 -- "else" --> block2
block5 -- "i is not None" --> block3
block5 -- "else" --> block4
block4 --> return
block3 --> block0
block2 --> block7
block1 --> return
block0 --> return
```
8 changes: 6 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/unreachable.rs
Expand Up @@ -894,13 +894,17 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
match block.stmts.last() {
Some(Stmt::For(_) | Stmt::While(_)) => {
self.post_process(next, Some(idx), exit, exit);
idx = orelse;
}
Some(Stmt::Assert(_)) => {
self.post_process(orelse, loop_start, loop_exit, exit);
idx = next;
}
_ => {
self.post_process(next, loop_start, loop_exit, exit);
idx = orelse;
}
};

idx = orelse;
}
NextBlock::Terminate => return,
}
Expand Down

0 comments on commit 4493aaf

Please sign in to comment.