|
15 | 15 | import cpp |
16 | 16 | import codingstandards.c.misra |
17 | 17 |
|
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 |
19 | 23 | where |
20 | 24 | not isExcluded(func, Statements5Package::functionReturnConditionQuery()) and |
21 | 25 | func.hasDefinition() and |
| 26 | + // Ignore functions which have multiple bodies |
| 27 | + count(func.getBlock()) = 1 and |
| 28 | + // Ignore functions which are compiler generated |
22 | 29 | not func.isCompilerGenerated() and |
| 30 | + // Report all the return statements in the function |
| 31 | + returnStmt.getEnclosingFunction() = func and |
23 | 32 | ( |
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 $@." |
26 | 36 | 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." |
29 | 42 | ) |
30 | | -select func, message |
| 43 | +select func, message, returnStmt, "return statement" |
0 commit comments