[clang-tools-extra] [clang-tidy] Fix assignment-in-if-condition false positive in statement expressions (PR #231704)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 16 05:52:12 PDT 2026


================
@@ -80,6 +80,33 @@ void f5(int arg) {
   v = f;
 }
 
+void f6(int arg) {
+  int x;
+  if (({ x = 5; }))
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
+    return;
+}
+
+void f7(int arg) {
+  if (({ int r; r = arg; (r = 1); }))
+  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
+    return;
+}
+
+void f8(int arg) {
+  if (({ label: arg = 1; }))
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
+    return;
+}
+
+int get_value();
+
+void f9(int arg) {
+  if (({ [[clang::nomerge]] arg = get_value(); }))
+  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
+    return;
+}
----------------
mark-zilber wrote:

f9 ensures that an assignment wrapped in an attribute is still diagnosed ( [[clang::nomerge]] turns the last statement into an AttributedStmt, so it's no longer an Expr)

Initially, when I used a custom loop for unwrapping, this was the only test covering the attribute branch. After switching to getExprStmt(), I assumed it was redundant, since the unwrapping is now done by an existing clang function that's already tested. But it actually checks our behavior rather than the unwrapping itself. Therefore, I've brought it back

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


More information about the cfe-commits mailing list