[clang] [Clang] Improve diagnostics to show the correct position for [[maybe_… (PR #223910)
Alexsander Borges Damaceno via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 15 22:06:21 PDT 2026
https://github.com/AlexsanderDamaceno created https://github.com/llvm/llvm-project/pull/223910
For [[maybe_unused]] written before the 'using' keyword of an alias-declaration:
`[[maybe_unused]] using T = int;`
Clang currently reports only:
`error: an attribute list cannot appear here
`
with no indication of where the attribute should actually go
`Parser::DiagnoseProhibitedAttributes` already provides exactly this behavior, emitting a more helpful fix-it that suggests moving the attribute to the correct location. ParseUsingDeclaration already uses it without pass the FixItLoc location this way the fix-it sugestion is not show for using decl.
This patch passes the right location when the attribute is `[[maybe_unused]]`, making the fix-it appear for the user.
Improvement suggestion link: https://github.com/llvm/llvm-project/issues/155787
>From b2b84f410f0bf9e35a3b7ccbc17e9b48690fac8f Mon Sep 17 00:00:00 2001
From: AlexsanderDamaceno <aemgbo at gmail.com>
Date: Wed, 16 Sep 2026 01:04:36 -0300
Subject: [PATCH] [Clang] Improve diagnostics to show the correct position for
[[maybe_unused]] before a using-alias
For [[maybe_unused]] written before the 'using' keyword of an
alias-declaration:
[[maybe_unused]] using T = int;
Clang currently reports only:
error: an attribute list cannot appear here
with no indication of where the attribute should actually go
Parser::DiagnoseProhibitedAttributes already supports emitting a more
helpful fix-it that moves the attribute to a given correct location.
ParseUsingDeclaration already uses it without pass the FixItLoc location
this way is fix-it sugestion is not show for using decl.
This patch passes the right location when the attribute is [[maybe_unused]],
making the fix-it appear for the user.
---
clang/lib/Parse/ParseDeclCXX.cpp | 5 ++++-
clang/test/Parser/cxx0x-attributes.cpp | 4 ++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 893989bd2398f..c9d133f5e341c 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -749,7 +749,10 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
return nullptr;
}
- ProhibitAttributes(PrefixAttrs);
+ SourceLocation MisplacedAttrFixItLoc;
+ if (PrefixAttrs.hasAttribute(ParsedAttr::AT_Unused))
+ MisplacedAttrFixItLoc = Tok.getLocation();
+ ProhibitAttributes(PrefixAttrs, MisplacedAttrFixItLoc);
Decl *DeclFromDeclSpec = nullptr;
Scope *CurScope = getCurScope();
diff --git a/clang/test/Parser/cxx0x-attributes.cpp b/clang/test/Parser/cxx0x-attributes.cpp
index 220fddb98127a..2f3329d558dca 100644
--- a/clang/test/Parser/cxx0x-attributes.cpp
+++ b/clang/test/Parser/cxx0x-attributes.cpp
@@ -200,6 +200,10 @@ using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applie
using V = int; // expected-note {{previous}}
using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}}
+void using_alias_in_block() {
+ [[maybe_unused]] using BlockAlias = int; // expected-error {{misplaced attributes; expected attributes here}}
+}
+
auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}}
auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}}
auto trailing() -> const int [[]];
More information about the cfe-commits
mailing list