[clang] [Clang] emit -Wunused-variable warning for unused structured bindings without the [[maybe_unused]] attribute (PR #127061)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 20 15:17:58 PST 2025
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/127061
>From a76ee008bdb87655da465e21d09c840edecc2b1b Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 13 Feb 2025 15:24:09 +0200
Subject: [PATCH 1/2] [Clang] emit -Wunused-variable warning for unused
structured bindings without the [[maybe_unused]] attribute
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaDecl.cpp | 9 +++++----
clang/test/SemaCXX/unused-bindings.cpp | 17 +++++++++++++++++
clang/test/SemaCXX/unused.cpp | 3 ++-
4 files changed, 26 insertions(+), 5 deletions(-)
create mode 100644 clang/test/SemaCXX/unused-bindings.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6344c4b36e357..4f20415ec006d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -160,6 +160,8 @@ Bug Fixes to C++ Support
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
- The initialization kind of elements of structured bindings
direct-list-initialized from an array is corrected to direct-initialization.
+- Clang now emits the ``-Wunused-variable`` warning when some structured bindings are unused
+ and the ``[[maybe_unused]]`` attribute is not applied. (#GH125810)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6eedc77ed20a0..19a73a66be5af 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1921,13 +1921,14 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
// For a decomposition declaration, warn if none of the bindings are
// referenced, instead of if the variable itself is referenced (which
// it is, by the bindings' expressions).
- bool IsAllPlaceholders = true;
+ bool IsAllIgnored = true;
for (const auto *BD : DD->bindings()) {
- if (BD->isReferenced() || BD->hasAttr<UnusedAttr>())
+ if (BD->isReferenced())
return false;
- IsAllPlaceholders = IsAllPlaceholders && BD->isPlaceholderVar(LangOpts);
+ IsAllIgnored = IsAllIgnored && (BD->isPlaceholderVar(LangOpts) ||
+ BD->hasAttr<UnusedAttr>());
}
- if (IsAllPlaceholders)
+ if (IsAllIgnored)
return false;
} else if (!D->getDeclName()) {
return false;
diff --git a/clang/test/SemaCXX/unused-bindings.cpp b/clang/test/SemaCXX/unused-bindings.cpp
new file mode 100644
index 0000000000000..01f2126133a20
--- /dev/null
+++ b/clang/test/SemaCXX/unused-bindings.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2c -Wunused %s
+
+namespace GH125810 {
+struct S {
+ int a, b;
+};
+
+void t(S s) {
+ auto &[_, _] = s;
+ auto &[a1, _] = s; // expected-warning {{unused variable '[a1, _]'}}
+ auto &[_, b2] = s; // expected-warning {{unused variable '[_, b2]'}}
+
+ auto &[a3 [[maybe_unused]], b3 [[maybe_unused]]] = s;
+ auto &[a4, b4 [[maybe_unused]]] = s; // expected-warning {{unused variable '[a4, b4]'}}
+ auto &[a5 [[maybe_unused]], b5] = s; // expected-warning {{unused variable '[a5, b5]'}}
+}
+}
diff --git a/clang/test/SemaCXX/unused.cpp b/clang/test/SemaCXX/unused.cpp
index 1f40c1b1ca903..ab728069f2faf 100644
--- a/clang/test/SemaCXX/unused.cpp
+++ b/clang/test/SemaCXX/unused.cpp
@@ -114,7 +114,8 @@ namespace maybe_unused_binding {
void test() {
struct X { int a, b; } x;
- auto [a [[maybe_unused]], b] = x; // expected-warning {{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
+ auto [a [[maybe_unused]], b] = x; // expected-warning {{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}} \
+ // expected-warning {{unused variable '[a, b]'}}
}
}
>From c600c2a2378310c3f2fc82edaca66ca80b40284d Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 14 Feb 2025 13:43:53 +0200
Subject: [PATCH 2/2] update test to use std c++26
---
clang/test/SemaCXX/unused-bindings.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/unused-bindings.cpp b/clang/test/SemaCXX/unused-bindings.cpp
index 01f2126133a20..83250dd4b0a11 100644
--- a/clang/test/SemaCXX/unused-bindings.cpp
+++ b/clang/test/SemaCXX/unused-bindings.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2c -Wunused %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++26 -Wunused %s
namespace GH125810 {
struct S {
More information about the cfe-commits
mailing list