[clang-tidy] Add check readability-implicit-bool-cast
Summary:
This is another check that I ported to clang-tidy from colobot-lint tool.
As previously discussed on cfe-dev mailing list, this is one of those
checks that I think is general and useful enough for contribution to
clang-tidy.
This patch contains implementation of check taken from colobot-lint, but
it is extended a great deal, including FixIt hints for automated
refactoring, exhaustive testcases, and user documentation.
Reviewers: sbenza, aaron.ballman, alexfh
Subscribers: Eugene.Zelenko
Differential Revision: http://reviews.llvm.org/D13635
llvm-svn: 251235
diff --git a/clang-tools-extra/test/clang-tidy/readability-implicit-bool-cast-cxx98.cpp b/clang-tools-extra/test/clang-tidy/readability-implicit-bool-cast-cxx98.cpp
new file mode 100644
index 0000000..1cd5c87
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/readability-implicit-bool-cast-cxx98.cpp
@@ -0,0 +1,42 @@
+// RUN: clang-tidy %s -checks=-*,readability-implicit-bool-cast -- -std=c++98
+
+#include <cstddef> // for NULL
+
+template<typename T>
+void functionTaking(T);
+
+struct Struct {
+ int member;
+};
+
+void useOldNullMacroInReplacements() {
+ int* pointer = NULL;
+ functionTaking<bool>(pointer);
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit cast 'int *' -> bool [readability-implicit-bool-cast]
+ // CHECK-FIXES: functionTaking<bool>(pointer != 0);
+
+ int Struct::* memberPointer = NULL;
+ functionTaking<bool>(!memberPointer);
+ // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit cast 'int struct Struct::*' -> bool
+ // CHECK-FIXES: functionTaking<bool>(memberPointer == 0);
+}
+
+void fixFalseLiteralConvertingToNullPointer() {
+ functionTaking<int*>(false);
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit cast bool -> 'int *'
+ // CHECK-FIXES: functionTaking<int*>(0);
+
+ int* pointer = NULL;
+ if (pointer == false) {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: implicit cast bool -> 'int *'
+ // CHECK-FIXES: if (pointer == 0) {}
+
+ functionTaking<int Struct::*>(false);
+ // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit cast bool -> 'int struct Struct::*'
+ // CHECK-FIXES: functionTaking<int Struct::*>(0);
+
+ int Struct::* memberPointer = NULL;
+ if (memberPointer != false) {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit cast bool -> 'int struct Struct::*'
+ // CHECK-FIXES: if (memberPointer != 0) {}
+}