New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][Sema] Warn unused cxx vardecl which entirely consists condition expr of if/while/for construct #87348
Conversation
@llvm/pr-subscribers-clang Author: Youngsuk Kim (JOE1994) ChangesFixes #41447 Full diff: https://github.com/llvm/llvm-project/pull/87348.diff 4 Files Affected:
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index a5879591f4c659..5f1f83bb00282f 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -1100,6 +1100,9 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
LLVM_PREFERRED_TYPE(bool)
unsigned EscapingByref : 1;
+
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned IsCXXCondDecl : 1;
};
union {
@@ -1589,6 +1592,15 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
NonParmVarDeclBits.EscapingByref = true;
}
+ bool isCXXCondDecl() const {
+ return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsCXXCondDecl;
+ }
+
+ void setCXXCondDecl() {
+ assert(!isa<ParmVarDecl>(this));
+ NonParmVarDeclBits.IsCXXCondDecl = true;
+ }
+
/// Determines if this variable's alignment is dependent.
bool hasDependentAlignment() const;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 8b44d24f5273aa..62f71658fa3b5a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -2188,8 +2188,12 @@ void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD,
assert(iter->getSecond() >= 0 &&
"Found a negative number of references to a VarDecl");
- if (iter->getSecond() != 0)
- return;
+ if (iter->getSecond() != 0) {
+ bool UnusedCXXCondDecl = VD->isCXXCondDecl() && (iter->getSecond() == 1);
+ if (!UnusedCXXCondDecl)
+ return;
+ }
+
unsigned DiagID = isa<ParmVarDecl>(VD) ? diag::warn_unused_but_set_parameter
: diag::warn_unused_but_set_variable;
DiagReceiver(VD->getLocation(), PDiag(DiagID) << VD);
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index e9fecaea84b021..d4d69beeba5a05 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -18563,6 +18563,9 @@ DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) {
return true;
}
+ if (auto *VD = dyn_cast<VarDecl>(Dcl))
+ VD->setCXXCondDecl();
+
return Dcl;
}
diff --git a/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp b/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
index 418baa78aa964b..cb066e209cb453 100644
--- a/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
+++ b/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
@@ -69,3 +69,9 @@ template <typename T> void f5() {
SWarnUnused swu;
++swu;
}
+
+void f6() {
+ if (int x = 123) { // expected-warning{{variable 'x' set but not used}}
+ ;
+ }
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this change. It looks reasonable.
Can you provide:
- A description in the github issue
- A release note entry
- More tests (ie the tests in the issue
Just to reenforce what was mentioned already. Your PR summary should briefly describe the problem and the approach your solution takes to fixing it. This is important b/c this is what usually ends up in the git log and that is critical for folks triaging build issues up and downstream without having to do more digging to understand a commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo nitpicks. And we still need a more descriptive commit message. Please edit #87348 (comment)
Please add the updated 'commit message' to the github interface as well, as that is the one that gets actually merged. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
…on expr of if/while/for construct Emit -Wunused-but-set-variable warning on C++ variables whose declaration (with initializer) entirely consist the condition expression of a if/while/for construct but are not actually used in the body of the if/while/for construct.
6e35c36
to
cb41627
Compare
|
Emit
-Wunused-but-set-variable
warning on C++ variables whose declaration (with initializer) entirely consist the condition expression of a if/while/for construct but are not actually used in the body of the if/while/for construct.Fixes #41447