Skip to content

[clang-tidy] Clang-Tidy produces a false positive when returning from a lambda cppcoreguidelines-owning-memory #59389

Closed
@H-G-Hristov

Description

@H-G-Hristov

Clang-Tidy produces a false positive when returning from a lambda:

https://releases.llvm.org/15.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/cppcoreguidelines/owning-memory.html#cppcoreguidelines-owning-memory

returning a newly created resource of type 'void' or 'gsl::owner<>' from a function whose return type is not 'gsl::owner<>'C/C++cppcoreguidelines-owning-memory

Is caused by:

TEST(ExternalLibTest, MicrosoftGslOwner)
{
    struct S
    {
        int value{1};
    };

    const auto MakeS = [] -> ::gsl::owner<S*>
    {
        return ::gsl::owner<S*>{new S{}};
    };

    const ::gsl::owner<S*> sPtr = MakeS();

    EXPECT_EQ(sPtr->value, 1);
    EXPECT_NE(sPtr->value, 2);
}

but this is fine:

namespace {

struct S
{
    int value{1};
};

auto MakeS()
{
    return ::gsl::owner<S*>{new S{}};
}

}  // namespace

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorclang-tidyconfirmedVerified by a second partyfalse-positiveWarning fires when it should not

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions