[clang] [clang] Add the candiscard attribute to suppress nodiscard (PR #154943)
Corentin Jabot via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 7 05:43:01 PDT 2025
================
@@ -489,6 +567,44 @@ void test() {
from_a_template<NoDiscard>(); // expected-warning {{ignoring return value of type 'NoDiscard' declared with 'nodiscard' attribute}}
from_a_template<WarnUnused>(); // no warning
from_a_template<WarnUnusedResult>(); // expected-warning {{ignoring return value of type 'WarnUnusedResult' declared with 'gnu::warn_unused_result' attribute}}
+
+ // In a template instantiation the information about the typedef is lost,
+ // so the candiscard attribute is lost, so the diagnostic is not suppressed
+ from_a_template<NoDIgnored>(); // expected-warning {{ignoring return value of type 'NoDiscard' declared with 'nodiscard' attribute}}
+ from_a_template<WUIgnored>(); // no warning
+ from_a_template<WURIgnored>(); // expected-warning {{ignoring return value of type 'WarnUnusedResult' declared with 'gnu::warn_unused_result' attribute}}
+
+ from_a_template_ignored<NoDiscard>(); // no warning
+ from_a_template_ignored<WarnUnused>(); // no warning
+ from_a_template_ignored<WarnUnusedResult>(); // no warning
+
+ return_nodint(); // expected-warning {{ignoring return value of type 'NoDInt' declared with 'clang::warn_unused_result' attribute}}
+ return_nodint_ignored(); // no warning
+ return_nodclass(); // expected-warning {{ignoring return value of type 'NoDClass' declared with 'nodiscard' attribute}}
+ return_nodclass_ignored(); // no warning
+
+ // In an alias template the information about the typedef is lost,
+ // so the diagnostic is not issued. GH#68456
+ return_nodtemplate();
+
+ // In an alias template the information about the typedef is lost,
+ // so the diagnostic is not suppressed. GH#68456
+ return_nodint2(); // expected-warning {{ignoring return value of type 'NoDInt' declared with 'clang::warn_unused_result' attribute}}
+ return_nodint_ignored2(); // expected-warning {{ignoring return value of type 'NoDInt' declared with 'clang::warn_unused_result' attribute}}
+ return_nodclass2(); // expected-warning {{ignoring return value of type 'NoDClass' declared with 'nodiscard' attribute}}
+ return_nodclass_ignored2(); // expected-warning {{ignoring return value of type 'NoDClass' declared with 'nodiscard' attribute}}
+}
+
+using BothAttributes [[clang::warn_unused_result, clang::candiscard]] = int;
+
+BothAttributes return_bothattributes1();
+[[nodiscard, clang::candiscard]] int return_bothattributes2();
+[[nodiscard]] NoDIgnored return_nodignored_nodiscard();
+
+void testBothAttributes() {
+ return_bothattributes1(); // no warning because candiscard takes priority
+ return_bothattributes2(); // no warning because candiscard takes priority
----------------
cor3ntin wrote:
Why though? Have you considered warning in this case?
https://github.com/llvm/llvm-project/pull/154943
More information about the cfe-commits
mailing list