[clang] [clang] Extend -Wunused-but-set-variable to static globals (PR #178342)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 19 01:14:14 PST 2026


================
@@ -0,0 +1,130 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-but-set-variable -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-but-set-variable -I %S -verify %S/Inputs/warn-unused-but-set-static-global-header-test.c
+
+#define NULL (void*)0
+
+void *set(int size);
+void func_call(void *);
+
+static int set_unused; // expected-warning {{variable 'set_unused' set but not used}}
+static int set_and_used;
+static int only_used;
+static int addr_taken;
+extern int external_var;  // No warning (external linkage).
+extern int global_var;  // No warning (not static).
+
+void f1() {
+  set_unused = 1;
+  set_and_used = 2;
+
+  int x = set_and_used;
+  (void)x;
+
+  int y = only_used;
+  (void)y;
+
+  int *p = &addr_taken;
+  (void)p;
+
+  external_var = 3;
+  global_var = 4;
+}
+
+// Test across multiple functions.
+static int set_used1;
+static int set_used2;
+
+static int set1; // expected-warning {{variable 'set1' set but not used}}
+static int set2; // expected-warning {{variable 'set2' set but not used}}
+
+void f2() {
+  set1 = 1;
+  set_used1 = 1;
+
+  int x = set_used2;
+  (void)x;
+}
+
+void f3() {
+  set2 = 2;
+  set_used2 = 2;
+
+  int x = set_used1;
+  (void)x;
+}
+
+static volatile int vol_set; // expected-warning {{variable 'vol_set' set but not used}}
----------------
zwuis wrote:

Should we warn on this? @JustinStitt What do you think?

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


More information about the cfe-commits mailing list