[PATCH] D116513: [clang-tidy] Fix bugs in misc-unused-parameters for Constructors calls site

Mehdi AMINI via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 2 18:33:59 PST 2022


mehdi_amini created this revision.
mehdi_amini added a reviewer: Eugene.Zelenko.
Herald added subscribers: carlosgalvezp, xazax.hun.
mehdi_amini requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

These weren't tracked and so weren't updated when applying fixes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116513

Files:
  clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters.cpp
@@ -186,6 +186,7 @@
 
 void someMoreCallSites() {
   C c(42);
+// CHECK-FIXES: C c();
   c.f(1);
 // CHECK-FIXES: c.f();
   c.g(1);
Index: clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -64,8 +64,9 @@
                                            : nullptr));
 }
 
+template <typename CallExprT>
 static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
-                                const CallExpr *Call, unsigned Index) {
+                                const CallExprT *Call, unsigned Index) {
   return FixItHint::CreateRemoval(removeNode(
       Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
       Call->getArg(Index),
@@ -75,13 +76,20 @@
 class UnusedParametersCheck::IndexerVisitor
     : public RecursiveASTVisitor<IndexerVisitor> {
 public:
-  IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); }
+  IndexerVisitor(ASTContext &Ctx) : mgr(Ctx.getSourceManager()) {
+    TraverseAST(Ctx);
+  }
 
   const std::unordered_set<const CallExpr *> &
   getFnCalls(const FunctionDecl *Fn) {
     return Index[Fn->getCanonicalDecl()].Calls;
   }
 
+  const std::unordered_set<const CXXConstructExpr *> &
+  getCtorCalls(const FunctionDecl *Fn) {
+    return Index[Fn->getCanonicalDecl()].CtorRefs;
+  }
+
   const std::unordered_set<const DeclRefExpr *> &
   getOtherRefs(const FunctionDecl *Fn) {
     return Index[Fn->getCanonicalDecl()].OtherRefs;
@@ -97,6 +105,15 @@
     return true;
   }
 
+  bool WalkUpFromCXXConstructExpr(CXXConstructExpr *Call) {
+    if (const auto *Ctor =
+            dyn_cast_or_null<FunctionDecl>(Call->getConstructor())) {
+      Ctor = Ctor->getCanonicalDecl();
+      Index[Ctor].CtorRefs.insert(Call);
+    }
+    return true;
+  }
+
   bool WalkUpFromCallExpr(CallExpr *Call) {
     if (const auto *Fn =
             dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
@@ -114,8 +131,9 @@
   struct IndexEntry {
     std::unordered_set<const CallExpr *> Calls;
     std::unordered_set<const DeclRefExpr *> OtherRefs;
+    std::unordered_set<const CXXConstructExpr *> CtorRefs;
   };
-
+  SourceManager &mgr;
   std::unordered_map<const FunctionDecl *, IndexEntry> Index;
 };
 
@@ -165,9 +183,14 @@
       MyDiag << removeParameter(Result, FD, ParamIndex);
 
   // Fix all call sites.
-  for (const CallExpr *Call : Indexer->getFnCalls(Function))
+  for (const CallExpr *Call : Indexer->getFnCalls(Function)) {
+    if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
+      MyDiag << removeArgument(Result, Call, ParamIndex);
+  }
+  for (const CXXConstructExpr *Call : Indexer->getCtorCalls(Function)) {
     if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
       MyDiag << removeArgument(Result, Call, ParamIndex);
+  }
 }
 
 void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116513.396975.patch
Type: text/x-patch
Size: 3308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220103/548dc029/attachment-0001.bin>


More information about the cfe-commits mailing list