Skip to content

[clang-tidy] misc-const-correctness false positive #157730

@firewave

Description

@firewave
#include <memory> #include <vector> struct S {}; static void f() { /*const*/ S *const newdata = new S; std::vector<std::unique_ptr<S>> data; data.emplace_back(newdata); }
<source>:7:15: warning: pointee of variable 'newdata' of type 'S *const' can be declared 'const' [misc-const-correctness] 7 | /*const*/ S *const newdata = new S; | ^ | const 

https://godbolt.org/z/Yr888PnPn

The resulting code fails to compile:

-checks=-*,misc-const-correctness /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/new_allocator.h:191:23: error: no matching constructor for initialization of 'std::unique_ptr<S>' [clang-diagnostic-error] 191 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/alloc_traits.h:674:8: note: in instantiation of function template specialization 'std::__new_allocator<std::unique_ptr<S>>::construct<std::unique_ptr<S>, const S *const &>' requested here 674 | __a.construct(__p, std::forward<_Args>(__args)...); | ^ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/vector.tcc:121:21: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::unique_ptr<S>>>::construct<std::unique_ptr<S>, const S *const &>' requested here 121 | _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, | ^ <source>:10:10: note: in instantiation of function template specialization 'std::vector<std::unique_ptr<S>>::emplace_back<const S *const &>' requested here 10 | data.emplace_back(newdata); | ^ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:311:2: note: candidate constructor template not viable: 1st argument ('const S *const') would lose const qualifier 311 | unique_ptr(pointer __p) noexcept | ^ ~~~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:353:12: note: candidate constructor template not viable: no known conversion from 'const S *const' to 'nullptr_t' (aka 'std::nullptr_t') for 1st argument 353 | constexpr unique_ptr(nullptr_t) noexcept | ^ ~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:360:7: note: candidate constructor not viable: no known conversion from 'const S *const' to 'unique_ptr<S>' for 1st argument 360 | unique_ptr(unique_ptr&&) = default; | ^ ~~~~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:525:7: note: candidate constructor not viable: no known conversion from 'const S *const' to 'const unique_ptr<S>' for 1st argument 525 | unique_ptr(const unique_ptr&) = delete; | ^ ~~~~~~~~~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:374:2: note: candidate template ignored: could not match 'unique_ptr<_Up, _Ep>' against 'const S *const' 374 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept | ^ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:385:2: note: candidate template ignored: could not match 'auto_ptr<_Up>' against 'const S *const' 385 | unique_ptr(auto_ptr<_Up>&& __u) noexcept; | ^ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:298:12: note: candidate constructor template not viable: requires 0 arguments, but 1 was provided 298 | constexpr unique_ptr() noexcept | ^ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:325:2: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided 325 | unique_ptr(pointer __p, const deleter_type& __d) noexcept | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:338:2: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided 338 | unique_ptr(pointer __p, | ^ ~~~~~~~~~~~~ 339 | __enable_if_t<!is_lvalue_reference<_Del>::value, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 340 | _Del&&> __d) noexcept | ~~~~~~~~~~~ /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/unique_ptr.h:347:2: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided 347 | unique_ptr(pointer, | ^ ~~~~~~~~ 348 | __enable_if_t<is_lvalue_reference<_Del>::value, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 349 | _DelUnref&&>) = delete; | ~~~~~~~~~~~~ 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions