]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add [[nodiscard]] to some std::locale functions
authorJonathan Wakely <jwakely@redhat.com>
Wed, 22 May 2024 17:38:36 +0000 (18:38 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 22 May 2024 22:11:32 +0000 (23:11 +0100)
libstdc++-v3/ChangeLog:

* include/bits/locale_classes.h (locale::combine)
(locale::name, locale::operator==, locale::operator!=)
(locale::operator(), locale::classic): Add nodiscard
attribute.
* include/bits/locale_classes.tcc (has_facet, use_facet):
Likewise.
* testsuite/22_locale/locale/cons/12438.cc: Add dg-warning for
nodiscard diagnostic.
* testsuite/22_locale/locale/cons/2.cc: Cast use_facet
expression to void, to suppress diagnostic.
* testsuite/22_locale/locale/cons/unicode.cc: Likewise.
* testsuite/22_locale/locale/operations/2.cc: Add dg-warning.

libstdc++-v3/include/bits/locale_classes.h
libstdc++-v3/include/bits/locale_classes.tcc
libstdc++-v3/testsuite/22_locale/locale/cons/12438.cc
libstdc++-v3/testsuite/22_locale/locale/cons/2.cc
libstdc++-v3/testsuite/22_locale/locale/cons/unicode.cc
libstdc++-v3/testsuite/22_locale/locale/operations/2.cc

index a2e94217006ba07cde44ec8e6c963b3abf075c43..50a748066f190866a5b0cd49c48a116e115a35c9 100644 (file)
@@ -240,6 +240,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  @throw  std::runtime_error if __other has no facet of type _Facet.
     */
     template<typename _Facet>
+      _GLIBCXX_NODISCARD
       locale
       combine(const locale& __other) const;
 
@@ -248,7 +249,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  @brief  Return locale name.
      *  @return  Locale name or "*" if unnamed.
     */
-    _GLIBCXX_DEFAULT_ABI_TAG
+    _GLIBCXX_NODISCARD _GLIBCXX_DEFAULT_ABI_TAG
     string
     name() const;
 
@@ -269,6 +270,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  @return  True if other and this refer to the same locale instance, are
      *          copies, or have the same name.  False otherwise.
     */
+    _GLIBCXX_NODISCARD
     bool
     operator==(const locale& __other) const throw();
 
@@ -279,6 +281,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  @param  __other  The locale to compare against.
      *  @return  ! (*this == __other)
     */
+    _GLIBCXX_NODISCARD
     bool
     operator!=(const locale& __other) const throw()
     { return !(this->operator==(__other)); }
@@ -300,6 +303,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  @return  True if collate<_Char> facet compares __s1 < __s2, else false.
     */
     template<typename _Char, typename _Traits, typename _Alloc>
+      _GLIBCXX_NODISCARD
       bool
       operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
                 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
@@ -321,6 +325,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     /**
      *  @brief  Return reference to the C locale.
     */
+    _GLIBCXX_NODISCARD
     static const locale&
     classic();
 
index 00eeb7dd9f8951c78e0d229ba509658eda6971a9..c79574e58de87fbf76f21624f711e75b0f4c2148 100644 (file)
@@ -173,6 +173,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @return  true if @p __loc contains a facet of type _Facet, else false.
   */
   template<typename _Facet>
+    _GLIBCXX_NODISCARD
     inline bool
     has_facet(const locale& __loc) _GLIBCXX_USE_NOEXCEPT
     {
@@ -202,6 +203,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wdangling-reference"
   template<typename _Facet>
+    _GLIBCXX_NODISCARD
     inline const _Facet&
     use_facet(const locale& __loc)
     {
index 7ff3a487745dcab0fce2c9f7b5a5ed17ecbe43e4..4838e1ba6936e07585e6012897ed7c1d4e7adce9 100644 (file)
@@ -45,7 +45,7 @@ void test01(int iters)
          locale loc2 = locale("");
          VERIFY( !has_facet<MyFacet>(loc2) );
          
-         loc1.combine<MyFacet>(loc2);
+         loc1.combine<MyFacet>(loc2); // { dg-warning "nodiscard" "" { target c++17 } }
          VERIFY( false );
        }
       catch (std::runtime_error&)
index 12478dbfdc2bd7e3b83409545e2fd99a21170eb0..dce150effea14b5e529cb1d556bcd1be3cfca2ce 100644 (file)
@@ -68,7 +68,7 @@ void test01()
     { VERIFY( false ); }
 
   try 
-    { use_facet<gnu_facet>(loc02); }
+    { (void) use_facet<gnu_facet>(loc02); }
   catch(bad_cast& obj)
     { VERIFY( true ); }
   catch(...)
index 24af4142cd938ea1d3dad67c9bc00d2040b6817c..98d744de91e1ccda5f4595a9900fa88b9f905503 100644 (file)
@@ -79,7 +79,7 @@ void test01()
     { VERIFY( false ); }
 
   try 
-    { use_facet<gnu_facet>(loc13); }
+    { (void) use_facet<gnu_facet>(loc13); }
   catch(bad_cast& obj)
     { VERIFY( true ); }
   catch(...)
index 899535137ba3d29241764dc99d10f181b0a12fe7..917adecac56ef485c7c3df9ba4a02a19d9d6c3e6 100644 (file)
@@ -48,7 +48,7 @@ void test02()
   // Derivation, MF check.
   locale loc_gnu(loc_c, new gnu_collate);
   gnu_count = 0;
-  loc_gnu(s01, s02);
+  loc_gnu(s01, s02); // { dg-warning "nodiscard" "" { target c++17 } }
   VERIFY( gnu_count == 1 );
 }