C and C++ Binaries


LANG.STRUCT.RC : 冗長な条件

要旨

条件式は、常に真か常に偽のどちらかとなります。

This includes conditions that are introduced by normalization rather than explicit in the code. Warnings for these cases also indicate a degree of redundancy in the analyzed code: see below for some examples.

プロパティ

クラス名 Redundant Condition
日本語クラス名 冗長な条件
クラス分類 冗長性 (redundancy)
ニーモニック LANG.STRUCT.RC
カテゴリー
MisraC2023 MisraC2023:14.3 Controlling expressions shall not be invariant
Misra2012 Misra2012:14.3 Controlling expressions shall not be invariant
Misra2004 Misra2004:13.7 Boolean operations whose results are invariant shall not be permitted
MisraC++2023 MisraC++2023:0.0.1 A function shall not contain unreachable statements
  MisraC++2023:0.0.2 Controlling expressions should not be invariant
CWE CWE:482 Comparing instead of Assigning
  CWE:570 Expression is Always False
  CWE:571 Expression is Always True
  CWE:1164 Irrelevant Code
CERT-C CERT-C:ERR30-C Take care when reading errno
  CERT-C:MSC07-C Detect and remove dead code
  CERT-C:MSC12-C Detect and remove code that has no effect or is never executed
CERT-CPP CERT-CPP:OOP54-CPP Gracefully handle self-copy assignment
JSF++ JSF++:81 The assignment operator shall handle self-assignment correctly.
  JSF++:187 All non-null statements shall potentially have a side-effect.
OWASP-2017 OWASP-2017:A3 Sensitive data exposure
OWASP-2021 OWASP-2021:A2 Cryptographic failures
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Redundant Condition"

int lang_struct_rc_1(int i){
    if (i > 0){                                // ok: i>0 can be either true or false
        if (i+1 > 1){           // 'Redundant Condition' warning issued here: 
                                //  if execution reaches this point, i+1>0 is true
      return i;
    }
  }
  return 0;
}

enum color { BLUE, RED, YELLOW };

bool process_bools(bool x, bool y){
  return x && y;
}

bool lang_struct_rc_2(bool doNegate, bool andArg){
    color colA = BLUE;
    color colB = RED;
    if (doNegate){                             // ok: doNegate can be either true or false
        return (bool)colA;              // 'Redundant Condition' warning issued here: 
                                        //     return (bool)colA; 
                                        // is normalized to the form
                                        //     if (colA != 0)
                                        //         TMP=true;
                                        //     else
                                        //         TMP=false;
                                        //     return TMP;
                                        // and (colA != 0) is always true
                                        // 
                                        // While the IF statement cited in the resulting warning report is not explicitly present
                                        // in the code, the warning does signal the presence of redundancy:
                                        //     (bool)colA
                                        // is functionally equivalent to 
                                        //     false
                                        // so neither the cast operation nor the colA variable is actually required.
    }
    return process_bools(colB, andArg); // 'Redundant Condition' warning issued here: 
                                        // negate(colB) coerces colB to bool, and this coercion is similarly
                                        // normalized to an IF expression whose condition is (colB != 0)
                                        // 
                                        // While the IF statement cited in the resulting warning report is not explicitly present
                                        // in the code, the warning does signal the presence of redundancy:
                                        //     process_bools(colB, andArg)
                                        // is functionally equivalent to 
                                        //     process_bools(true, andArg)
                                        // so the colB variable isn't required either.
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。