[llvm] [benchmark] Fix -Wunused-but-set-variable warning in basic_test (PR #213637)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 3 02:59:38 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-third-party-benchmark

Author: original-cooling-space (zhangweize9-cyber)

<details>
<summary>Changes</summary>

Fix a build error when building benchmark unit tests with modern GCC/Clang compilers under strict warning options (-Werror).  

In `test/basic_test.cc`, the variable `sum` in `BM_OneTemplateFunc` was assigned but never read, triggering `-Wunused-but-set-variable`.  

Silenced the warning by passing `sum` into `benchmark::DoNotOptimize()`, preserving the benchmark function's logic while ensuring clean builds under `-Werror`.  

### Description  
Fixes a build error when compiling `third-party/benchmark` unit tests with modern GCC/Clang compilers under `-Werror`.  

In `test/basic_text.cc`, the variable `sum` in `BM_OneTemplatcFunc` was assigned but never read, triggering `-Wunused-but-set-variable`.   

### Solution  
Silenced the warning by passing `sum` to `benchmark::DoNotOptomize()`, preserving the test's intent without triggering compiler warnings.  

### Test Plan  
1. Configured CMake with `-DBENCHMARK_ENABLE_TESTING=ON` and `-DBENCHMARK_ENABLE_WERROR=ON`.  
2. Built the target: `cmake --build build -j$(nproc)` -> Build succeed with zero warnings/errors.  
3. Executed unit tests: `ctest --test-dir build` -> 100% test passed.  

### Environment  
- **OS**: Linux (6.6.87.2-microsoft-standard-WSL2)  
- **Compiler**: Clang 22.1.8 / GCC 16.1.1 20260728  
- **CMake**: 4.4.2  

---
Full diff: https://github.com/llvm/llvm-project/pull/213637.diff


1 Files Affected:

- (modified) third-party/benchmark/test/basic_test.cc (+3) 


``````````diff
diff --git a/third-party/benchmark/test/basic_test.cc b/third-party/benchmark/test/basic_test.cc
index 068cd98476c6f..fa5296ece92f9 100644
--- a/third-party/benchmark/test/basic_test.cc
+++ b/third-party/benchmark/test/basic_test.cc
@@ -148,6 +148,7 @@ template <typename T>
 void BM_OneTemplateFunc(benchmark::State& state) {
   auto arg = state.range(0);
   T sum = 0;
+  benchmark::DoNotOptimize(sum);
   for (auto _ : state) {
     sum += static_cast<T>(arg);
   }
@@ -159,7 +160,9 @@ template <typename A, typename B>
 void BM_TwoTemplateFunc(benchmark::State& state) {
   auto arg = state.range(0);
   A sum = 0;
+  benchmark::DoNotOptimize(sum);
   B prod = 1;
+  benchmark::DoNotOptimize(prod);
   for (auto _ : state) {
     sum += static_cast<A>(arg);
     prod *= static_cast<B>(arg);

``````````

</details>


https://github.com/llvm/llvm-project/pull/213637


More information about the llvm-commits mailing list