Add unreachable code rule (#5384)

Co-authored-by: Thomas de Zeeuw <thomas@astral.sh>
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Thomas de Zeeuw 2023-07-04 16:27:23 +02:00 committed by GitHub
parent 937de121f3
commit 0b963ddcfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 4688 additions and 4 deletions

1
.gitattributes vendored
View file

@ -4,3 +4,4 @@ crates/ruff/resources/test/fixtures/isort/line_ending_crlf.py text eol=crlf
crates/ruff/resources/test/fixtures/pycodestyle/W605_1.py text eol=crlf
ruff.schema.json linguist-generated=true text=auto eol=lf
*.md.snap linguist-language=Markdown

1
Cargo.lock generated
View file

@ -1865,6 +1865,7 @@ dependencies = [
"result-like",
"ruff_cache",
"ruff_diagnostics",
"ruff_index",
"ruff_macros",
"ruff_python_ast",
"ruff_python_semantic",

View file

@ -17,6 +17,7 @@ name = "ruff"
[dependencies]
ruff_cache = { path = "../ruff_cache" }
ruff_diagnostics = { path = "../ruff_diagnostics", features = ["serde"] }
ruff_index = { path = "../ruff_index" }
ruff_macros = { path = "../ruff_macros" }
ruff_python_whitespace = { path = "../ruff_python_whitespace" }
ruff_python_ast = { path = "../ruff_python_ast", features = ["serde"] }
@ -88,3 +89,5 @@ colored = { workspace = true, features = ["no-color"] }
[features]
default = []
schemars = ["dep:schemars"]
# Enables the UnreachableCode rule
unreachable-code = []

View file

@ -0,0 +1,11 @@
def func():
assert True
def func():
assert False
def func():
assert True, "oops"
def func():
assert False, "oops"

View file

@ -0,0 +1,41 @@
def func():
async for i in range(5):
print(i)
def func():
async for i in range(20):
print(i)
else:
return 0
def func():
async for i in range(10):
if i == 5:
return 1
return 0
def func():
async for i in range(111):
if i == 5:
return 1
else:
return 0
return 2
def func():
async for i in range(12):
continue
def func():
async for i in range(1110):
if True:
continue
def func():
async for i in range(13):
break
def func():
async for i in range(1110):
if True:
break

View file

@ -0,0 +1,41 @@
def func():
for i in range(5):
print(i)
def func():
for i in range(20):
print(i)
else:
return 0
def func():
for i in range(10):
if i == 5:
return 1
return 0
def func():
for i in range(111):
if i == 5:
return 1
else:
return 0
return 2
def func():
for i in range(12):
continue
def func():
for i in range(1110):
if True:
continue
def func():
for i in range(13):
break
def func():
for i in range(1110):
if True:
break

View file

@ -0,0 +1,108 @@
def func():
if False:
return 0
return 1
def func():
if True:
return 1
return 0
def func():
if False:
return 0
else:
return 1
def func():
if True:
return 1
else:
return 0
def func():
if False:
return 0
else:
return 1
return "unreachable"
def func():
if True:
return 1
else:
return 0
return "unreachable"
def func():
if True:
if True:
return 1
return 2
else:
return 3
return "unreachable2"
def func():
if False:
return 0
def func():
if True:
return 1
def func():
if True:
return 1
elif False:
return 2
else:
return 0
def func():
if False:
return 1
elif True:
return 2
else:
return 0
def func():
if True:
if False:
return 0
elif True:
return 1
else:
return 2
return 3
elif True:
return 4
else:
return 5
return 6
def func():
if False:
return "unreached"
elif False:
return "also unreached"
return "reached"
# Test case found in the Bokeh repository that trigger a false positive.
def func(self, obj: BytesRep) -> bytes:
data = obj["data"]
if isinstance(data, str):
return base64.b64decode(data)
elif isinstance(data, Buffer):
buffer = data
else:
id = data["id"]
if id in self._buffers:
buffer = self._buffers[id]
else:
self.error(f"can't resolve buffer '{id}'")
return buffer.data

View file

@ -0,0 +1,131 @@
def func(status):
match status:
case _:
return 0
return "unreachable"
def func(status):
match status:
case 1:
return 1
return 0
def func(status):
match status:
case 1:
return 1
case _:
return 0
def func(status):
match status:
case 1 | 2 | 3:
return 5
return 6
def func(status):
match status:
case 1 | 2 | 3:
return 5
case _:
return 10
return 0
def func(status):
match status:
case 0:
return 0
case 1:
return 1
case 1:
return "1 again"
case _:
return 3
def func(status):
i = 0
match status, i:
case _, _:
return 0
def func(status):
i = 0
match status, i:
case _, 0:
return 0
case _, 2:
return 0
def func(point):
match point:
case (0, 0):
print("Origin")
case _:
raise ValueError("oops")
def func(point):
match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")
def where_is(point):
class Point:
x: int
y: int
match point:
case Point(x=0, y=0):
print("Origin")
case Point(x=0, y=y):
print(f"Y={y}")
case Point(x=x, y=0):
print(f"X={x}")
case Point():
print("Somewhere else")
case _:
print("Not a point")
def func(points):
match points:
case []:
print("No points")
case [Point(0, 0)]:
print("The origin")
case [Point(x, y)]:
print(f"Single point {x}, {y}")
case [Point(0, y1), Point(0, y2)]:
print(f"Two on the Y axis at {y1}, {y2}")
case _:
print("Something else")
def func(point):
match point:
case Point(x, y) if x == y:
print(f"Y=X at {x}")
case Point(x, y):
print(f"Not on the diagonal")
def func():
from enum import Enum
class Color(Enum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
match color:
case Color.RED:
print("I see red!")
case Color.GREEN:
print("Grass is green")
case Color.BLUE:
print("I'm feeling the blues :(")

View file

@ -0,0 +1,5 @@
def func():
raise Exception
def func():
raise "a glass!"

View file

@ -0,0 +1,23 @@
def func():
pass
def func():
pass
def func():
return
def func():
return 1
def func():
return 1
return "unreachable"
def func():
i = 0
def func():
i = 0
i += 2
return i

View file

@ -0,0 +1,41 @@
def func():
try:
...
except Exception:
...
except OtherException as e:
...
else:
...
finally:
...
def func():
try:
...
except Exception:
...
def func():
try:
...
except Exception:
...
except OtherException as e:
...
def func():
try:
...
except Exception:
...
except OtherException as e:
...
else:
...
def func():
try:
...
finally:
...

View file

@ -0,0 +1,121 @@
def func():
while False:
return "unreachable"
return 1
def func():
while False:
return "unreachable"
else:
return 1
def func():
while False:
return "unreachable"
else:
return 1
return "also unreachable"
def func():
while True:
return 1
return "unreachable"
def func():
while True:
return 1
else:
return "unreachable"
def func():
while True:
return 1
else:
return "unreachable"
return "also unreachable"
def func():
i = 0
while False:
i += 1
return i
def func():
i = 0
while True:
i += 1
return i
def func():
while True:
pass
return 1
def func():
i = 0
while True:
if True:
print("ok")
i += 1
return i
def func():
i = 0
while True:
if False:
print("ok")
i += 1
return i
def func():
while True:
if True:
return 1
return 0
def func():
while True:
continue
def func():
while False:
continue
def func():
while True:
break
def func():
while False:
break
def func():
while True:
if True:
continue
def func():
while True:
if True:
break
'''
TODO: because `try` statements aren't handled this triggers a false positive as
the last statement is reached, but the rules thinks it isn't (it doesn't
see/process the break statement).
# Test case found in the Bokeh repository that trigger a false positive.
def bokeh2(self, host: str = DEFAULT_HOST, port: int = DEFAULT_PORT) -> None:
self.stop_serving = False
while True:
try:
self.server = HTTPServer((host, port), HtmlOnlyHandler)
self.host = host
self.port = port
break
except OSError:
log.debug(f"port {port} is in use, trying to next one")
port += 1
self.thread = threading.Thread(target=self._run_web_server)
'''

View file

@ -0,0 +1,185 @@
def after_return():
return "reachable"
return "unreachable"
async def also_works_on_async_functions():
return "reachable"
return "unreachable"
def if_always_true():
if True:
return "reachable"
return "unreachable"
def if_always_false():
if False:
return "unreachable"
return "reachable"
def if_elif_always_false():
if False:
return "unreachable"
elif False:
return "also unreachable"
return "reachable"
def if_elif_always_true():
if False:
return "unreachable"
elif True:
return "reachable"
return "also unreachable"
def ends_with_if():
if False:
return "unreachable"
else:
return "reachable"
def infinite_loop():
while True:
continue
return "unreachable"
''' TODO: we could determine these, but we don't yet.
def for_range_return():
for i in range(10):
if i == 5:
return "reachable"
return "unreachable"
def for_range_else():
for i in range(111):
if i == 5:
return "reachable"
else:
return "unreachable"
return "also unreachable"
def for_range_break():
for i in range(13):
return "reachable"
return "unreachable"
def for_range_if_break():
for i in range(1110):
if True:
return "reachable"
return "unreachable"
'''
def match_wildcard(status):
match status:
case _:
return "reachable"
return "unreachable"
def match_case_and_wildcard(status):
match status:
case 1:
return "reachable"
case _:
return "reachable"
return "unreachable"
def raise_exception():
raise Exception
return "unreachable"
def while_false():
while False:
return "unreachable"
return "reachable"
def while_false_else():
while False:
return "unreachable"
else:
return "reachable"
def while_false_else_return():
while False:
return "unreachable"
else:
return "reachable"
return "also unreachable"
def while_true():
while True:
return "reachable"
return "unreachable"
def while_true_else():
while True:
return "reachable"
else:
return "unreachable"
def while_true_else_return():
while True:
return "reachable"
else:
return "unreachable"
return "also unreachable"
def while_false_var_i():
i = 0
while False:
i += 1
return i
def while_true_var_i():
i = 0
while True:
i += 1
return i
def while_infinite():
while True:
pass
return "unreachable"
def while_if_true():
while True:
if True:
return "reachable"
return "unreachable"
# Test case found in the Bokeh repository that trigger a false positive.
def bokeh1(self, obj: BytesRep) -> bytes:
data = obj["data"]
if isinstance(data, str):
return base64.b64decode(data)
elif isinstance(data, Buffer):
buffer = data
else:
id = data["id"]
if id in self._buffers:
buffer = self._buffers[id]
else:
self.error(f"can't resolve buffer '{id}'")
return buffer.data
'''
TODO: because `try` statements aren't handled this triggers a false positive as
the last statement is reached, but the rules thinks it isn't (it doesn't
see/process the break statement).
# Test case found in the Bokeh repository that trigger a false positive.
def bokeh2(self, host: str = DEFAULT_HOST, port: int = DEFAULT_PORT) -> None:
self.stop_serving = False
while True:
try:
self.server = HTTPServer((host, port), HtmlOnlyHandler)
self.host = host
self.port = port
break
except OSError:
log.debug(f"port {port} is in use, trying to next one")
port += 1
self.thread = threading.Thread(target=self._run_web_server)
'''

View file

@ -619,6 +619,11 @@ where
);
}
}
#[cfg(feature = "unreachable-code")]
if self.enabled(Rule::UnreachableCode) {
self.diagnostics
.extend(ruff::rules::unreachable::in_function(name, body));
}
}
Stmt::Return(_) => {
if self.enabled(Rule::ReturnOutsideFunction) {

View file

@ -761,6 +761,8 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Ruff, "011") => (RuleGroup::Unspecified, rules::ruff::rules::StaticKeyDictComprehension),
(Ruff, "012") => (RuleGroup::Unspecified, rules::ruff::rules::MutableClassDefault),
(Ruff, "013") => (RuleGroup::Unspecified, rules::ruff::rules::ImplicitOptional),
#[cfg(feature = "unreachable-code")]
(Ruff, "014") => (RuleGroup::Nursery, rules::ruff::rules::UnreachableCode),
(Ruff, "100") => (RuleGroup::Unspecified, rules::ruff::rules::UnusedNOQA),
(Ruff, "200") => (RuleGroup::Unspecified, rules::ruff::rules::InvalidPyprojectToml),

View file

@ -30,6 +30,10 @@ mod tests {
#[test_case(Rule::MutableDataclassDefault, Path::new("RUF008.py"))]
#[test_case(Rule::PairwiseOverZipped, Path::new("RUF007.py"))]
#[test_case(Rule::StaticKeyDictComprehension, Path::new("RUF011.py"))]
#[cfg_attr(
feature = "unreachable-code",
test_case(Rule::UnreachableCode, Path::new("RUF014.py"))
)]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(

View file

@ -9,6 +9,8 @@ pub(crate) use mutable_class_default::*;
pub(crate) use mutable_dataclass_default::*;
pub(crate) use pairwise_over_zipped::*;
pub(crate) use static_key_dict_comprehension::*;
#[cfg(feature = "unreachable-code")]
pub(crate) use unreachable::*;
pub(crate) use unused_noqa::*;
mod ambiguous_unicode_character;
@ -24,6 +26,8 @@ mod mutable_class_default;
mod mutable_dataclass_default;
mod pairwise_over_zipped;
mod static_key_dict_comprehension;
#[cfg(feature = "unreachable-code")]
pub(crate) mod unreachable;
mod unused_noqa;
#[derive(Clone, Copy)]

View file

@ -0,0 +1,97 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
assert True
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1[["Exception raised"]]
block2["assert True\n"]
start --> block2
block2 -- "True" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 1
### Source
```python
def func():
assert False
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1[["Exception raised"]]
block2["assert False\n"]
start --> block2
block2 -- "False" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 2
### Source
```python
def func():
assert True, "oops"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1[["Exception raised"]]
block2["assert True, #quot;oops#quot;\n"]
start --> block2
block2 -- "True" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 3
### Source
```python
def func():
assert False, "oops"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1[["Exception raised"]]
block2["assert False, #quot;oops#quot;\n"]
start --> block2
block2 -- "False" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```

View file

@ -0,0 +1,241 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
async for i in range(5):
print(i)
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["print(i)\n"]
block2["async for i in range(5):
print(i)\n"]
start --> block2
block2 -- "range(5)" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 1
### Source
```python
def func():
async for i in range(20):
print(i)
else:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["print(i)\n"]
block1["return 0\n"]
block2["async for i in range(20):
print(i)
else:
return 0\n"]
start --> block2
block2 -- "range(20)" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 2
### Source
```python
def func():
async for i in range(10):
if i == 5:
return 1
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 1\n"]
block2["if i == 5:
return 1\n"]
block3["async for i in range(10):
if i == 5:
return 1\n"]
start --> block3
block3 -- "range(10)" --> block2
block3 -- "else" --> block0
block2 -- "i == 5" --> block1
block2 -- "else" --> block3
block1 --> return
block0 --> return
```
## Function 3
### Source
```python
def func():
async for i in range(111):
if i == 5:
return 1
else:
return 0
return 2
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 2\n"]
block1["return 1\n"]
block2["if i == 5:
return 1\n"]
block3["return 0\n"]
block4["async for i in range(111):
if i == 5:
return 1
else:
return 0\n"]
start --> block4
block4 -- "range(111)" --> block2
block4 -- "else" --> block3
block3 --> return
block2 -- "i == 5" --> block1
block2 -- "else" --> block4
block1 --> return
block0 --> return
```
## Function 4
### Source
```python
def func():
async for i in range(12):
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["async for i in range(12):
continue\n"]
start --> block2
block2 -- "range(12)" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 5
### Source
```python
def func():
async for i in range(1110):
if True:
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["if True:
continue\n"]
block3["async for i in range(1110):
if True:
continue\n"]
start --> block3
block3 -- "range(1110)" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> block3
block0 --> return
```
## Function 6
### Source
```python
def func():
async for i in range(13):
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["async for i in range(13):
break\n"]
start --> block2
block2 -- "range(13)" --> block1
block2 -- "else" --> block0
block1 --> block0
block0 --> return
```
## Function 7
### Source
```python
def func():
async for i in range(1110):
if True:
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["if True:
break\n"]
block3["async for i in range(1110):
if True:
break\n"]
start --> block3
block3 -- "range(1110)" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> block0
block0 --> return
```

View file

@ -0,0 +1,241 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
for i in range(5):
print(i)
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["print(i)\n"]
block2["for i in range(5):
print(i)\n"]
start --> block2
block2 -- "range(5)" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 1
### Source
```python
def func():
for i in range(20):
print(i)
else:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["print(i)\n"]
block1["return 0\n"]
block2["for i in range(20):
print(i)
else:
return 0\n"]
start --> block2
block2 -- "range(20)" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 2
### Source
```python
def func():
for i in range(10):
if i == 5:
return 1
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 1\n"]
block2["if i == 5:
return 1\n"]
block3["for i in range(10):
if i == 5:
return 1\n"]
start --> block3
block3 -- "range(10)" --> block2
block3 -- "else" --> block0
block2 -- "i == 5" --> block1
block2 -- "else" --> block3
block1 --> return
block0 --> return
```
## Function 3
### Source
```python
def func():
for i in range(111):
if i == 5:
return 1
else:
return 0
return 2
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 2\n"]
block1["return 1\n"]
block2["if i == 5:
return 1\n"]
block3["return 0\n"]
block4["for i in range(111):
if i == 5:
return 1
else:
return 0\n"]
start --> block4
block4 -- "range(111)" --> block2
block4 -- "else" --> block3
block3 --> return
block2 -- "i == 5" --> block1
block2 -- "else" --> block4
block1 --> return
block0 --> return
```
## Function 4
### Source
```python
def func():
for i in range(12):
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["for i in range(12):
continue\n"]
start --> block2
block2 -- "range(12)" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 5
### Source
```python
def func():
for i in range(1110):
if True:
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["if True:
continue\n"]
block3["for i in range(1110):
if True:
continue\n"]
start --> block3
block3 -- "range(1110)" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> block3
block0 --> return
```
## Function 6
### Source
```python
def func():
for i in range(13):
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["for i in range(13):
break\n"]
start --> block2
block2 -- "range(13)" --> block1
block2 -- "else" --> block0
block1 --> block0
block0 --> return
```
## Function 7
### Source
```python
def func():
for i in range(1110):
if True:
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["if True:
break\n"]
block3["for i in range(1110):
if True:
break\n"]
start --> block3
block3 -- "range(1110)" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> block0
block0 --> return
```

View file

@ -0,0 +1,535 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
if False:
return 0
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["return 0\n"]
block2["if False:
return 0\n"]
start --> block2
block2 -- "False" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 1
### Source
```python
def func():
if True:
return 1
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 1\n"]
block2["if True:
return 1\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 2
### Source
```python
def func():
if False:
return 0
else:
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 1\n"]
block2["if False:
return 0
else:
return 1\n"]
start --> block2
block2 -- "False" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 3
### Source
```python
def func():
if True:
return 1
else:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["return 0\n"]
block2["if True:
return 1
else:
return 0\n"]
start --> block2
block2 -- "True" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 4
### Source
```python
def func():
if False:
return 0
else:
return 1
return "unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable#quot;\n"]
block1["return 0\n"]
block2["return 1\n"]
block3["if False:
return 0
else:
return 1\n"]
start --> block3
block3 -- "False" --> block1
block3 -- "else" --> block2
block2 --> return
block1 --> return
block0 --> return
```
## Function 5
### Source
```python
def func():
if True:
return 1
else:
return 0
return "unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable#quot;\n"]
block1["return 1\n"]
block2["return 0\n"]
block3["if True:
return 1
else:
return 0\n"]
start --> block3
block3 -- "True" --> block1
block3 -- "else" --> block2
block2 --> return
block1 --> return
block0 --> return
```
## Function 6
### Source
```python
def func():
if True:
if True:
return 1
return 2
else:
return 3
return "unreachable2"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable2#quot;\n"]
block1["return 2\n"]
block2["return 1\n"]
block3["if True:
return 1\n"]
block4["return 3\n"]
block5["if True:
if True:
return 1
return 2
else:
return 3\n"]
start --> block5
block5 -- "True" --> block3
block5 -- "else" --> block4
block4 --> return
block3 -- "True" --> block2
block3 -- "else" --> block1
block2 --> return
block1 --> return
block0 --> return
```
## Function 7
### Source
```python
def func():
if False:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["return 0\n"]
block2["if False:
return 0\n"]
start --> block2
block2 -- "False" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 8
### Source
```python
def func():
if True:
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["return 1\n"]
block2["if True:
return 1\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 9
### Source
```python
def func():
if True:
return 1
elif False:
return 2
else:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["return 2\n"]
block2["return 0\n"]
block3["elif False:
return 2
else:
return 0\n"]
block4["if True:
return 1
elif False:
return 2
else:
return 0\n"]
start --> block4
block4 -- "True" --> block0
block4 -- "else" --> block3
block3 -- "False" --> block1
block3 -- "else" --> block2
block2 --> return
block1 --> return
block0 --> return
```
## Function 10
### Source
```python
def func():
if False:
return 1
elif True:
return 2
else:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["return 2\n"]
block2["return 0\n"]
block3["elif True:
return 2
else:
return 0\n"]
block4["if False:
return 1
elif True:
return 2
else:
return 0\n"]
start --> block4
block4 -- "False" --> block0
block4 -- "else" --> block3
block3 -- "True" --> block1
block3 -- "else" --> block2
block2 --> return
block1 --> return
block0 --> return
```
## Function 11
### Source
```python
def func():
if True:
if False:
return 0
elif True:
return 1
else:
return 2
return 3
elif True:
return 4
else:
return 5
return 6
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 6\n"]
block1["return 3\n"]
block2["return 0\n"]
block3["return 1\n"]
block4["return 2\n"]
block5["elif True:
return 1
else:
return 2\n"]
block6["if False:
return 0
elif True:
return 1
else:
return 2\n"]
block7["return 4\n"]
block8["return 5\n"]
block9["elif True:
return 4
else:
return 5\n"]
block10["if True:
if False:
return 0
elif True:
return 1
else:
return 2
return 3
elif True:
return 4
else:
return 5\n"]
start --> block10
block10 -- "True" --> block6
block10 -- "else" --> block9
block9 -- "True" --> block7
block9 -- "else" --> block8
block8 --> return
block7 --> return
block6 -- "False" --> block2
block6 -- "else" --> block5
block5 -- "True" --> block3
block5 -- "else" --> block4
block4 --> return
block3 --> return
block2 --> return
block1 --> return
block0 --> return
```
## Function 12
### Source
```python
def func():
if False:
return "unreached"
elif False:
return "also unreached"
return "reached"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;reached#quot;\n"]
block1["return #quot;unreached#quot;\n"]
block2["return #quot;also unreached#quot;\n"]
block3["elif False:
return #quot;also unreached#quot;\n"]
block4["if False:
return #quot;unreached#quot;
elif False:
return #quot;also unreached#quot;\n"]
start --> block4
block4 -- "False" --> block1
block4 -- "else" --> block3
block3 -- "False" --> block2
block3 -- "else" --> block0
block2 --> return
block1 --> return
block0 --> return
```
## Function 13
### Source
```python
def func(self, obj: BytesRep) -> bytes:
data = obj["data"]
if isinstance(data, str):
return base64.b64decode(data)
elif isinstance(data, Buffer):
buffer = data
else:
id = data["id"]
if id in self._buffers:
buffer = self._buffers[id]
else:
self.error(f"can't resolve buffer '{id}'")
return buffer.data
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return buffer.data\n"]
block1["return base64.b64decode(data)\n"]
block2["buffer = data\n"]
block3["buffer = self._buffers[id]\n"]
block4["self.error(f#quot;can't resolve buffer '{id}'#quot;)\n"]
block5["id = data[#quot;id#quot;]\nif id in self._buffers:
buffer = self._buffers[id]
else:
self.error(f#quot;can't resolve buffer '{id}'#quot;)\n"]
block6["elif isinstance(data, Buffer):
buffer = data
else:
id = data[#quot;id#quot;]
if id in self._buffers:
buffer = self._buffers[id]
else:
self.error(f#quot;can't resolve buffer '{id}'#quot;)\n"]
block7["data = obj[#quot;data#quot;]\nif isinstance(data, str):
return base64.b64decode(data)
elif isinstance(data, Buffer):
buffer = data
else:
id = data[#quot;id#quot;]
if id in self._buffers:
buffer = self._buffers[id]
else:
self.error(f#quot;can't resolve buffer '{id}'#quot;)\n"]
start --> block7
block7 -- "isinstance(data, str)" --> block1
block7 -- "else" --> block6
block6 -- "isinstance(data, Buffer)" --> block2
block6 -- "else" --> block5
block5 -- "id in self._buffers" --> block3
block5 -- "else" --> block4
block4 --> block0
block3 --> block0
block2 --> block0
block1 --> return
block0 --> return
```

View file

@ -0,0 +1,776 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func(status):
match status:
case _:
return 0
return "unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable#quot;\n"]
block1["return 0\n"]
block2["match status:
case _:
return 0\n"]
start --> block2
block2 --> block1
block1 --> return
block0 --> return
```
## Function 1
### Source
```python
def func(status):
match status:
case 1:
return 1
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 1\n"]
block2["match status:
case 1:
return 1\n"]
start --> block2
block2 -- "case 1" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 2
### Source
```python
def func(status):
match status:
case 1:
return 1
case _:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["match status:
case 1:
return 1
case _:
return 0\n"]
block2["return 1\n"]
block3["match status:
case 1:
return 1
case _:
return 0\n"]
start --> block3
block3 -- "case 1" --> block2
block3 -- "else" --> block1
block2 --> return
block1 --> block0
block0 --> return
```
## Function 3
### Source
```python
def func(status):
match status:
case 1 | 2 | 3:
return 5
return 6
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 6\n"]
block1["return 5\n"]
block2["match status:
case 1 | 2 | 3:
return 5\n"]
start --> block2
block2 -- "case 1 | 2 | 3" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 4
### Source
```python
def func(status):
match status:
case 1 | 2 | 3:
return 5
case _:
return 10
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 10\n"]
block2["match status:
case 1 | 2 | 3:
return 5
case _:
return 10\n"]
block3["return 5\n"]
block4["match status:
case 1 | 2 | 3:
return 5
case _:
return 10\n"]
start --> block4
block4 -- "case 1 | 2 | 3" --> block3
block4 -- "else" --> block2
block3 --> return
block2 --> block1
block1 --> return
block0 --> return
```
## Function 5
### Source
```python
def func(status):
match status:
case 0:
return 0
case 1:
return 1
case 1:
return "1 again"
case _:
return 3
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 3\n"]
block1["match status:
case 0:
return 0
case 1:
return 1
case 1:
return #quot;1 again#quot;
case _:
return 3\n"]
block2["return #quot;1 again#quot;\n"]
block3["match status:
case 0:
return 0
case 1:
return 1
case 1:
return #quot;1 again#quot;
case _:
return 3\n"]
block4["return 1\n"]
block5["match status:
case 0:
return 0
case 1:
return 1
case 1:
return #quot;1 again#quot;
case _:
return 3\n"]
block6["return 0\n"]
block7["match status:
case 0:
return 0
case 1:
return 1
case 1:
return #quot;1 again#quot;
case _:
return 3\n"]
start --> block7
block7 -- "case 0" --> block6
block7 -- "else" --> block5
block6 --> return
block5 -- "case 1" --> block4
block5 -- "else" --> block3
block4 --> return
block3 -- "case 1" --> block2
block3 -- "else" --> block1
block2 --> return
block1 --> block0
block0 --> return
```
## Function 6
### Source
```python
def func(status):
i = 0
match status, i:
case _, _:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["return 0\n"]
block2["match status, i:
case _, _:
return 0\n"]
block3["i = 0\n"]
start --> block3
block3 --> block2
block2 -- "case _, _" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 7
### Source
```python
def func(status):
i = 0
match status, i:
case _, 0:
return 0
case _, 2:
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["return 0\n"]
block2["match status, i:
case _, 0:
return 0
case _, 2:
return 0\n"]
block3["return 0\n"]
block4["match status, i:
case _, 0:
return 0
case _, 2:
return 0\n"]
block5["i = 0\n"]
start --> block5
block5 --> block4
block4 -- "case _, 0" --> block3
block4 -- "else" --> block2
block3 --> return
block2 -- "case _, 2" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 8
### Source
```python
def func(point):
match point:
case (0, 0):
print("Origin")
case _:
raise ValueError("oops")
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["raise ValueError(#quot;oops#quot;)\n"]
block2["match point:
case (0, 0):
print(#quot;Origin#quot;)
case _:
raise ValueError(#quot;oops#quot;)\n"]
block3["print(#quot;Origin#quot;)\n"]
block4["match point:
case (0, 0):
print(#quot;Origin#quot;)
case _:
raise ValueError(#quot;oops#quot;)\n"]
start --> block4
block4 -- "case (0, 0)" --> block3
block4 -- "else" --> block2
block3 --> block0
block2 --> block1
block1 --> return
block0 --> return
```
## Function 9
### Source
```python
def func(point):
match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["raise ValueError(#quot;Not a point#quot;)\n"]
block2["match point:
case (0, 0):
print(#quot;Origin#quot;)
case (0, y):
print(f#quot;Y={y}#quot;)
case (x, 0):
print(f#quot;X={x}#quot;)
case (x, y):
print(f#quot;X={x}, Y={y}#quot;)
case _:
raise ValueError(#quot;Not a point#quot;)\n"]
block3["print(f#quot;X={x}, Y={y}#quot;)\n"]
block4["match point:
case (0, 0):
print(#quot;Origin#quot;)
case (0, y):
print(f#quot;Y={y}#quot;)
case (x, 0):
print(f#quot;X={x}#quot;)
case (x, y):
print(f#quot;X={x}, Y={y}#quot;)
case _:
raise ValueError(#quot;Not a point#quot;)\n"]
block5["print(f#quot;X={x}#quot;)\n"]
block6["match point:
case (0, 0):
print(#quot;Origin#quot;)
case (0, y):
print(f#quot;Y={y}#quot;)
case (x, 0):
print(f#quot;X={x}#quot;)
case (x, y):
print(f#quot;X={x}, Y={y}#quot;)
case _:
raise ValueError(#quot;Not a point#quot;)\n"]
block7["print(f#quot;Y={y}#quot;)\n"]
block8["match point:
case (0, 0):
print(#quot;Origin#quot;)
case (0, y):
print(f#quot;Y={y}#quot;)
case (x, 0):
print(f#quot;X={x}#quot;)
case (x, y):
print(f#quot;X={x}, Y={y}#quot;)
case _:
raise ValueError(#quot;Not a point#quot;)\n"]
block9["print(#quot;Origin#quot;)\n"]
block10["match point:
case (0, 0):
print(#quot;Origin#quot;)
case (0, y):
print(f#quot;Y={y}#quot;)
case (x, 0):
print(f#quot;X={x}#quot;)
case (x, y):
print(f#quot;X={x}, Y={y}#quot;)
case _:
raise ValueError(#quot;Not a point#quot;)\n"]
start --> block10
block10 -- "case (0, 0)" --> block9
block10 -- "else" --> block8
block9 --> block0
block8 -- "case (0, y)" --> block7
block8 -- "else" --> block6
block7 --> block0
block6 -- "case (x, 0)" --> block5
block6 -- "else" --> block4
block5 --> block0
block4 -- "case (x, y)" --> block3
block4 -- "else" --> block2
block3 --> block0
block2 --> block1
block1 --> return
block0 --> return
```
## Function 10
### Source
```python
def where_is(point):
class Point:
x: int
y: int
match point:
case Point(x=0, y=0):
print("Origin")
case Point(x=0, y=y):
print(f"Y={y}")
case Point(x=x, y=0):
print(f"X={x}")
case Point():
print("Somewhere else")
case _:
print("Not a point")
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["print(#quot;Not a point#quot;)\n"]
block2["match point:
case Point(x=0, y=0):
print(#quot;Origin#quot;)
case Point(x=0, y=y):
print(f#quot;Y={y}#quot;)
case Point(x=x, y=0):
print(f#quot;X={x}#quot;)
case Point():
print(#quot;Somewhere else#quot;)
case _:
print(#quot;Not a point#quot;)\n"]
block3["print(#quot;Somewhere else#quot;)\n"]
block4["match point:
case Point(x=0, y=0):
print(#quot;Origin#quot;)
case Point(x=0, y=y):
print(f#quot;Y={y}#quot;)
case Point(x=x, y=0):
print(f#quot;X={x}#quot;)
case Point():
print(#quot;Somewhere else#quot;)
case _:
print(#quot;Not a point#quot;)\n"]
block5["print(f#quot;X={x}#quot;)\n"]
block6["match point:
case Point(x=0, y=0):
print(#quot;Origin#quot;)
case Point(x=0, y=y):
print(f#quot;Y={y}#quot;)
case Point(x=x, y=0):
print(f#quot;X={x}#quot;)
case Point():
print(#quot;Somewhere else#quot;)
case _:
print(#quot;Not a point#quot;)\n"]
block7["print(f#quot;Y={y}#quot;)\n"]
block8["match point:
case Point(x=0, y=0):
print(#quot;Origin#quot;)
case Point(x=0, y=y):
print(f#quot;Y={y}#quot;)
case Point(x=x, y=0):
print(f#quot;X={x}#quot;)
case Point():
print(#quot;Somewhere else#quot;)
case _:
print(#quot;Not a point#quot;)\n"]
block9["print(#quot;Origin#quot;)\n"]
block10["match point:
case Point(x=0, y=0):
print(#quot;Origin#quot;)
case Point(x=0, y=y):
print(f#quot;Y={y}#quot;)
case Point(x=x, y=0):
print(f#quot;X={x}#quot;)
case Point():
print(#quot;Somewhere else#quot;)
case _:
print(#quot;Not a point#quot;)\n"]
block11["class Point:
x: int
y: int\n"]
start --> block11
block11 --> block10
block10 -- "case Point(x=0, y=0)" --> block9
block10 -- "else" --> block8
block9 --> block0
block8 -- "case Point(x=0, y=y)" --> block7
block8 -- "else" --> block6
block7 --> block0
block6 -- "case Point(x=x, y=0)" --> block5
block6 -- "else" --> block4
block5 --> block0
block4 -- "case Point()" --> block3
block4 -- "else" --> block2
block3 --> block0
block2 --> block1
block1 --> block0
block0 --> return
```
## Function 11
### Source
```python
def func(points):
match points:
case []:
print("No points")
case [Point(0, 0)]:
print("The origin")
case [Point(x, y)]:
print(f"Single point {x}, {y}")
case [Point(0, y1), Point(0, y2)]:
print(f"Two on the Y axis at {y1}, {y2}")
case _:
print("Something else")
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["print(#quot;Something else#quot;)\n"]
block2["match points:
case []:
print(#quot;No points#quot;)
case [Point(0, 0)]:
print(#quot;The origin#quot;)
case [Point(x, y)]:
print(f#quot;Single point {x}, {y}#quot;)
case [Point(0, y1), Point(0, y2)]:
print(f#quot;Two on the Y axis at {y1}, {y2}#quot;)
case _:
print(#quot;Something else#quot;)\n"]
block3["print(f#quot;Two on the Y axis at {y1}, {y2}#quot;)\n"]
block4["match points:
case []:
print(#quot;No points#quot;)
case [Point(0, 0)]:
print(#quot;The origin#quot;)
case [Point(x, y)]:
print(f#quot;Single point {x}, {y}#quot;)
case [Point(0, y1), Point(0, y2)]:
print(f#quot;Two on the Y axis at {y1}, {y2}#quot;)
case _:
print(#quot;Something else#quot;)\n"]
block5["print(f#quot;Single point {x}, {y}#quot;)\n"]
block6["match points:
case []:
print(#quot;No points#quot;)
case [Point(0, 0)]:
print(#quot;The origin#quot;)
case [Point(x, y)]:
print(f#quot;Single point {x}, {y}#quot;)
case [Point(0, y1), Point(0, y2)]:
print(f#quot;Two on the Y axis at {y1}, {y2}#quot;)
case _:
print(#quot;Something else#quot;)\n"]
block7["print(#quot;The origin#quot;)\n"]
block8["match points:
case []:
print(#quot;No points#quot;)
case [Point(0, 0)]:
print(#quot;The origin#quot;)
case [Point(x, y)]:
print(f#quot;Single point {x}, {y}#quot;)
case [Point(0, y1), Point(0, y2)]:
print(f#quot;Two on the Y axis at {y1}, {y2}#quot;)
case _:
print(#quot;Something else#quot;)\n"]
block9["print(#quot;No points#quot;)\n"]
block10["match points:
case []:
print(#quot;No points#quot;)
case [Point(0, 0)]:
print(#quot;The origin#quot;)
case [Point(x, y)]:
print(f#quot;Single point {x}, {y}#quot;)
case [Point(0, y1), Point(0, y2)]:
print(f#quot;Two on the Y axis at {y1}, {y2}#quot;)
case _:
print(#quot;Something else#quot;)\n"]
start --> block10
block10 -- "case []" --> block9
block10 -- "else" --> block8
block9 --> block0
block8 -- "case [Point(0, 0)]" --> block7
block8 -- "else" --> block6
block7 --> block0
block6 -- "case [Point(x, y)]" --> block5
block6 -- "else" --> block4
block5 --> block0
block4 -- "case [Point(0, y1), Point(0, y2)]" --> block3
block4 -- "else" --> block2
block3 --> block0
block2 --> block1
block1 --> block0
block0 --> return
```
## Function 12
### Source
```python
def func(point):
match point:
case Point(x, y) if x == y:
print(f"Y=X at {x}")
case Point(x, y):
print(f"Not on the diagonal")
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["print(f#quot;Not on the diagonal#quot;)\n"]
block2["match point:
case Point(x, y) if x == y:
print(f#quot;Y=X at {x}#quot;)
case Point(x, y):
print(f#quot;Not on the diagonal#quot;)\n"]
block3["print(f#quot;Y=X at {x}#quot;)\n"]
block4["match point:
case Point(x, y) if x == y:
print(f#quot;Y=X at {x}#quot;)
case Point(x, y):
print(f#quot;Not on the diagonal#quot;)\n"]
start --> block4
block4 -- "case Point(x, y) if x == y" --> block3
block4 -- "else" --> block2
block3 --> block0
block2 -- "case Point(x, y)" --> block1
block2 -- "else" --> block0
block1 --> block0
block0 --> return
```
## Function 13
### Source
```python
def func():
from enum import Enum
class Color(Enum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
match color:
case Color.RED:
print("I see red!")
case Color.GREEN:
print("Grass is green")
case Color.BLUE:
print("I'm feeling the blues :(")
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["print(#quot;I'm feeling the blues :(#quot;)\n"]
block2["match color:
case Color.RED:
print(#quot;I see red!#quot;)
case Color.GREEN:
print(#quot;Grass is green#quot;)
case Color.BLUE:
print(#quot;I'm feeling the blues :(#quot;)\n"]
block3["print(#quot;Grass is green#quot;)\n"]
block4["match color:
case Color.RED:
print(#quot;I see red!#quot;)
case Color.GREEN:
print(#quot;Grass is green#quot;)
case Color.BLUE:
print(#quot;I'm feeling the blues :(#quot;)\n"]
block5["print(#quot;I see red!#quot;)\n"]
block6["match color:
case Color.RED:
print(#quot;I see red!#quot;)
case Color.GREEN:
print(#quot;Grass is green#quot;)
case Color.BLUE:
print(#quot;I'm feeling the blues :(#quot;)\n"]
block7["from enum import Enum\nclass Color(Enum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'\ncolor = Color(input(#quot;Enter your choice of 'red', 'blue' or 'green': #quot;))\n"]
start --> block7
block7 --> block6
block6 -- "case Color.RED" --> block5
block6 -- "else" --> block4
block5 --> block0
block4 -- "case Color.GREEN" --> block3
block4 -- "else" --> block2
block3 --> block0
block2 -- "case Color.BLUE" --> block1
block2 -- "else" --> block0
block1 --> block0
block0 --> return
```

View file

@ -0,0 +1,41 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
raise Exception
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["raise Exception\n"]
start --> block0
block0 --> return
```
## Function 1
### Source
```python
def func():
raise "a glass!"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["raise #quot;a glass!#quot;\n"]
start --> block0
block0 --> return
```

View file

@ -0,0 +1,136 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
pass
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["pass\n"]
start --> block0
block0 --> return
```
## Function 1
### Source
```python
def func():
pass
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["pass\n"]
start --> block0
block0 --> return
```
## Function 2
### Source
```python
def func():
return
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return\n"]
start --> block0
block0 --> return
```
## Function 3
### Source
```python
def func():
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
start --> block0
block0 --> return
```
## Function 4
### Source
```python
def func():
return 1
return "unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable#quot;\n"]
block1["return 1\n"]
start --> block1
block1 --> return
block0 --> return
```
## Function 5
### Source
```python
def func():
i = 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["i = 0\n"]
start --> block0
block0 --> return
```
## Function 6
### Source
```python
def func():
i = 0
i += 2
return i
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["i = 0\ni += 2\nreturn i\n"]
start --> block0
block0 --> return
```

View file

@ -0,0 +1,527 @@
---
source: crates/ruff/src/rules/ruff/rules/unreachable.rs
description: "This is a Mermaid graph. You can use https://mermaid.live to visualize it as a diagram."
---
## Function 0
### Source
```python
def func():
while False:
return "unreachable"
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["return #quot;unreachable#quot;\n"]
block2["while False:
return #quot;unreachable#quot;\n"]
start --> block2
block2 -- "False" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 1
### Source
```python
def func():
while False:
return "unreachable"
else:
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable#quot;\n"]
block1["return 1\n"]
block2["while False:
return #quot;unreachable#quot;
else:
return 1\n"]
start --> block2
block2 -- "False" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 2
### Source
```python
def func():
while False:
return "unreachable"
else:
return 1
return "also unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;also unreachable#quot;\n"]
block1["return #quot;unreachable#quot;\n"]
block2["return 1\n"]
block3["while False:
return #quot;unreachable#quot;
else:
return 1\n"]
start --> block3
block3 -- "False" --> block1
block3 -- "else" --> block2
block2 --> return
block1 --> return
block0 --> return
```
## Function 3
### Source
```python
def func():
while True:
return 1
return "unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;unreachable#quot;\n"]
block1["return 1\n"]
block2["while True:
return 1\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> return
block0 --> return
```
## Function 4
### Source
```python
def func():
while True:
return 1
else:
return "unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["return #quot;unreachable#quot;\n"]
block2["while True:
return 1
else:
return #quot;unreachable#quot;\n"]
start --> block2
block2 -- "True" --> block0
block2 -- "else" --> block1
block1 --> return
block0 --> return
```
## Function 5
### Source
```python
def func():
while True:
return 1
else:
return "unreachable"
return "also unreachable"
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return #quot;also unreachable#quot;\n"]
block1["return 1\n"]
block2["return #quot;unreachable#quot;\n"]
block3["while True:
return 1
else:
return #quot;unreachable#quot;\n"]
start --> block3
block3 -- "True" --> block1
block3 -- "else" --> block2
block2 --> return
block1 --> return
block0 --> return
```
## Function 6
### Source
```python
def func():
i = 0
while False:
i += 1
return i
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return i\n"]
block1["i += 1\n"]
block2["i = 0\nwhile False:
i += 1\n"]
start --> block2
block2 -- "False" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 7
### Source
```python
def func():
i = 0
while True:
i += 1
return i
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return i\n"]
block1["i += 1\n"]
block2["i = 0\nwhile True:
i += 1\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 8
### Source
```python
def func():
while True:
pass
return 1
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 1\n"]
block1["pass\n"]
block2["while True:
pass\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 9
### Source
```python
def func():
i = 0
while True:
if True:
print("ok")
i += 1
return i
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return i\n"]
block1["i += 1\n"]
block2["print(#quot;ok#quot;)\n"]
block3["if True:
print(#quot;ok#quot;)\n"]
block4["i = 0\nwhile True:
if True:
print(#quot;ok#quot;)
i += 1\n"]
start --> block4
block4 -- "True" --> block3
block4 -- "else" --> block0
block3 -- "True" --> block2
block3 -- "else" --> block1
block2 --> block1
block1 --> block4
block0 --> return
```
## Function 10
### Source
```python
def func():
i = 0
while True:
if False:
print("ok")
i += 1
return i
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return i\n"]
block1["i += 1\n"]
block2["print(#quot;ok#quot;)\n"]
block3["if False:
print(#quot;ok#quot;)\n"]
block4["i = 0\nwhile True:
if False:
print(#quot;ok#quot;)
i += 1\n"]
start --> block4
block4 -- "True" --> block3
block4 -- "else" --> block0
block3 -- "False" --> block2
block3 -- "else" --> block1
block2 --> block1
block1 --> block4
block0 --> return
```
## Function 11
### Source
```python
def func():
while True:
if True:
return 1
return 0
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0["return 0\n"]
block1["return 1\n"]
block2["if True:
return 1\n"]
block3["while True:
if True:
return 1\n"]
start --> block3
block3 -- "True" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> return
block0 --> return
```
## Function 12
### Source
```python
def func():
while True:
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["while True:
continue\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 13
### Source
```python
def func():
while False:
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["while False:
continue\n"]
start --> block2
block2 -- "False" --> block1
block2 -- "else" --> block0
block1 --> block2
block0 --> return
```
## Function 14
### Source
```python
def func():
while True:
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["while True:
break\n"]
start --> block2
block2 -- "True" --> block1
block2 -- "else" --> block0
block1 --> block0
block0 --> return
```
## Function 15
### Source
```python
def func():
while False:
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["while False:
break\n"]
start --> block2
block2 -- "False" --> block1
block2 -- "else" --> block0
block1 --> block0
block0 --> return
```
## Function 16
### Source
```python
def func():
while True:
if True:
continue
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["continue\n"]
block2["if True:
continue\n"]
block3["while True:
if True:
continue\n"]
start --> block3
block3 -- "True" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> block3
block0 --> return
```
## Function 17
### Source
```python
def func():
while True:
if True:
break
```
### Control Flow Graph
```mermaid
flowchart TD
start(("Start"))
return(("End"))
block0[["`*(empty)*`"]]
block1["break\n"]
block2["if True:
break\n"]
block3["while True:
if True:
break\n"]
start --> block3
block3 -- "True" --> block2
block3 -- "else" --> block0
block2 -- "True" --> block1
block2 -- "else" --> block3
block1 --> block0
block0 --> return
```

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,249 @@
---
source: crates/ruff/src/rules/ruff/mod.rs
---
RUF014.py:3:5: RUF014 Unreachable code in after_return
|
1 | def after_return():
2 | return "reachable"
3 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
4 |
5 | async def also_works_on_async_functions():
|
RUF014.py:7:5: RUF014 Unreachable code in also_works_on_async_functions
|
5 | async def also_works_on_async_functions():
6 | return "reachable"
7 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
8 |
9 | def if_always_true():
|
RUF014.py:12:5: RUF014 Unreachable code in if_always_true
|
10 | if True:
11 | return "reachable"
12 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
13 |
14 | def if_always_false():
|
RUF014.py:16:9: RUF014 Unreachable code in if_always_false
|
14 | def if_always_false():
15 | if False:
16 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
17 | return "reachable"
|
RUF014.py:21:9: RUF014 Unreachable code in if_elif_always_false
|
19 | def if_elif_always_false():
20 | if False:
21 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
22 | elif False:
23 | return "also unreachable"
|
RUF014.py:23:9: RUF014 Unreachable code in if_elif_always_false
|
21 | return "unreachable"
22 | elif False:
23 | return "also unreachable"
| ^^^^^^^^^^^^^^^^^^^^^^^^^ RUF014
24 | return "reachable"
|
RUF014.py:28:9: RUF014 Unreachable code in if_elif_always_true
|
26 | def if_elif_always_true():
27 | if False:
28 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
29 | elif True:
30 | return "reachable"
|
RUF014.py:31:5: RUF014 Unreachable code in if_elif_always_true
|
29 | elif True:
30 | return "reachable"
31 | return "also unreachable"
| ^^^^^^^^^^^^^^^^^^^^^^^^^ RUF014
32 |
33 | def ends_with_if():
|
RUF014.py:35:9: RUF014 Unreachable code in ends_with_if
|
33 | def ends_with_if():
34 | if False:
35 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
36 | else:
37 | return "reachable"
|
RUF014.py:42:5: RUF014 Unreachable code in infinite_loop
|
40 | while True:
41 | continue
42 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
43 |
44 | ''' TODO: we could determine these, but we don't yet.
|
RUF014.py:75:5: RUF014 Unreachable code in match_wildcard
|
73 | case _:
74 | return "reachable"
75 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
76 |
77 | def match_case_and_wildcard(status):
|
RUF014.py:83:5: RUF014 Unreachable code in match_case_and_wildcard
|
81 | case _:
82 | return "reachable"
83 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
84 |
85 | def raise_exception():
|
RUF014.py:87:5: RUF014 Unreachable code in raise_exception
|
85 | def raise_exception():
86 | raise Exception
87 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
88 |
89 | def while_false():
|
RUF014.py:91:9: RUF014 Unreachable code in while_false
|
89 | def while_false():
90 | while False:
91 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
92 | return "reachable"
|
RUF014.py:96:9: RUF014 Unreachable code in while_false_else
|
94 | def while_false_else():
95 | while False:
96 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
97 | else:
98 | return "reachable"
|
RUF014.py:102:9: RUF014 Unreachable code in while_false_else_return
|
100 | def while_false_else_return():
101 | while False:
102 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
103 | else:
104 | return "reachable"
|
RUF014.py:105:5: RUF014 Unreachable code in while_false_else_return
|
103 | else:
104 | return "reachable"
105 | return "also unreachable"
| ^^^^^^^^^^^^^^^^^^^^^^^^^ RUF014
106 |
107 | def while_true():
|
RUF014.py:110:5: RUF014 Unreachable code in while_true
|
108 | while True:
109 | return "reachable"
110 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
111 |
112 | def while_true_else():
|
RUF014.py:116:9: RUF014 Unreachable code in while_true_else
|
114 | return "reachable"
115 | else:
116 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
117 |
118 | def while_true_else_return():
|
RUF014.py:122:9: RUF014 Unreachable code in while_true_else_return
|
120 | return "reachable"
121 | else:
122 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
123 | return "also unreachable"
|
RUF014.py:123:5: RUF014 Unreachable code in while_true_else_return
|
121 | else:
122 | return "unreachable"
123 | return "also unreachable"
| ^^^^^^^^^^^^^^^^^^^^^^^^^ RUF014
124 |
125 | def while_false_var_i():
|
RUF014.py:128:9: RUF014 Unreachable code in while_false_var_i
|
126 | i = 0
127 | while False:
128 | i += 1
| ^^^^^^ RUF014
129 | return i
|
RUF014.py:135:5: RUF014 Unreachable code in while_true_var_i
|
133 | while True:
134 | i += 1
135 | return i
| ^^^^^^^^ RUF014
136 |
137 | def while_infinite():
|
RUF014.py:140:5: RUF014 Unreachable code in while_infinite
|
138 | while True:
139 | pass
140 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
141 |
142 | def while_if_true():
|
RUF014.py:146:5: RUF014 Unreachable code in while_if_true
|
144 | if True:
145 | return "reachable"
146 | return "unreachable"
| ^^^^^^^^^^^^^^^^^^^^ RUF014
147 |
148 | # Test case found in the Bokeh repository that trigger a false positive.
|

View file

@ -61,7 +61,7 @@ mod tests {
use super::{main, Args};
#[test]
#[cfg_attr(not(feature = "unreachable-code"), test)]
fn test_generate_json_schema() -> Result<()> {
let mode = if env::var("RUFF_UPDATE_SCHEMA").as_deref() == Ok("1") {
Mode::Write

View file

@ -40,6 +40,11 @@ impl<I: Idx, T> IndexSlice<I, T> {
}
}
#[inline]
pub const fn first(&self) -> Option<&T> {
self.raw.first()
}
#[inline]
pub const fn len(&self) -> usize {
self.raw.len()
@ -63,6 +68,13 @@ impl<I: Idx, T> IndexSlice<I, T> {
(0..self.len()).map(|n| I::new(n))
}
#[inline]
pub fn iter_enumerated(
&self,
) -> impl DoubleEndedIterator<Item = (I, &T)> + ExactSizeIterator + '_ {
self.raw.iter().enumerate().map(|(n, t)| (I::new(n), t))
}
#[inline]
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
self.raw.iter_mut()

View file

@ -36,10 +36,11 @@ pub(super) fn generate_newtype_index(item: ItemStruct) -> syn::Result<proc_macro
#vis #struct_token #ident(std::num::NonZeroU32)#semi_token
impl #ident {
const MAX: u32 = u32::MAX - 1;
const MAX_VALUE: u32 = u32::MAX - 1;
const MAX: Self = Self::from_u32(Self::MAX_VALUE);
#vis const fn from_usize(value: usize) -> Self {
assert!(value <= Self::MAX as usize);
assert!(value <= Self::MAX_VALUE as usize);
// SAFETY:
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
@ -49,7 +50,7 @@ pub(super) fn generate_newtype_index(item: ItemStruct) -> syn::Result<proc_macro
}
#vis const fn from_u32(value: u32) -> Self {
assert!(value <= Self::MAX);
assert!(value <= Self::MAX_VALUE);
// SAFETY:
// * The `value < u32::MAX` guarantees that the add doesn't overflow.