summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2014-07-11 08:08:47 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2014-07-11 08:08:47 +0000
commit1c8b31753b0699960d314a67dba2c71ba832e6ae (patch)
treebaa10f2e41cb0f56787cb9fe1776011537c8211b /clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
parent780ce0f8e3505e7a3df0ee5f5d421d8e0e253079 (diff)
downloadbcm5719-llvm-1c8b31753b0699960d314a67dba2c71ba832e6ae.tar.gz
bcm5719-llvm-1c8b31753b0699960d314a67dba2c71ba832e6ae.zip
[clang-tidy] Add a checker for implicit bool conversion of a bool*.
The goal is to find code like the example below, which is likely a typo where someone meant to write "if (*b)". bool *b = SomeFunction(); if (b) { // b never dereferenced } This checker naturally has a relatively high false positive rate so it applies some heuristics to avoid cases where the pointer is checked for nullptr before being written. Differential Revision: http://reviews.llvm.org/D4458 llvm-svn: 212797
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp b/clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
new file mode 100644
index 00000000000..98cfd667242
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
@@ -0,0 +1,86 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-bool-pointer-implicit-conversion %t
+// REQUIRES: shell
+
+bool *SomeFunction();
+void SomeOtherFunction(bool*);
+bool F();
+void G(bool);
+
+
+template <typename T>
+void t(T b) {
+ if (b) {
+ }
+}
+
+void foo() {
+ bool *b = SomeFunction();
+ if (b) {
+// CHECK-MESSAGES: dubious check of 'bool *' against 'nullptr'
+// CHECK-FIXES: if (*b) {
+ }
+
+ if (F() && b) {
+// CHECK-MESSAGES: dubious check of 'bool *' against 'nullptr'
+// CHECK-FIXES: if (F() && *b) {
+ }
+
+ // TODO: warn here.
+ if (b) {
+ G(b);
+ }
+
+#define TESTMACRO if (b || F())
+
+ TESTMACRO {
+// CHECK-MESSAGES: dubious check of 'bool *' against 'nullptr'
+// Can't fix this.
+// CHECK-FIXES: #define TESTMACRO if (b || F())
+// CHECK-FIXES: TESTMACRO {
+ }
+
+// CHECK-MESSAGES-NOT: warning:
+
+ t(b);
+
+ if (!b) {
+ // no-warning
+ }
+
+ if (SomeFunction()) {
+ // no-warning
+ }
+
+ bool *c = SomeFunction();
+ if (c) {
+ (void)c;
+ (void)*c; // no-warning
+ }
+
+ if (c) {
+ *c = true; // no-warning
+ }
+
+ if (c) {
+ c[0] = false; // no-warning
+ }
+
+ if (c) {
+ SomeOtherFunction(c); // no-warning
+ }
+
+ if (c) {
+ delete[] c; // no-warning
+ }
+
+ if (c) {
+ *(c) = false; // no-warning
+ }
+
+ struct {
+ bool *b;
+ } d = { SomeFunction() };
+
+ if (d.b)
+ (void)*d.b; // no-warning
+}
OpenPOWER on IntegriCloud