Skip to content

Commit 7f6405e

Browse files
committed
fix review
1 parent 824f287 commit 7f6405e

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@ namespace {
1919

2020
// check if the type is unsigned char or signed char
2121
AST_MATCHER(Type, isNumericChar) {
22-
const auto *BT = dyn_cast<BuiltinType>(&Node);
23-
if (BT == nullptr)
24-
return false;
25-
const BuiltinType::Kind K = BT->getKind();
26-
return K == BuiltinType::UChar || K == BuiltinType::SChar;
22+
return Node.isSpecificBuiltinType(BuiltinType::SChar) ||
23+
Node.isSpecificBuiltinType(BuiltinType::UChar);
2724
}
2825

2926
// check if the type is char
3027
AST_MATCHER(Type, isChar) {
31-
const auto *BT = dyn_cast<BuiltinType>(&Node);
32-
if (BT == nullptr)
33-
return false;
34-
const BuiltinType::Kind K = BT->getKind();
35-
return K == BuiltinType::Char_U || K == BuiltinType::Char_S;
28+
return Node.isSpecificBuiltinType(BuiltinType::Char_S) ||
29+
Node.isSpecificBuiltinType(BuiltinType::Char_U);
3630
}
3731

3832
} // namespace

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ New checks
9595
<clang-tidy/checks/bugprone/unintended-char-ostream-output>` check.
9696

9797
Finds unintended character output from ``unsigned char`` and ``signed char`` to an
98-
ostream.
98+
``ostream``.
9999

100100
New check aliases
101101
^^^^^^^^^^^^^^^^^

clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ integer values.
1414

1515
.. code-block:: c++
1616

17-
uint8_t v = 9;
18-
std::cout << v; // output '\t' instead of '9'
17+
uint8_t v = 65;
18+
std::cout << v; // output 'A' instead of '65'
1919

2020
It could be fixed as
2121

clang-tools-extra/test/clang-tidy/checkers/bugprone/unintended-char-ostream-output.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ class A : public std::ostream {};
3030
void origin_ostream(std::ostream &os) {
3131
unsigned char unsigned_value = 9;
3232
os << unsigned_value;
33-
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'unsigned char' passed to
34-
// 'operator<<' outputs as character instead of integer
33+
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'unsigned char' passed to 'operator<<' outputs as character instead of integer
3534

3635
signed char signed_value = 9;
3736
os << signed_value;
38-
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'signed char' passed to
39-
// 'operator<<' outputs as character instead of integer
37+
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'signed char' passed to 'operator<<' outputs as character instead of integer
4038

4139
char char_value = 9;
4240
os << char_value;
@@ -45,13 +43,11 @@ void origin_ostream(std::ostream &os) {
4543
void based_on_ostream(A &os) {
4644
unsigned char unsigned_value = 9;
4745
os << unsigned_value;
48-
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'unsigned char' passed to
49-
// 'operator<<' outputs as character instead of integer
46+
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'unsigned char' passed to 'operator<<' outputs as character instead of integer
5047

5148
signed char signed_value = 9;
5249
os << signed_value;
53-
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'signed char' passed to
54-
// 'operator<<' outputs as character instead of integer
50+
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'signed char' passed to 'operator<<' outputs as character instead of integer
5551

5652
char char_value = 9;
5753
os << char_value;
@@ -67,3 +63,11 @@ void based_on_ostream(std::basic_ostream<unsigned char> &os) {
6763
char char_value = 9;
6864
os << char_value;
6965
}
66+
67+
template <class T> class B : public std::ostream {};
68+
void template_based_on_ostream(B<int> &os) {
69+
unsigned char unsigned_value = 9;
70+
os << unsigned_value;
71+
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'unsigned char' passed to 'operator<<' outputs as character instead of integer
72+
}
73+

0 commit comments

Comments
 (0)