[PATCH] D122661: [Clang] Do not warn on unused lifetime-extending vars with side effects...

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 5 07:09:49 PDT 2022


aaron.ballman added inline comments.


================
Comment at: clang/lib/Sema/SemaDecl.cpp:1893-1894
+    const Expr *Init = VD->getInit();
+    if (const ExprWithCleanups *Cleanups =
+            dyn_cast_or_null<ExprWithCleanups>(Init))
+      Init = Cleanups->getSubExpr();
----------------



================
Comment at: clang/lib/Sema/SemaDecl.cpp:1901
     if (const TypedefType *TT = Ty->getAs<TypedefType>()) {
+      // White-list anything with an __attribute__((unused)) type.
       if (TT->getDecl()->hasAttr<UnusedAttr>())
----------------



================
Comment at: clang/lib/Sema/SemaDecl.cpp:1908-1909
+    // extension.
+    if (const MaterializeTemporaryExpr *MTE =
+            dyn_cast_or_null<MaterializeTemporaryExpr>(Init)) {
+      if (MTE->getExtendingDecl()) {
----------------



================
Comment at: clang/test/SemaCXX/warn-unused-variables.cpp:275-278
+template <typename T>
+void foo(T &t) {
+  const auto &extended = S{t};
+}
----------------
I think the difference between Clang and GCC here is that GCC only diagnoses when this function is instantiated: https://godbolt.org/z/f6ddacT97

How do we behave when we instantiate it?

Also, what happens with the test case as-written in the bug report:
```
struct RAIIWrapper {
    RAIIWrapper();
    ~RAIIWrapper();
};

void foo() {
    auto const  guard = RAIIWrapper();
    auto const& guard2 = RAIIWrapper();
    auto && guard3 = RAIIWrapper();
}
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122661/new/

https://reviews.llvm.org/D122661



More information about the cfe-commits mailing list