[libc++][test] Silence MSVC warning
authorCasey Carter <Casey@Carter.net>
Mon, 9 Jan 2023 09:38:14 +0000 (01:38 -0800)
committerCasey Carter <Casey@Carter.net>
Mon, 9 Jan 2023 23:05:31 +0000 (15:05 -0800)
Our static analyzer likes to warn when loop bodies are never executed, which is true for `make_string<T>("")`. Build the result with `basic_string`'s iterator-pair constructor instead, which is simpler (one liner), faster (single pass), and doesn't trigger the warning.

Differential Revision: https://reviews.llvm.org/D141263

libcxx/test/std/ranges/range.factories/range.istream.view/utils.h

index ec6ed4d..254c95b 100644 (file)
@@ -6,11 +6,7 @@
 
 template <class CharT, std::size_t N>
 auto make_string(const char (&in)[N]) {
-  std::basic_string<CharT> r(N - 1, static_cast<CharT>(0));
-  for (std::size_t i = 0; i < N - 1; ++i) {
-    r[i] = static_cast<CharT>(in[i]);
-  }
-  return r;
+  return std::basic_string<CharT>(in + 0, in + (N - 1));
 }
 
 template <class CharT, std::size_t N>