-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[libc++][char_traits] Applied [[nodiscard]]
#172244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
frederick-vs-ja
merged 1 commit into
llvm:main
from
frederick-vs-ja:nodiscard-char_traits
Dec 15, 2025
+206
−31
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
libcxx/test/libcxx/strings/char.traits/nodiscard.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // Check that functions are marked [[nodiscard]] | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "test_macros.h" | ||
|
|
||
| void test_char() { | ||
| typedef char char_t; | ||
| typedef std::char_traits<char_t> traits; | ||
| typedef typename traits::int_type int_t; | ||
|
|
||
| const char_t buf[1] = {}; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq(char_t(), char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::lt(char_t(), char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::compare(buf, buf, 0); | ||
| traits::length(buf); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::find(buf, 0, char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::not_eof(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_char_type(int_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_int_type(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq_int_type(int_t(), int_t()); | ||
| traits::eof(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
|
|
||
| #ifndef TEST_HAS_NO_CHAR8_T | ||
| void test_char8_t() { | ||
|
Comment on lines
+44
to
+45
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for review: I attempted to use a function template to handle five character types. However, the count of expected warnings varies in different modes, and it's infeasible to write the different counts in comments. As a result, I chose to repeat the function body 5 times. |
||
| typedef char8_t char_t; | ||
| typedef std::char_traits<char_t> traits; | ||
| typedef typename traits::int_type int_t; | ||
|
|
||
| const char_t buf[1] = {}; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq(char_t(), char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::lt(char_t(), char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::compare(buf, buf, 0); | ||
| traits::length(buf); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::find(buf, 0, char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::not_eof(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_char_type(int_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_int_type(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq_int_type(int_t(), int_t()); | ||
| traits::eof(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| #endif | ||
|
|
||
| void test_char16_t() { | ||
| typedef char16_t char_t; | ||
| typedef std::char_traits<char_t> traits; | ||
| typedef typename traits::int_type int_t; | ||
|
|
||
| const char_t buf[1] = {}; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq(char_t(), char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::lt(char_t(), char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::compare(buf, buf, 0); | ||
| traits::length(buf); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::find(buf, 0, char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::not_eof(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_char_type(int_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_int_type(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq_int_type(int_t(), int_t()); | ||
| traits::eof(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
|
|
||
| void test_char32_t() { | ||
| typedef char32_t char_t; | ||
| typedef std::char_traits<char_t> traits; | ||
| typedef typename traits::int_type int_t; | ||
|
|
||
| const char_t buf[1] = {}; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq(char_t(), char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::lt(char_t(), char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::compare(buf, buf, 0); | ||
| traits::length(buf); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::find(buf, 0, char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::not_eof(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_char_type(int_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_int_type(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq_int_type(int_t(), int_t()); | ||
| traits::eof(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
|
|
||
| #ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
| void test_wchar_t() { | ||
| typedef wchar_t char_t; | ||
| typedef std::char_traits<char_t> traits; | ||
| typedef typename traits::int_type int_t; | ||
|
|
||
| const char_t buf[1] = {}; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq(char_t(), char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::lt(char_t(), char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::compare(buf, buf, 0); | ||
| traits::length(buf); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::find(buf, 0, char_t()); | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::not_eof(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_char_type(int_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::to_int_type(char_t()); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| traits::eq_int_type(int_t(), int_t()); | ||
| traits::eof(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for review: Non-uglified
nodiscardoccurrences can only appear since C++20 currently, while other uglified__nodiscard__occurrences can appear in C++03.