Skip to content

[clang-tidy] bugprone-unused-return-value false positive for unique_ptr assignment #84314

@zufuliu

Description

@zufuliu

https://godbolt.org/z/a6f19rboW

// clang-tidy --checks=bugprone-unused-return-value test.cpp --
#include <cstdlib>
#include <memory>

extern std::unique_ptr<int> alloc();

struct Foo {
    std::unique_ptr<int> foo;
    std::unique_ptr<int> bar;
    void *baz;
    Foo() {
        foo = std::make_unique<int>();
        bar = alloc();
        baz = malloc(sizeof(int));
    }
};

void f1(std::unique_ptr<int> &foo) {
    foo = std::make_unique<int>();
}

void f2(std::unique_ptr<int> &bar) {
    bar = alloc();
}

void f3(void* &baz) {
    baz = malloc(sizeof(int));
}
--checks=bugprone-unused-return-value
[<source>:12:9: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors [bugprone-unused-return-value]](javascript:;)
   12 |         foo = std::make_unique<int>();
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[<source>:13:9: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors [bugprone-unused-return-value]](javascript:;)
   13 |         bar = alloc();
      |         ^~~~~~~~~~~~~
[<source>:19:5: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors [bugprone-unused-return-value]](javascript:;)
   19 |     foo = std::make_unique<int>();
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[<source>:23:5: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors [bugprone-unused-return-value]](javascript:;)
   23 |     bar = alloc();
      |     ^~~~~~~~~~~~~
4 warnings gener

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions