boostorg / test

The reference C++ unit testing framework (TDD, xUnit, C++03/11/14/17)
http://boost.org/libs/test
Boost Software License 1.0
183 stars 140 forks source link

clang-tidy bugprone-use-after-move false positive in BOOST_* macros #343

Closed jspam closed 1 month ago

jspam commented 2 years ago
#include <boost/test/unit_test.hpp>
#include <vector>

void f(std::vector<int> p);

int main() {
  std::vector<int> v;
  BOOST_CHECK_NO_THROW(f(std::move(v)));
} 

run clang-tidy --extra-arg=-I/path/to/boost/include -checks=-*,bugprone-use-after-move test.cpp (clang-tidy version 14.0.5)

yields:

/tmp/test.cpp:8:36: warning: 'v' used after it was moved [bugprone-use-after-move]                                                           
  BOOST_CHECK_NO_THROW(f(std::move(v)));                                                                                                     

/tmp/test.cpp:8:26: note: move occurred here                                                                                                 
  BOOST_CHECK_NO_THROW(f(std::move(v)));                                                                                                     

/tmp/test.cpp:8:36: note: the use happens in a later loop iteration than the move                                                            
  BOOST_CHECK_NO_THROW(f(std::move(v)));

Control flow analysis seems to be unable to see do { … } while( ::boost::test_tools::tt_detail::dummy_cond() ) as a loop that runs only once. If I replace these loops by do { … } while(0) loops in boost/test/tools/old/interface.hpp, the warning disappears.

Since it's probably unreasonable to expect clang-tidy to look into function calls, I wanted to first ask here – what is the purpose of dummy_cond? Would it be possible to use do { … } while(0) instead, which seems to be a common pattern?