Skip to content

Commit e7e0c6b

Browse files
committed
Rust: Add qhelp + examples for unreachable code query.
1 parent 35378aa commit e7e0c6b

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>This rule finds code that is never reached. Unused code should be removed to increase readability and avoid confusion.</p>
8+
</overview>
9+
10+
<recommendation>
11+
<p>Remove any unreachable code.</p>
12+
</recommendation>
13+
14+
<example>
15+
<p>In the following example, the final <code>return</code> statement can never be reached:</p>
16+
<sample src="UnreachableCodeBad.rs" />
17+
<p>The problem can be fixed simply by removing the unreachable code:</p>
18+
<sample src="UnreachableCodeGood.rs" />
19+
</example>
20+
21+
<references>
22+
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/Unreachable_code">Unreachable code</a></li>
23+
</references>
24+
</qhelp>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn fib(input: u32) -> u32 {
2+
if (input == 0) {
3+
return 0;
4+
} else if (input == 1) {
5+
return 1;
6+
} else {
7+
return fib(input - 1) + fib(input - 2);
8+
}
9+
10+
return input; // BAD: this code is never reached
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn fib(input: u32) -> u32 {
2+
if (input == 0) {
3+
return 0;
4+
} else if (input == 1) {
5+
return 1;
6+
} else {
7+
return fib(input - 1) + fib(input - 2);
8+
}
9+
}

0 commit comments

Comments
 (0)