Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

No trivial switch statements

ID: cpp/trivial-switchKind: problemSecurity severity: Severity: recommendationPrecision: highTags:   - maintainability   - readability   - external/jsfQuery suites:   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

The following forms ofswitch statement are consideredtrivial:

  1. No cases at all.

  2. Just a default case.

  3. Just one non-default case.

  4. A default case and one non-default case.

Recommendation

Either theswitch statement should be replaced with a simpler control flow structure, or it should be extended to handle more cases. Each trivial form has a different replacement:

  1. If there are no cases, theswitch statement can be removed.

  2. If there is just one default case, theswitch keyword, thedefault keyword, and the subsequent colon can all be removed.

  3. If there is just one non-default case, theswitch statement can be turned into anif statement.

  4. If there is one default case and one non-default case, theswitch statement can be turned into anif/else statement.

Example

intf(){intval=0;switch(val){//wrong, use an if insteadcase0://...default://...}switch(val){//correct, has 2 cases and a defaultcase0://...case1://...default://...}}

References

  • AV Rule 196,Joint Strike Fighter Air Vehicle C++ Coding Standards. Lockheed Martin Corporation, 2005.


[8]ページ先頭

©2009-2025 Movatter.jp