llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Vinayak Dev (vinayakdsci) <details> <summary>Changes</summary> Fixes #<!-- -->79518 Copy constructors can have initialization with side effects, and thus clang should not emit a warning when -Wunused-variable is used in this context. Currently however, a warning is emitted. Now, compilation happens without warnings. --- Full diff: https://github.com/llvm/llvm-project/pull/81127.diff 2 Files Affected: - (modified) clang/lib/Sema/SemaDecl.cpp (+1-1) - (modified) clang/test/SemaCXX/warn-unused-variables.cpp (+26-2) ``````````diff diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 18a5d93ab8e8c..7f78aef33f1e3 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2044,7 +2044,7 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts, return false; if (Init) { - const auto *Construct = dyn_cast<CXXConstructExpr>(Init); + const auto *Construct = dyn_cast<CXXConstructExpr>(Init->IgnoreImpCasts()); if (Construct && !Construct->isElidable()) { const CXXConstructorDecl *CD = Construct->getConstructor(); if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() && diff --git a/clang/test/SemaCXX/warn-unused-variables.cpp b/clang/test/SemaCXX/warn-unused-variables.cpp index b649c7d808935..92032a9c2b812 100644 --- a/clang/test/SemaCXX/warn-unused-variables.cpp +++ b/clang/test/SemaCXX/warn-unused-variables.cpp @@ -1,5 +1,5 @@ // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s -// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++11 %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++17 %s template<typename T> void f() { T t; t = 17; @@ -183,7 +183,7 @@ void foo(int size) { NonTriviallyDestructible array[2]; // no warning NonTriviallyDestructible nestedArray[2][2]; // no warning - Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}} + Foo fooScalar = 1; // no warning Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}} Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}} } @@ -297,3 +297,27 @@ void RAIIWrapperTest() { } } // namespace gh54489 + +// Ensure that -Wunused-variable does not emit warning +// on copy constructors with side effects +namespace gh79518 { + +struct S { + S(int); +}; + +// With an initializer list +struct A { + int x; + A(int x) : x(x) {} +}; + +void foo() { + S s(0); // no warning + S s2 = 0; // no warning + S s3{0}; // no warning + + A a = 1; // no warning +} + +} // namespace gh79518 `````````` </details> https://github.com/llvm/llvm-project/pull/81127 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits