Skip to content

Commit aaa1044

Browse files
committed
Rule 15.5: Improvements
* Exclude compiler generated return statements * Exclude functions with more than one block * Improve alert message * Only report that the last statement is not a return, if there's at least one return statement in the block.
1 parent 71f6d17 commit aaa1044

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

c/misra/src/rules/RULE-15-5/FunctionReturnCondition.ql

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,29 @@
1515
import cpp
1616
import codingstandards.c.misra
1717

18-
from Function func, string message
18+
class UserWrittenReturnStmt extends ReturnStmt {
19+
UserWrittenReturnStmt() { not this.isCompilerGenerated() }
20+
}
21+
22+
from Function func, string message, UserWrittenReturnStmt returnStmt
1923
where
2024
not isExcluded(func, Statements5Package::functionReturnConditionQuery()) and
2125
func.hasDefinition() and
26+
// Ignore functions which have multiple bodies
27+
count(func.getBlock()) = 1 and
28+
// Ignore functions which are compiler generated
2229
not func.isCompilerGenerated() and
30+
// Report all the return statements in the function
31+
returnStmt.getEnclosingFunction() = func and
2332
(
24-
count(ReturnStmt return | return.getEnclosingFunction() = func) > 1 and
25-
message = "Function has more than on return statement."
33+
// There is more than one return statement
34+
count(UserWrittenReturnStmt return | return.getEnclosingFunction() = func) > 1 and
35+
message = "Function has more than one $@."
2636
or
27-
not func.getBlock().getLastStmt() instanceof ReturnStmt and
28-
message = "The last statement of the function is not a return statement."
37+
// There is exactly one return statement
38+
count(UserWrittenReturnStmt return | return.getEnclosingFunction() = func) = 1 and
39+
// But it is not the last statement in the function
40+
not func.getBlock().getLastStmt() instanceof UserWrittenReturnStmt and
41+
message = "The $@ is not the last statement of the function."
2942
)
30-
select func, message
43+
select func, message, returnStmt, "return statement"
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
| test.c:1:6:1:7 | f1 | Function has more than on return statement. |
2-
| test.c:14:6:14:7 | f3 | The last statement of the function is not a return statement. |
3-
| test.c:21:6:21:7 | f4 | Function has more than on return statement. |
4-
| test.c:21:6:21:7 | f4 | The last statement of the function is not a return statement. |
1+
| test.c:1:6:1:7 | f1 | Function has more than one $@. | test.c:3:5:3:11 | return ... | return statement |
2+
| test.c:1:6:1:7 | f1 | Function has more than one $@. | test.c:5:3:5:9 | return ... | return statement |
3+
| test.c:14:6:14:7 | f3 | The $@ is not the last statement of the function. | test.c:17:3:17:9 | return ... | return statement |
4+
| test.c:21:6:21:7 | f4 | Function has more than one $@. | test.c:23:5:23:11 | return ... | return statement |
5+
| test.c:21:6:21:7 | f4 | Function has more than one $@. | test.c:25:3:25:9 | return ... | return statement |

0 commit comments

Comments
 (0)