5

我有以下测试用例:

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;
}
4

1 回答 1

-2

clang 分析器常见问题解答中,这被认为是常见的误报:

问:我如何告诉静态分析器我不关心特定的死存储?

当分析器发现存储在变量中的值从未被使用时,它将产生类似于以下的消息:

Value stored to 'x' is never read

您可以使用 (void)x; 习惯用法是承认您的代码中有一个死存储,但您不希望将来报告它。

关于为什么它是误报的推理是没有用的。警告可能会随着代码或 Clang 版本之间的细微更改而消失/重新出现。

于 2018-01-24T00:06:22.227 回答