メインコンテンツ

MISRA C:2012 Rule 16.2

A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement

説明

ルール定義

A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement 1

根拠

C 標準では、switch ラベル (たとえば、case または default) を switch ステートメント本体内の任意のステートメントの前に置くことを許可しています。この柔軟性が原因で、構造化されていないコードになることがあります。構造化されていないコードを回避するには、switch ラベルを必ず switch ステートメント本体の一番外側のレベルにのみ置くようにします。

レポート内の追加のメッセージ

レポート ファイルのすべてのメッセージは「MISRA-C switch statements syntax normative restriction (MISRA-C switch ステートメント構文標準規格)」から開始します。

  • Initializers shall not be used in switch clauses.

  • The child statement of a switch shall be a compound statement.

  • All switch clauses shall appear at the same level.

  • A switch clause shall only contain switch labels and switch clauses, and no other code.

  • A switch statement shall only contain switch labels and switch clauses, and no other code.

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

チェック情報

グループ: switch ステートメント
カテゴリ: 必要
AGC カテゴリ: 推奨

1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.