github / codeql-coding-standards

This repository contains CodeQL queries and libraries which support various Coding Standards.
MIT License
121 stars 55 forks source link

`A20-8-5`-`A20-8-6`: Cannot create a smart pointer with `nullptr` value #721

Open nbusser-sr opened 2 days ago

nbusser-sr commented 2 days ago

Affected rules

Rule A20-8-5 (required, implementation, automated) std::make_unique shall be used to construct objects owned by std::unique_ptr.

Rule A20-8-6 (required, implementation, automated) std::make_shared shall be used to construct objects owned by std::shared_ptr.

Description

A20-8-5 triggers when initializing an unique_ptr to nullptr.
A20-8-6 triggers when initializing an shared_ptr to nullptr.

Initializing a smart pointer with a nullptr value can be however necessary in some specific cases.

Example

std::unique_ptr<bool> uniquePtr{nullptr}; // Triggers A20-8-5
std::shared_ptr<bool> sharedPtr{nullptr}; // Triggers A20-8-6