Warning C26411

The parameter 'parameter' is a reference to unique pointer and it is never reassigned or reset, use T* or T& instead (r.33)

When you pass a unique pointer to a function by reference, it implies that its resource may be released or transferred inside the function. If the function uses its parameter only to access the resource, it's safe to pass a raw pointer or a reference. For more information, see C++ Core Guidelines rule R.33: Take a unique_ptr<widget>& parameter to express that a function reseats the widget.


  • The limitations from the warning C26410 are also applicable here.

  • The heuristic to detect release or reset access to the unique pointer is naive. We only detect calls to assignment operators and to functions named reset (case-insensitive). Obviously, this detection doesn't cover all possible cases of smart pointer modifications. (For example, it doesn't detect std::swap, or any special non-const function in a custom smart pointer). We expect this warning may produce many false positives on custom types, and in some scenarios dealing with standard unique pointers. We expect to improve the heuristic as we implement more checks focused on smart pointers.

  • The fact that smart pointers are often templates brings an interesting limitation. The compiler isn't required to process template code in templates if it's not used. In code that makes limited use of smart pointer interfaces, the checker may produce unexpected results. The checker can't properly identify semantics of the template type, because some functions may never get used. For the standard std::unique_ptr, this limitation is mitigated by recognizing the type's name. This analysis may be extended in the future to cover more well-known smart pointers.

  • Lambda expressions that do implicit capture-by-reference may lead to surprising warnings about references to unique pointers. Currently, all captured reference parameters in lambdas are reported, regardless of whether they're reset or not. A future release may extend the heuristic to correlate lambda fields and lambda parameters.

Code analysis name: NO_REF_TO_UNIQUE_PTR

Example: Unnecessary reference

void TraceValid(std::unique_ptr<Slot> &slot)    // C26411
    if (!IsDamaged(slot.get()))
        std::cout << *slot.get();

void ReleaseValid(std::unique_ptr<Slot> &slot)  // OK
    if (!IsDamaged(slot.get()))