在下面发布的这个最小示例上运行 clang-tidy 时,我从 clang-tidy 得到(imho)误报错误(最后的完整错误跟踪)
Value assigned to field 'id' in implicit constructor is garbage or undefined [clang-analyzer-core.uninitialized.Assign]
该代码只将 a 分配给boost::variant
两个值之一。如果我将复制构造函数替换为Size(const Size& sz) = default;
. 但是,我不能这样做,因为 Sizecv::Size
在实际代码中是 a 。
尽管它可能没有尽可能优雅,但我看不出有任何错误。如果我弄错了,clang-tidy或boost,有人可以指出我的方向。
#include <boost/variant.hpp>
class Size
{
public:
Size() = default;
Size(const Size &sz) : width(sz.width) {}
int width{0};
};
struct B {
Size size;
};
struct A {
Size size;
uint32_t id{0};
};
int main() {
using T = boost::variant<A, B>;
T config = B();
}
我在跑步:
- 铿锵工具-额外提交 50fe75789f08b96284d2c14cb6583b3783c74460
- llvm 提交 afb8c1fed21eb4848d86f2d28e9cb3afcfbb2656
- 提升 1.67
完整的错误转储:
test_variant.cc:15:8: warning: Value assigned to field 'id' in implicit constructor is garbage or undefined [clang-analyzer-core.uninitialized.Assign]
struct A {
^
test_variant.cc:22:5: note: Calling move constructor for 'variant'
T config = B();
^
libraries/boost/include/boost/variant/variant.hpp:1880:9: note: Calling 'variant::internal_apply_visitor'
operand.internal_apply_visitor(visitor);
^
libraries/boost/include/boost/variant/variant.hpp:2466:16: note: Calling 'variant::internal_apply_visitor_impl'
return internal_apply_visitor_impl(
^
libraries/boost/include/boost/variant/variant.hpp:2452:16: note: Calling 'visitation_impl'
return detail::variant::visitation_impl(
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:225:5: note: Control jumps to 'case 0:' at line 238
switch (logical_which)
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:240:11: note: Calling 'visitation_impl_invoke'
, BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE
^
libraries/boost/include/boost/preprocessor/repetition/repeat.hpp:29:26: note: expanded from macro 'BOOST_PP_REPEAT'
# define BOOST_PP_REPEAT BOOST_PP_CAT(BOOST_PP_REPEAT_, BOOST_PP_AUTO_REC(BOOST_PP_REPEAT_P, 4))
^
libraries/boost/include/boost/preprocessor/cat.hpp:22:32: note: expanded from macro 'BOOST_PP_CAT'
# define BOOST_PP_CAT(a, b) BOOST_PP_CAT_I(a, b)
^
libraries/boost/include/boost/preprocessor/cat.hpp:29:34: note: expanded from macro 'BOOST_PP_CAT_I'
# define BOOST_PP_CAT_I(a, b) a ## b
^
note: (skipping 21 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
libraries/boost/include/boost/preprocessor/repetition/repeat.hpp:53:56: note: expanded from macro 'BOOST_PP_REPEAT_1_2'
# define BOOST_PP_REPEAT_1_2(m, d) BOOST_PP_REPEAT_1_1(m, d) m(2, 1, d)
^
libraries/boost/include/boost/preprocessor/repetition/repeat.hpp:52:36: note: expanded from macro 'BOOST_PP_REPEAT_1_1'
# define BOOST_PP_REPEAT_1_1(m, d) m(2, 0, d)
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:231:16: note: expanded from macro 'BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE'
return (visitation_impl_invoke)( \
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:154:12: note: Calling 'visitation_impl_invoke_impl'
return (visitation_impl_invoke_impl)(
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:112:12: note: Calling 'move_into::internal_visit'
return visitor.internal_visit(
^
libraries/boost/include/boost/variant/variant.hpp:507:23: note: Calling implicit move constructor for 'A'
new(storage_) T(::boost::detail::variant::move(operand));
^
test_variant.cc:15:8: note: Value assigned to field 'id' in implicit constructor is garbage or undefined
struct A {