Skip to content

Commit eeb3f99

Browse files
committed
[clang-tidy] misc-unused-parameters: don't comment out parameter name for C code
Summary: The fixit `int square(int /*num*/)` yields `error: parameter name omitted` for C code. Enable it only for C++ code. Reviewers: klimek, ilya-biryukov, lebedev.ri, aaron.ballman Subscribers: xazax.hun, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63088 llvm-svn: 364106
1 parent 19c4d66 commit eeb3f99

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ void UnusedParametersCheck::warnOnUnusedParameter(
138138
Indexer = llvm::make_unique<IndexerVisitor>(*Result.Context);
139139
}
140140

141-
// Comment out parameter name for non-local functions.
141+
// Cannot remove parameter for non-local functions.
142142
if (Function->isExternallyVisible() ||
143143
!Result.SourceManager->isInMainFile(Function->getLocation()) ||
144144
!Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function)) {
145+
146+
// It is illegal to omit parameter name here in C code, so early-out.
147+
if (!Result.Context->getLangOpts().CPlusPlus)
148+
return;
149+
145150
SourceRange RemovalRange(Param->getLocation());
146-
// Note: We always add a space before the '/*' to not accidentally create a
147-
// '*/*' for pointer types, which doesn't start a comment. clang-format will
148-
// clean this up afterwards.
151+
// Note: We always add a space before the '/*' to not accidentally create
152+
// a '*/*' for pointer types, which doesn't start a comment. clang-format
153+
// will clean this up afterwards.
149154
MyDiag << FixItHint::CreateReplacement(
150155
RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
151156
return;

clang-tools-extra/test/clang-tidy/misc-unused-parameters.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// =============
55
void a(int i) {;}
66
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
7-
// CHECK-FIXES: {{^}}void a(int /*i*/) {;}{{$}}
7+
// CHECK-FIXES: {{^}}void a(int i) {;}{{$}}
88

99
static void b(); // In C, forward declarations can leave out parameters.
1010
static void b(int i) {;}

0 commit comments

Comments
 (0)