-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Control flow: return
and raise
#17121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW0101_unreachable.py.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,34 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
unreachable.py:18:5: PLW0101 Unreachable code in `after_return` | ||
| | ||
16 | def after_return(): | ||
17 | return 1 | ||
18 | / print("unreachable") | ||
19 | | print("and this") | ||
| |_____________________^ PLW0101 | ||
20 | | ||
21 | def after_raise(): | ||
| | ||
|
||
unreachable.py:23:5: PLW0101 Unreachable code in `after_raise` | ||
| | ||
21 | def after_raise(): | ||
22 | raise ValueError | ||
23 | / print("unreachable") | ||
24 | | print("and this") | ||
| |_____________________^ PLW0101 | ||
25 | | ||
26 | def multiple_returns(): | ||
| | ||
|
||
unreachable.py:28:5: PLW0101 Unreachable code in `multiple_returns` | ||
| | ||
26 | def multiple_returns(): | ||
27 | return 1 | ||
28 | / print("unreachable") | ||
29 | | return 2 | ||
30 | | print("unreachable range should include above return") | ||
| |__________________________________________________________^ PLW0101 | ||
| |
18 changes: 18 additions & 0 deletions
18
crates/ruff_python_semantic/resources/test/fixtures/cfg/jumps.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def only_return(): | ||
return | ||
|
||
def after_return(): | ||
return 1 | ||
print("unreachable") | ||
print("and this") | ||
|
||
def after_raise(): | ||
raise ValueError | ||
print("unreachable") | ||
print("and this") | ||
|
||
def multiple_returns(): | ||
return 1 | ||
print("unreachable") | ||
return 2 | ||
print("and this") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,8 +192,12 @@ impl<'stmt> CFGBuilder<'stmt> { | |
|
||
/// Runs the core logic for the builder. | ||
fn process_stmts(&mut self, stmts: &'stmt [Stmt]) { | ||
let start = 0; | ||
for stmt in stmts { | ||
// SAFETY With notation as below, we always maintain the invariant | ||
// `start <= end + 1`. Since `end <= stmts.len() -1` we conclude that | ||
// `start <= stmts.len()`. It is therefore always safe to use `start` as | ||
MichaReiser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// the beginning of a range for the purposes of slicing into `stmts`. | ||
let mut start = 0; | ||
for (end, stmt) in stmts.iter().enumerate() { | ||
let cache_exit = self.exit(); | ||
match stmt { | ||
Stmt::FunctionDef(_) | ||
|
@@ -223,10 +227,30 @@ impl<'stmt> CFGBuilder<'stmt> { | |
Stmt::With(_) => {} | ||
|
||
// Jumps | ||
Stmt::Return(_) => {} | ||
Stmt::Return(_) => { | ||
let edges = Edges::always(self.cfg.terminal()); | ||
self.set_current_block_stmts(&stmts[start..=end]); | ||
self.set_current_block_edges(edges); | ||
start = end + 1; | ||
|
||
if stmts.get(start).is_some() { | ||
let next_block = self.new_block(); | ||
self.move_to(next_block); | ||
Comment on lines
+234
to
+238
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a bit repetitive. We may want to abstract this somehow (e.g. store |
||
} | ||
} | ||
Stmt::Break(_) => {} | ||
Stmt::Continue(_) => {} | ||
Stmt::Raise(_) => {} | ||
Stmt::Raise(_) => { | ||
let edges = Edges::always(self.cfg.terminal()); | ||
self.set_current_block_stmts(&stmts[start..=end]); | ||
self.set_current_block_edges(edges); | ||
start = end + 1; | ||
|
||
if stmts.get(start).is_some() { | ||
let next_block = self.new_block(); | ||
self.move_to(next_block); | ||
} | ||
} | ||
|
||
// An `assert` is a mixture of a switch and a jump. | ||
Stmt::Assert(_) => {} | ||
|
@@ -269,6 +293,11 @@ impl<'stmt> CFGBuilder<'stmt> { | |
self.current = block; | ||
} | ||
|
||
/// Makes new block and returns index | ||
fn new_block(&mut self) -> BlockId { | ||
self.cfg.blocks.push(BlockData::default()) | ||
} | ||
|
||
/// Populates the current basic block with the given set of statements. | ||
/// | ||
/// This should only be called once on any given block. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ruff_python_semantic/src/cfg/snapshots/ruff_python_semantic__cfg__tests__jumps.py.md.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
source: crates/ruff_python_semantic/src/cfg/mod.rs | ||
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram." | ||
--- | ||
## Function 0 | ||
### Source | ||
```python | ||
def only_return(): | ||
return | ||
``` | ||
|
||
### Control Flow Graph | ||
```mermaid | ||
flowchart TD | ||
node0["return"] | ||
node1((("EXIT"))) | ||
node0==>node1 | ||
``` | ||
|
||
## Function 1 | ||
### Source | ||
```python | ||
def after_return(): | ||
return 1 | ||
print("unreachable") | ||
print("and this") | ||
``` | ||
|
||
### Control Flow Graph | ||
```mermaid | ||
flowchart TD | ||
node0["return 1"] | ||
node1((("EXIT"))) | ||
node2["print(#quot;unreachable#quot;) | ||
print(#quot;and this#quot;)"] | ||
node0==>node1 | ||
node2==>node1 | ||
``` | ||
|
||
## Function 2 | ||
### Source | ||
```python | ||
def after_raise(): | ||
raise ValueError | ||
print("unreachable") | ||
print("and this") | ||
``` | ||
|
||
### Control Flow Graph | ||
```mermaid | ||
flowchart TD | ||
node0["raise ValueError"] | ||
node1((("EXIT"))) | ||
node2["print(#quot;unreachable#quot;) | ||
print(#quot;and this#quot;)"] | ||
node0==>node1 | ||
node2==>node1 | ||
``` | ||
|
||
## Function 3 | ||
### Source | ||
```python | ||
def multiple_returns(): | ||
return 1 | ||
print("unreachable") | ||
return 2 | ||
print("and this") | ||
``` | ||
|
||
### Control Flow Graph | ||
```mermaid | ||
flowchart TD | ||
node0["return 1"] | ||
node1((("EXIT"))) | ||
node2["print(#quot;unreachable#quot;) | ||
return 2"] | ||
node3["print(#quot;and this#quot;)"] | ||
node0==>node1 | ||
node2==>node1 | ||
node3==>node1 | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need multiline span diagnostics so that we can highlight the "why". (CC: @ntBre might be a good first rule to port)
With our current noqa system. Would a user have to put the noqa on the last line? Would that be confusing if they have
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's the opposite: noqa comments go at the start of the range. So I think it would be
Playground example with existing rule