Skip to content

Commit 2b4d67b

Browse files
authored
[Clang][Sema]: Allow copy constructor side effects (#81127)
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. Fixes #79518
1 parent 601a958 commit 2b4d67b

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ Bug Fixes in This Version
218218
for logical operators in C23.
219219
Fixes (`#64356 <https://github.com/llvm/llvm-project/issues/64356>`_).
220220

221+
- Clang no longer produces a false-positive `-Wunused-variable` warning
222+
for variables created through copy initialization having side-effects in C++17 and later.
223+
Fixes (`#79518 <https://github.com/llvm/llvm-project/issues/79518>`_).
224+
221225
Bug Fixes to Compiler Builtins
222226
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
223227

clang/lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,8 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
20442044
return false;
20452045

20462046
if (Init) {
2047-
const auto *Construct = dyn_cast<CXXConstructExpr>(Init);
2047+
const auto *Construct =
2048+
dyn_cast<CXXConstructExpr>(Init->IgnoreImpCasts());
20482049
if (Construct && !Construct->isElidable()) {
20492050
const CXXConstructorDecl *CD = Construct->getConstructor();
20502051
if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() &&

clang/test/SemaCXX/warn-unused-variables.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s
2-
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++11 %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++11 %s
3+
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++14 %s
4+
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++17 %s
35
template<typename T> void f() {
46
T t;
57
t = 17;
@@ -183,7 +185,8 @@ void foo(int size) {
183185
NonTriviallyDestructible array[2]; // no warning
184186
NonTriviallyDestructible nestedArray[2][2]; // no warning
185187

186-
Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
188+
// Copy initialzation gives warning before C++17
189+
Foo fooScalar = 1; // cxx98-14-warning {{unused variable 'fooScalar'}}
187190
Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
188191
Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
189192
}
@@ -297,3 +300,29 @@ void RAIIWrapperTest() {
297300
}
298301

299302
} // namespace gh54489
303+
304+
// Ensure that -Wunused-variable does not emit warning
305+
// on copy constructors with side effects (C++17 and later)
306+
#if __cplusplus >= 201703L
307+
namespace gh79518 {
308+
309+
struct S {
310+
S(int);
311+
};
312+
313+
// With an initializer list
314+
struct A {
315+
int x;
316+
A(int x) : x(x) {}
317+
};
318+
319+
void foo() {
320+
S s(0); // no warning
321+
S s2 = 0; // no warning
322+
S s3{0}; // no warning
323+
324+
A a = 1; // no warning
325+
}
326+
327+
} // namespace gh79518
328+
#endif

0 commit comments

Comments
 (0)