File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,17 @@ bool isOverrideMethod(const FunctionDecl *Function) {
2626 return MD->size_overridden_methods () > 0 || MD->hasAttr <OverrideAttr>();
2727 return false ;
2828}
29+
30+ bool hasAttrAfterParam (const clang::SourceManager *SourceManager,
31+ const ParmVarDecl *Param) {
32+ for (const auto *Attr : Param->attrs ()) {
33+ if (SourceManager->isBeforeInTranslationUnit (Param->getLocation (),
34+ Attr->getLocation ())) {
35+ return true ;
36+ }
37+ }
38+ return false ;
39+ }
2940} // namespace
3041
3142void UnusedParametersCheck::registerMatchers (MatchFinder *Finder) {
@@ -189,6 +200,11 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
189200 if (Param->isUsed () || Param->isReferenced () || !Param->getDeclName () ||
190201 Param->hasAttr <UnusedAttr>())
191202 continue ;
203+ if (hasAttrAfterParam (Result.SourceManager , Param)) {
204+ // Due to how grammar works, attributes would be wrongly applied to the
205+ // type if we remove the preceding parameter name.
206+ continue ;
207+ }
192208
193209 // In non-strict mode ignore function definitions with empty bodies
194210 // (constructor initializer counts for non-empty body).
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ void f(void (*fn)()) {;}
3333// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: parameter 'fn' is unused [misc-unused-parameters]
3434// CHECK-FIXES: {{^}}void f(void (* /*fn*/)()) {;}{{$}}
3535
36+ int *k ([[clang::lifetimebound]] int *i) {;}
37+ // CHECK-MESSAGES: :[[@LINE-1]]:38: warning: parameter 'i' is unused [misc-unused-parameters]
38+ // CHECK-FIXES: {{^}}int *k({{\[\[clang::lifetimebound\]\]}} int * /*i*/) {;}{{$}}
39+
3640// Unchanged cases
3741// ===============
3842void f (int i); // Don't remove stuff in declarations
@@ -41,6 +45,7 @@ void h(int i[]);
4145void s (int i[1 ]);
4246void u (void (*fn)());
4347void w (int i) { (void )i; } // Don't remove used parameters
48+ int *x (int *i [[clang::lifetimebound]]) { return nullptr ; } // Don't reanchor attribute to the type.
4449
4550bool useLambda (int (*fn)(int ));
4651static bool static_var = useLambda([] (int a) { return a; });
You can’t perform that action at this time.
0 commit comments