Skip to content
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

Merged
merged 1 commit into from Apr 5, 2024

Conversation

JOE1994
Copy link
Member

@JOE1994 JOE1994 commented Apr 2, 2024

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

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 2, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 2, 2024

@llvm/pr-subscribers-clang

Author: Youngsuk Kim (JOE1994)

Changes

Fixes #41447


Full diff: https://github.com/llvm/llvm-project/pull/87348.diff

4 Files Affected:

  • (modified) clang/include/clang/AST/Decl.h (+12)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+6-2)
  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+3)
  • (modified) clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp (+6)
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}}
+    ;
+  }
+}

@JOE1994 JOE1994 added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Apr 2, 2024
@JOE1994 JOE1994 requested a review from Endilll April 2, 2024 16:36
Copy link
Contributor

@cor3ntin cor3ntin left a 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

clang/lib/Sema/SemaDecl.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaDecl.cpp Outdated Show resolved Hide resolved
clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp Outdated Show resolved Hide resolved
@shafik
Copy link
Collaborator

shafik commented Apr 4, 2024

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.

@JOE1994
Copy link
Member Author

JOE1994 commented Apr 4, 2024

@cor3ntin @shafik Thank you so much for your feedback!
I've pushed an extra commit with more explanation in comments along with more test cases.
I also added an entry to Clang release notes.

Copy link
Contributor

@cor3ntin cor3ntin left a 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)

clang/docs/ReleaseNotes.rst Outdated Show resolved Hide resolved
clang/lib/Sema/SemaDecl.cpp Outdated Show resolved Hide resolved
@erichkeane
Copy link
Collaborator

Please add the updated 'commit message' to the github interface as well, as that is the one that gets actually merged.

@JOE1994 JOE1994 changed the title [Clang][Sema] Warn unused cxx var declared in condition of if-statement [Clang][Sema] Warn unused cxx vardecl which entirely consists condition expr of if/while/for construct Apr 4, 2024
Copy link

github-actions bot commented Apr 4, 2024

✅ 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.
@JOE1994 JOE1994 force-pushed the clang_warn_unused_cxxconddecl branch from 6e35c36 to cb41627 Compare April 4, 2024 19:07
@JOE1994
Copy link
Member Author

JOE1994 commented Apr 4, 2024

  • Rebased on top of main to resolve merge conflicts in ReleaseNotes.
  • Applied clang-format
  • Updated commit message to the github interface.

@JOE1994 JOE1994 merged commit 90453f4 into llvm:main Apr 5, 2024
5 checks passed
@JOE1994 JOE1994 deleted the clang_warn_unused_cxxconddecl branch April 5, 2024 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Doesn't warn about the unused variable
5 participants