Skip to content

Commit 01d28c1

Browse files
authored
[Clang] emit -Wunused-variable warning for unused structured bindings without the [[maybe_unused]] attribute (#127061)
Fixes #125810 --- This patch resolves an issue in Clang where the `-Wunused-variable` warning was suppressed for structured bindings with elements marked `[[maybe_unused]]`, causing the entire declaration to be treated as used and preventing the warning from being emitted.
1 parent a306ae0 commit 01d28c1

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ Bug Fixes to C++ Support
348348
- Correctly diagnoses if unresolved using declarations shadows template paramters (#GH129411)
349349
- Clang was previously coalescing volatile writes to members of volatile base class subobjects.
350350
The issue has been addressed by propagating qualifiers during derived-to-base conversions in the AST. (#GH127824)
351+
- Clang now emits the ``-Wunused-variable`` warning when some structured bindings are unused
352+
and the ``[[maybe_unused]]`` attribute is not applied. (#GH125810)
351353

352354
Bug Fixes to AST Handling
353355
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,13 +1934,14 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
19341934
// For a decomposition declaration, warn if none of the bindings are
19351935
// referenced, instead of if the variable itself is referenced (which
19361936
// it is, by the bindings' expressions).
1937-
bool IsAllPlaceholders = true;
1937+
bool IsAllIgnored = true;
19381938
for (const auto *BD : DD->bindings()) {
1939-
if (BD->isReferenced() || BD->hasAttr<UnusedAttr>())
1939+
if (BD->isReferenced())
19401940
return false;
1941-
IsAllPlaceholders = IsAllPlaceholders && BD->isPlaceholderVar(LangOpts);
1941+
IsAllIgnored = IsAllIgnored && (BD->isPlaceholderVar(LangOpts) ||
1942+
BD->hasAttr<UnusedAttr>());
19421943
}
1943-
if (IsAllPlaceholders)
1944+
if (IsAllIgnored)
19441945
return false;
19451946
} else if (!D->getDeclName()) {
19461947
return false;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++26 -Wunused %s
2+
3+
namespace GH125810 {
4+
struct S {
5+
int a, b;
6+
};
7+
8+
void t(S s) {
9+
auto &[_, _] = s;
10+
auto &[a1, _] = s; // expected-warning {{unused variable '[a1, _]'}}
11+
auto &[_, b2] = s; // expected-warning {{unused variable '[_, b2]'}}
12+
13+
auto &[a3 [[maybe_unused]], b3 [[maybe_unused]]] = s;
14+
auto &[a4, b4 [[maybe_unused]]] = s; // expected-warning {{unused variable '[a4, b4]'}}
15+
auto &[a5 [[maybe_unused]], b5] = s; // expected-warning {{unused variable '[a5, b5]'}}
16+
}
17+
}

clang/test/SemaCXX/unused.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ namespace maybe_unused_binding {
114114

115115
void test() {
116116
struct X { int a, b; } x;
117-
auto [a [[maybe_unused]], b] = x; // expected-warning {{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
117+
auto [a [[maybe_unused]], b] = x; // expected-warning {{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}} \
118+
// expected-warning {{unused variable '[a, b]'}}
118119
}
119120

120121
}

0 commit comments

Comments
 (0)