Skip to content

Commit f2f9ba7

Browse files
committed
[Clang][Sema]: Allow copy constructor side effects
1 parent 356fdc3 commit f2f9ba7

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

clang/docs/ReleaseNotes.rst

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

176+
- Clang now doesn't produce false-positive warning `-Wunused-variable`
177+
for variables created through copy initialization having side-effects.
178+
Fixes (`#79518 <https://github.com/llvm/llvm-project/issues/79518>`_).
179+
176180
Bug Fixes to Compiler Builtins
177181
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178182

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: 33 additions & 0 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
22
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++11 %s
3+
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -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,12 @@ void foo(int size) {
183185
NonTriviallyDestructible array[2]; // no warning
184186
NonTriviallyDestructible nestedArray[2][2]; // no warning
185187

188+
// Copy initialzation gives warning before C++17
189+
#if __cplusplus <= 201402L
186190
Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
191+
#else
192+
Foo fooScalar = 1; // no warning
193+
#endif
187194
Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
188195
Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
189196
}
@@ -297,3 +304,29 @@ void RAIIWrapperTest() {
297304
}
298305

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

0 commit comments

Comments
 (0)