unique_ptr constructed from a raw new
ID |
c.maintainability.make_unique |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Modernization |
Language |
C / C++ |
Description
Building a std::unique_ptr from a raw new is exception-unsafe in some call contexts and states the type twice. Use std::make_unique<T>(args…), which owns the allocation from the start and reads more clearly.
Rationale
Building a std::unique_ptr from a raw new is exception-unsafe in some call contexts and states the type twice. Use std::make_unique<T>(args…), which owns the allocation from the start and reads more clearly.
The following code illustrates the pattern detected by this rule:
void f() {
// FLAGGED: unique_ptr constructed from a raw new
std::unique_ptr<int> a(new int(5));
// FLAGGED: unique_ptr constructed from a raw new
auto b = std::unique_ptr<Foo>(new Foo(1, 2));