Skip to content

Commit be1f352

Browse files
committed
[Clang][Sema]: Allow copy constructor side effects
1 parent a643ab8 commit be1f352

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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 -std=gnu++17 %s
33
template<typename T> void f() {
44
T t;
55
t = 17;
@@ -183,7 +183,7 @@ void foo(int size) {
183183
NonTriviallyDestructible array[2]; // no warning
184184
NonTriviallyDestructible nestedArray[2][2]; // no warning
185185

186-
Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
186+
Foo fooScalar = 1; // no warning
187187
Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
188188
Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
189189
}
@@ -297,3 +297,27 @@ void RAIIWrapperTest() {
297297
}
298298

299299
} // namespace gh54489
300+
301+
// Ensure that -Wunused-variable does not emit warning
302+
// on copy constructors with side effects
303+
namespace gh79518 {
304+
305+
struct S {
306+
S(int);
307+
};
308+
309+
// With an initializer list
310+
struct A {
311+
int x;
312+
A(int x) : x(x) {}
313+
};
314+
315+
void foo() {
316+
S s(0); // no warning
317+
S s2 = 0; // no warning
318+
S s3{0}; // no warning
319+
320+
A a = 1; // no warning
321+
}
322+
323+
} // namespace gh79518

0 commit comments

Comments
 (0)