Skip to content

-Wpointer-bool-conversion/-Waddress misses an obvious case #82512

@r-barnes

Description

@r-barnes

The following pattern is often used to evaluate an expensive function only once, cache its result, and return that on subsequent calls.

int foo() {
    static bool is_true = [](){ return expensive_helper_function; };

    return is_true;
}

But this code is subtly wrong. It should be

static bool is_true = [](){ return expensive_helper_function; }(); // Note the parens!

In the bad case it looks as though the lambda is being evaluated to a pointer(?) and this is being evaluated always to true when it is assigned to is_true.

It feels as though -Wpointer-bool-conversion should flag this, but it does not (godbolt).

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-negativeWarning doesn't fire when it shouldgood first issuehttps://github.com/llvm/llvm-project/contribute

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions