Author: Cristiano Lacerda
Date: 2026-10-02T14:56:48Z
New Revision: fdfd37c0148b6186c19cc9ffa3f2cbfeb2948533

URL: 
https://github.com/llvm/llvm-project/commit/fdfd37c0148b6186c19cc9ffa3f2cbfeb2948533
DIFF: 
https://github.com/llvm/llvm-project/commit/fdfd37c0148b6186c19cc9ffa3f2cbfeb2948533.diff

LOG: [clang][analyzer] Diagnose dead stores on parenthesized lvalues (#227176)

This commit fixes that `deadcode.DeadStores` previously failed to diagnose a
dead store when the assigned variable is wrapped in parentheses.

```c
int f(int input) {
  int value;
  (value) = input;
  value = 2;
  return value;
}
```

This change was made with assistance of an AI tool (OpenAI Codex), which
implemented the fix (IgnoreParens()) and the test.
I have found the false negative myself (on a real codebase that uses a
code generator) and ran the tests manually.

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
    clang/test/Analysis/dead-stores.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
index 0d693a3e3ff3dd..c68ee72a9dbda5 100644
--- a/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
@@ -325,7 +325,7 @@ class DeadStoreObs : public LiveVariables::Observer {
     if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
       if (!B->isAssignmentOp()) return; // Skip non-assignments.
 
-      if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
+      if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens()))
         if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
           // Special case: check for assigning null to a pointer.
           //  This is a common form of defensive programming.

diff  --git a/clang/test/Analysis/dead-stores.c 
b/clang/test/Analysis/dead-stores.c
index 1e9f230415163f..05e7566fc9b5a0 100644
--- a/clang/test/Analysis/dead-stores.c
+++ b/clang/test/Analysis/dead-stores.c
@@ -660,3 +660,20 @@ int rdar34122265_test_nested(int input) {
   }
   return bar.x.x + bar.y.y;
 }
+
+// Parentheses on the left-hand side must not hide a dead store.
+long long dead_store_to_parenthesized_variable(int input) {
+  int foo;
+  long long bar;
+  long baz;
+  (bar) = input;
+  // non-nested-warning@-1 {{Value stored to 'bar' is never read}}
+  (foo) = input;
+  // non-nested-warning@-1 {{Value stored to 'foo' is never read}}
+  (baz) = input;
+  // non-nested-warning@-1 {{Value stored to 'baz' is never read}}
+  bar = 2;
+  foo = 2;
+  baz = 2;
+  return bar + foo + baz;
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to