Skip to content

Commit a76ee00

Browse files
committed
[Clang] emit -Wunused-variable warning for unused structured bindings without the [[maybe_unused]] attribute
1 parent bcba311 commit a76ee00

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
@@ -160,6 +160,8 @@ Bug Fixes to C++ Support
160160
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
161161
- The initialization kind of elements of structured bindings
162162
direct-list-initialized from an array is corrected to direct-initialization.
163+
- Clang now emits the ``-Wunused-variable`` warning when some structured bindings are unused
164+
and the ``[[maybe_unused]]`` attribute is not applied. (#GH125810)
163165

164166
Bug Fixes to AST Handling
165167
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,13 +1921,14 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
19211921
// For a decomposition declaration, warn if none of the bindings are
19221922
// referenced, instead of if the variable itself is referenced (which
19231923
// it is, by the bindings' expressions).
1924-
bool IsAllPlaceholders = true;
1924+
bool IsAllIgnored = true;
19251925
for (const auto *BD : DD->bindings()) {
1926-
if (BD->isReferenced() || BD->hasAttr<UnusedAttr>())
1926+
if (BD->isReferenced())
19271927
return false;
1928-
IsAllPlaceholders = IsAllPlaceholders && BD->isPlaceholderVar(LangOpts);
1928+
IsAllIgnored = IsAllIgnored && (BD->isPlaceholderVar(LangOpts) ||
1929+
BD->hasAttr<UnusedAttr>());
19291930
}
1930-
if (IsAllPlaceholders)
1931+
if (IsAllIgnored)
19311932
return false;
19321933
} else if (!D->getDeclName()) {
19331934
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++2c -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)