JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


LANG.TYPE.NCPFO : Non-const Predicate Function Object

要旨

The function call operator of a user-defined predicate function object modifies internal object state.

A predicate function object P will only trigger a warning of this class if:

For the sake of this warning class, a predicate function object is an object of a class that overrides operator() to return bool or unsigned char.

プロパティ

クラス名 Non-const Predicate Function Object
日本語クラス名 Non-const Predicate Function Object
クラス分類 スタイル (style)
ニーモニック LANG.TYPE.NCPFO
カテゴリー
AUTOSARC++14 AUTOSARC++14:A25-1-1 Non-static data members or captured values of predicate function objects that are state related to this object's identity shall not be copied.
MisraC++2023 MisraC++2023:28.3.1 Predicates shall not have persistent side effects
CERT-CPP CERT-CPP:CTR58-CPP Predicate function objects should not be mutable
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Non-const Predicate Function Object"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

#include <vector>
#include <algorithm>

class PfoNotWrapped {
private:
    size_t timesCalled;
public:
    PfoNotWrapped() : timesCalled(0) {}
    bool operator() (const int &) {                                // 'Non-const Predicate Function Object' warning issued here
        ++timesCalled;
        return timesCalled == 3;
    }
};

class PfoWrapped {
private:
    size_t timesCalled;
public:
    PfoWrapped() : timesCalled(0) {}
    bool operator() (const int &) {                                           // ok: all uses are wrapped with std::reference_wrapper
        ++timesCalled;
        return timesCalled == 3;
    }
};

class PfoConst {
  private:
    size_t timesCalled;
  public:
    PfoConst() : timesCalled(0) {}
    bool operator() (const int &) const {                                     // ok: uses are not wrapped, but operator() does not modify object state
        return timesCalled == 3;
    }
};

size_t non_const_pred_func_object(void) {
    std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    size_t timesCalled = 0;

    auto it1 = std::remove_if(v.begin(),
                              v.end(),
                              [timesCalled](const int &) mutable { // 'Non-const Predicate Function Object' warning issued here
                                  ++timesCalled;
                                  return timesCalled == 3;
                              });

    PfoNotWrapped predNW;
    auto it2 = std::remove_if(v.begin(),
                              it1,
                              predNW);            // not in a std::reference_wrapper: causes warning to be issued for PfoNotWrapped::operator(), above

    PfoWrapped predW;
    auto it3 = std::remove_if(v.begin(),
                              it2,
                              std::ref(predW));                               // std::ref() creates a std::reference_wrapper                                              

    PfoConst predC;
    auto it4 = std::remove_if(v.begin(),
                              it3,
			      predC);                                         // predC() does not mutate internal state so does not need to be wrapped

    v.erase(it4, v.end());
    return v.size();
}

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

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.