Skip to content

Commit

Permalink
Revert "Reland "[Clang][SemaCXX] Add unused warning for variables dec…
Browse files Browse the repository at this point in the history
…lared in condition expressions""

This reverts commit 92023b1.

Further issues were uncovered:
- False positive -Wunused-variable on function-static variables with the
  used attribute. ClangBuiltLinux/linux#1926
  A test case needs to be added for this.
- Request for a new flag for these.
  https://reviews.llvm.org/D152495#4628877
  https://reviews.llvm.org/D152495#4628887
  • Loading branch information
nickdesaulniers committed Aug 31, 2023
1 parent f6ba985 commit f0f395e
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 122 deletions.
3 changes: 0 additions & 3 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ Improvements to Clang's diagnostics
(`#64871: <https://github.com/llvm/llvm-project/issues/64871>`_).
Also clang no longer emits false positive warnings about the output length of
``%g`` format specifier.
- Clang now warns on unused variables declared and initialized in condition
expressions.
(`#61681: <https://github.com/llvm/llvm-project/issues/61681>`_)

Bug Fixes in This Version
-------------------------
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
return false;
} else if (!D->getDeclName()) {
return false;
} else if (D->isReferenced() || (!isa<VarDecl>(D) && D->isUsed())) {
} else if (D->isReferenced() || D->isUsed()) {
return false;
}

Expand Down
4 changes: 0 additions & 4 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4021,10 +4021,6 @@ ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar,
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());
ConditionVar->setReferenced(/*R=*/false);

switch (CK) {
case ConditionKind::Boolean:
return CheckBooleanCondition(StmtLoc, Condition.get());
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5353,7 +5353,7 @@ void Sema::BuildVariableInstantiation(
// will have been deferred.
if (!NewVar->isInvalidDecl() &&
NewVar->getDeclContext()->isFunctionOrMethod() &&
OldVar->getType()->isDependentType() && !OldVar->isImplicit())
OldVar->getType()->isDependentType())
DiagnoseUnusedDecl(NewVar);
}

Expand Down
114 changes: 1 addition & 113 deletions clang/test/SemaCXX/warn-unused-variables.cpp
Original file line number Diff line number Diff line change
@@ -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++14-extensions -Wno-c++17-extensions -verify -std=c++11 %s
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=c++11 %s
template<typename T> void f() {
T t;
t = 17;
Expand Down Expand Up @@ -294,115 +294,3 @@ void RAIIWrapperTest() {
}

} // 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);
else if (auto [a, b] = (int[2]){ 1, 2 }; 1) // expected-warning {{unused variable '[a, b]'}}
return;
else if (auto [a, b] = (int[2]){ 1, 2 }; a)
return;
}

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;
}

void whiles() {
while(int unused = 1) // expected-warning {{unused variable 'unused'}}
return;
while(int used = 1)
(void)used;
}


void switches() {
switch(int unused = 1) { // expected-warning {{unused variable 'unused'}}
case 1: return;
}
switch(constexpr int used = 3; int unused = 4) { // expected-warning {{unused variable 'unused'}}
case used: return;
}
switch(int used = 3; int unused = 4) { // expected-warning {{unused variable 'unused'}}
case 3: (void)used;
}
switch(constexpr int used1 = 0; constexpr int used2 = 6) {
case (used1+used2): return;
}
switch(auto [a, b] = (int[2]){ 1, 2 }; 1) { // expected-warning {{unused variable '[a, b]'}}
case 1: return;
}
switch(auto [a, b] = (int[2]){ 1, 2 }; b) {
case 1: return;
}
switch(auto [a, b] = (int[2]){ 1, 2 }; 1) {
case 1: (void)a;
}
}
template <typename T>
struct Vector {
void doIt() {
for (auto& e : elements){} // expected-warning {{unused variable 'e'}}
}
T elements[10];
};
void ranged_for() {
Vector<int> vector;
vector.doIt(); // expected-note {{here}}
}


struct RAII {
int &x;
RAII(int &ref) : x(ref) {}
~RAII() { x = 0;}
operator int() const { return 1; }
};
void aggregate() {
int x = 10;
int y = 10;
if (RAII var = x) {}
for(RAII var = x; RAII var2 = y;) {}
while (RAII var = x) {}
switch (RAII var = x) {}
}

struct TrivialDtor{
int &x;
TrivialDtor(int &ref) : x(ref) { ref = 32; }
operator int() const { return 1; }
};
void trivial_dtor() {
int x = 10;
int y = 10;
if (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
for(TrivialDtor var = x; TrivialDtor var2 = y;) {} // expected-warning {{unused variable 'var'}} \
// expected-warning {{unused variable 'var2'}}
while (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
switch (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
}

} // namespace inside_condition

0 comments on commit f0f395e

Please sign in to comment.