Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 6-4-5 Is return as last statement of a switch-clause also compliant to this rule?
#3
We use PC-lint / FlexeLint and in future PC-lint Plus / FlexeLint Plus for static code analysis and additionally MISRA C++:2008 compliance checking.

We have nearly all MISRA C++ rules active on checking, but not rule 6-6-5 as this rule is definitely counterproductive regarding quality of code. In most cases it is much better to exit a function using return once detecting that it can further process then build a large if tree, especially on functions of return type void.

So in our thousands of C++ source code files there are thousands of multiple returns within a function and there are several hundreds within a switch statement, too.

Rule 6-4-5 and of course also rule 6-4-3 are a problem on compliance checking with rule 6-6-5 being not applied if the usage of a return is not compliant (acceptable), just usage of break or throw as last statement of a switch-clause. It is important for us that switch statements are checked for being well-formed and for an unintentional fall-through from one switch-clause to next switch-clause.

We discussed this issue already with support of Gimpel (producing PC-lint / FlexeLint). One suggestion was inserting below line with return one more line with break. But the break is unreachable and therefore results in a message because of rule 0-1-1. The break after return is also bad for code coverage as this unreachable statement results in never getting 100% code coverage by the coverage tests.

Another suggestion was to split the compliance checking for the rules 6-4-3 and 6-4-5 up to 2 error numbers per rule. One error number checks for missing break and missing throw at end of a switch-clause, but does not output an error message if a return exists as last statement, and the other error number is output if a switch-clause ends with a return. This would make it possible to suppress the error for switch-clause ending with return, but have the other error number for also missing break / throw still enabled.

But best would be for all of us not using rule 6-6-5 if a return as last statement of a switch-clause is also compliant for rule 6-4-3 and 6-4-5.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)