github / codeql-coding-standards

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

`A4-7-1`: integer data loss false positive erasing from container #793

Open fjatWbyT opened 1 week ago

fjatWbyT commented 1 week ago

Affected rules

Description

Decrement cannot lead to wrap-around since the container is checked for emptiness.

Example

class container
{
    bool empty()
    {
        return num_items_ == 0;
    }

    void erase()
    {
        if (empty())
            return;

        num_items_--;
    }

    std::size_t num_items_ = 0;
};