경고 C26431

'expr' 식의 형식이 이미 gsl::not_null있습니다. nullness(f.23)에 대해 테스트하지 마세요.

C++ 핵심 지침: F.23: not_null<T> 를 사용하여 "null"이 유효한 값이 아님을 나타냅니다.

지침 지원 라이브러리의 표식 형식 gsl::not_null 은 null 포인터가 아닌 값을 명확하게 나타내는 데 사용됩니다. 런타임에 가정이 유지되지 않으면 하드 오류가 발생합니다. 따라서 식이 형식gsl::not_null의 결과로 계산되는 경우 null에 대해 검사 필요가 없습니다.

설명

gsl::not_null 자체는 씬 포인터 래퍼 클래스이므로 규칙은 오버로드된 변환 연산자(포함된 포인터 개체를 반환)에 대한 호출의 결과를 포함하는 임시 변수를 실제로 추적합니다. 이러한 논리를 사용하면 변수를 포함하고 결국 형식의 결과를 갖는 식에 이 규칙을 적용할 gsl::not_null 수 있습니다. 그러나 현재는 반환 gsl::not_null되는 함수 호출이 포함된 식을 건너뜁니다.

null 검사 대한 현재 추론은 다음 컨텍스트를 검색합니다.

  • 분기 조건의 기호 식입니다(예: if (p) { ... }).
  • 비트가 아닌 논리 연산;
  • 하나의 피연산자는 0으로 계산되는 상수 식인 비교 연산입니다.

코드 분석 이름: DONT_TEST_NOTNULL

예시

불필요한 null 검사 의심스러운 논리를 표시합니다.

class type {
public:
    template<class T> bool is() const;
    template<class T> gsl::not_null<const T*> as() const;
    //...
};

class alias_type : public type {
public:
    gsl::not_null<const type*> get_underlying_type() const;
    gsl::not_null<const type*> get_root_type() const
    {
        const auto ut = get_underlying_type();
        if (ut)                                     // C26431
        {
            const auto uat = ut->as<alias_type>();
            if (uat)                                // C26431, also incorrect use of API!
                return uat->get_root_type();

            return ut;
        }

        return this;                                // Alias to nothing? Actually, dead code!
    }
    //...
};

불필요한 null 검사 의심스러운 논리를 표시하고 다시 작업합니다.

    //...
    gsl::not_null<const type*> get_root_type() const
    {
        const auto ut = get_underlying_type();
        if (ut->is<alias_type>())
            return ut->as<alias_type>()->get_root_type();

        return ut;
    }
    //...