我有以下测试用例:
testcase("[room] exits") {
auto [center, east, north, south, west] = make_test_rooms();
check_eq(center->east(), east);
check_eq(center->north(), north);
check_eq(center->south(), south);
check_eq(center->west(), west + 1);
}
当我编译它时,clang++ (clang version 5.0.1 (tags/RELEASE_501/final)) 报告:
room.cpp:52:7: note: Value stored to '[center, east, north, south, west]' during its initialization is never read
在上面的代码中,testcase
是为doctest 单元测试包check_eq
定义的宏,它们扩展为(某种自注册变量+函数对)和(基本上,“断言 a == b”,具有魔术处理)。DOCTEST_TEST_CASE()
DOCTEST_CHECK_EQ
我知道这段代码正在执行,因为这west + 1
是一个故意引入的错误。当我运行我的测试时,我收到这样一条失败消息:
===============================================================================
/.../room.cpp(51)
TEST CASE: [room] exits
/.../room.cpp(57) ERROR!
CHECK_EQ( center->west(), west + 1 )
with expansion:
CHECK_EQ( 0x00007fd6f1d011a0, 0x00007fd6f1d011f8 )
据我所见,我使用了结构化绑定中的所有值:center
出现在我的支票左侧,并north, south, east, west
出现在右侧。为什么clang ++报告“从未读过”?
更新
这是重现该问题的代码。我运行这个命令行(取自输出make VERBOSE=1
并减少):
$ /opt/local/bin/cmake -E __run_co_compile --tidy=/opt/local/bin/clang-tidy --source=test.cpp -- /opt/local/bin/clang++ -g -std=gnu++1z -o test.cpp.o -c test.cpp
/Users/austin/Code/agb/test.cpp:12:7: warning: Value stored to '[a, b, c]' during its initialization is never read [clang-analyzer-deadcode.DeadStores]
auto [a,b,c] = foo();
^
/Users/austin/Code/agb/test.cpp:12:7: note: Value stored to '[a, b, c]' during its initialization is never read
我将此源文件用作test.cpp
:
#include <tuple>
static std::tuple<int, int, int>
foo()
{
return {1,2,3};
}
bool
test()
{
auto [a,b,c] = foo();
return a<b and b<c;
}