メインコンテンツ

MISRA C++:2008 Rule 4-5-3

Expressions with type (plain) char and wchar_t shall not be used as operands to built-in operators other than the assignment operator =, the equality operators == and !=, and the unary & operator.N

説明

ルール定義

Expressions with type (plain) char and wchar_t shall not be used as operands to built-in operators other than the assignment operator =, the equality operators == and !=, and the unary & operator.N 1

根拠

C++ 規格では、文字 '0' から '9' の値が連続であることを要求しています。他の文字は適切に定義された値をもちません。これらの文字をルールに記載されている演算以外の演算で使用すると、それらの文字の基となる値を暗黙的に使用し、予期しない結果になる可能性があります。

Polyspace 実装

char 型の式と wchar_t 型の式で次の演算子以外の演算子が使用されている場合、Polyspace® はこのルールの違反を報告します。

  • =

  • ==

  • !=

  • 単項の &

トラブルシューティング

ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

void charManipulations (char ch) {
   
    char initChar = 'a'; //Compliant
    char finalChar = 'z'; //Compliant
    
    if(ch == initChar) {} //Compliant 
    if( (ch >= initChar) && (ch <= finalChar)) {} //Noncompliant 
    else if( (ch >= '0') && (ch <= '9') ) {} //Compliant by exception
}

この例では、文字型のオペランドを = および == 演算子で使用した場合はルールに違反しません。文字型のオペランドは、比較が数字 '0' から '9' に対して実行される限り、関係演算子で使用することもできます。

チェック情報

グループ: Standard Conversions
カテゴリ: 必要

バージョン履歴

R2013b で導入


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.