Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -381,6 +381,9 @@ (`#57081: `_) - Clang no longer emits inappropriate notes about the loss of ``__unaligned`` qualifier on overload resolution, when the actual reason for the failure is loss of other qualifiers. +- Clang now warns on unused variables of non-aggregate types declared and initialized + in condition expressions. + (`#61681: `_) Bug Fixes in This Version ------------------------- Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -1989,7 +1989,7 @@ return false; } else if (!D->getDeclName()) { return false; - } else if (D->isReferenced() || D->isUsed()) { + } else if (D->isReferenced() || (!isa(D) && D->isUsed())) { return false; } Index: clang/lib/Sema/SemaExprCXX.cpp =================================================================== --- clang/lib/Sema/SemaExprCXX.cpp +++ clang/lib/Sema/SemaExprCXX.cpp @@ -4013,6 +4013,11 @@ ConditionVar, ConditionVar->getType().getNonReferenceType(), VK_LValue, ConditionVar->getLocation()); + // Ensure that `-Wunused-variable` will be emitted for condition variables + // that are not referenced later. e.g.: if (int var = init()); + if (!T->isAggregateType()) + ConditionVar->setReferenced(/*R=*/false); + switch (CK) { case ConditionKind::Boolean: return CheckBooleanCondition(StmtLoc, Condition.get()); Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -5345,7 +5345,7 @@ // will have been deferred. if (!NewVar->isInvalidDecl() && NewVar->getDeclContext()->isFunctionOrMethod() && - OldVar->getType()->isDependentType()) + OldVar->getType()->isDependentType() && !OldVar->isImplicit()) DiagnoseUnusedDecl(NewVar); } Index: clang/test/SemaCXX/warn-unused-variables.cpp =================================================================== --- clang/test/SemaCXX/warn-unused-variables.cpp +++ 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=c++11 %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -Wno-c++1z-extensions -verify -std=c++11 %s template void f() { T t; t = 17; @@ -294,3 +294,49 @@ } } // namespace gh54489 + +namespace inside_condition { + void ifs() { + if (int hoge = 0) // expected-warning {{unused variable 'hoge'}} + return; + if (const int const_hoge = 0) // expected-warning {{unused variable 'const_hoge'}} + return; + else if (int fuga = 0) + (void)fuga; + else if (int used = 1; int catched = used) // expected-warning {{unused variable 'catched'}} + return; + else if (int refed = 1; int used = refed) + (void)used; + else if (int unused1 = 2; int unused2 = 3) // expected-warning {{unused variable 'unused1'}} \ + // expected-warning {{unused variable 'unused2'}} + return; + else if (int unused = 4; int used = 5) // expected-warning {{unused variable 'unused'}} + (void)used; + else if (int used = 6; int unused = 7) // expected-warning {{unused variable 'unused'}} + (void)used; + else if (int used1 = 8; int used2 = 9) + (void)(used1 + used2); + } + + void fors() { + for (int i = 0;int unused = 0;); // expected-warning {{unused variable 'i'}} \ + // expected-warning {{unused variable 'unused'}} + for (int i = 0;int used = 0;) // expected-warning {{unused variable 'i'}} + (void)used; + while(int var = 1) // expected-warning {{unused variable 'var'}} + return; + } + + struct RAII { + int &x; + RAII(int &ref) : x(ref) {} + ~RAII() { x = 0;} + operator bool() const { return true; } + }; + void aggregate() { + int x = 10; + if (RAII var = x) {} + } + + +} // namespace inside_condition