[PATCH] D63088: [clang-tidy] misc-unused-parameters: don't comment out parameter name for C code

Matthias Gehre via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 10 12:16:49 PDT 2019


mgehre created this revision.
mgehre added reviewers: klimek, ilya-biryukov, lebedev.ri, aaron.ballman.
Herald added a subscriber: xazax.hun.
Herald added a project: clang.

The fixit `int square(int /*num*/)` yields `error: parameter name omitted` for C code. Enable it only for C++ code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63088

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


Index: clang-tools-extra/test/clang-tidy/misc-unused-parameters.c
===================================================================
--- clang-tools-extra/test/clang-tidy/misc-unused-parameters.c
+++ clang-tools-extra/test/clang-tidy/misc-unused-parameters.c
@@ -4,7 +4,7 @@
 // =============
 void a(int i) {;}
 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
-// CHECK-FIXES: {{^}}void a(int  /*i*/) {;}{{$}}
+// CHECK-FIXES: {{^}}void a(int i) {;}{{$}}
 
 static void b(); // In C, forward declarations can leave out parameters.
 static void b(int i) {;}
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
@@ -138,16 +138,21 @@
     Indexer = llvm::make_unique<IndexerVisitor>(*Result.Context);
   }
 
-  // Comment out parameter name for non-local functions.
+  // Cannot remove parameter for non-local functions.
   if (Function->isExternallyVisible() ||
       !Result.SourceManager->isInMainFile(Function->getLocation()) ||
       !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function)) {
-    SourceRange RemovalRange(Param->getLocation());
-    // Note: We always add a space before the '/*' to not accidentally create a
-    // '*/*' for pointer types, which doesn't start a comment. clang-format will
-    // clean this up afterwards.
-    MyDiag << FixItHint::CreateReplacement(
-        RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
+
+    // Comment out parameter name.
+    if (Result.Context->getLangOpts().CPlusPlus) {
+      SourceRange RemovalRange(Param->getLocation());
+      // Note: We always add a space before the '/*' to not accidentally create
+      // a
+      // '*/*' for pointer types, which doesn't start a comment. clang-format
+      // will clean this up afterwards.
+      MyDiag << FixItHint::CreateReplacement(
+          RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
+    }
     return;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63088.203871.patch
Type: text/x-patch
Size: 2144 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190610/49df96ce/attachment.bin>


More information about the cfe-commits mailing list