Skip to content

Commit

Permalink
[clang-tidy] misc-unused-parameters: don't comment out parameter name…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
mgehre committed Jun 21, 2019
1 parent 19c4d66 commit eeb3f99
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
Expand Up @@ -138,14 +138,19 @@ void UnusedParametersCheck::warnOnUnusedParameter(
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)) {

// It is illegal to omit parameter name here in C code, so early-out.
if (!Result.Context->getLangOpts().CPlusPlus)
return;

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.
// 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;
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/test/clang-tidy/misc-unused-parameters.c
Expand Up @@ -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) {;}
Expand Down

0 comments on commit eeb3f99

Please sign in to comment.