Bug 1475882 - clang-tidy: Enable misc-string-integer-assignment check. r?andi draft
authorChris Peterson <cpeterson@mozilla.com>
Sat, 14 Jul 2018 23:48:40 -0700
changeset 819048 816f0f6ded5d2651258f6b8dcb77568b91aa7e52
parent 819047 6d4e44b524c7d2f883076968877886a623f1e058
child 819049 d46d4695a4795be9d9ac50754bdaa441f0928d62
push id116421
push usercpeterson@mozilla.com
push dateTue, 17 Jul 2018 01:36:52 +0000
reviewersandi
bugs1475882
milestone63.0a1
Bug 1475882 - clang-tidy: Enable misc-string-integer-assignment check. r?andi The check finds assignments of an integer to std::string: https://clang.llvm.org/extra/clang-tidy/checks/bugprone-string-integer-assignment.html There are currently ten misc-string-integer-assignment warnings in mozilla-central, but they are all in third-party Breakpad code: toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:306:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:307:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:309:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:310:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:311:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:313:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:314:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:315:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility toolkit/crashreporter/google-breakpad/src/processor/minidump.cc:316:17: warning: an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility MozReview-Commit-ID: AUOyOuCzy1R
tools/clang-tidy/config.yaml
tools/clang-tidy/test/misc-string-integer-assignment.cpp
tools/clang-tidy/test/misc-string-integer-assignment.json
tools/clang-tidy/test/structures.h
--- a/tools/clang-tidy/config.yaml
+++ b/tools/clang-tidy/config.yaml
@@ -51,16 +51,18 @@ clang_checkers:
   - name: misc-forward-declaration-namespace
     # Name with clang tidy 6.0. We are currently using 5.0
     # - name: bugprone-forward-declaration-namespace
     publish: !!bool yes
   - name: misc-macro-repeated-side-effects
     publish: !!bool yes
   - name: misc-string-constructor
     publish: !!bool yes
+  - name: misc-string-integer-assignment
+    publish: !!bool yes
   - name: misc-suspicious-missing-comma
     publish: !!bool yes
   - name: misc-suspicious-semicolon
     publish: !!bool yes
   - name: misc-unused-using-decls
     publish: !!bool yes
   - name: modernize-avoid-bind
     publish: !!bool yes
new file mode 100644
--- /dev/null
+++ b/tools/clang-tidy/test/misc-string-integer-assignment.cpp
@@ -0,0 +1,28 @@
+// https://clang.llvm.org/extra/clang-tidy/checks/bugprone-string-integer-assignment.html
+
+#include "structures.h"
+
+void test_int()
+{
+  // Numeric types can be implicitly casted to character types.
+  std::string s;
+  int x = 5965;
+  s = 6; // warning
+  s = x; // warning
+}
+
+void test_conversion()
+{
+  // Use the appropriate conversion functions or character literals.
+  std::string s;
+  int x = 5965;
+  s = '6'; // OK
+  s = std::to_string(x); // OK
+}
+
+void test_cast()
+{
+  // In order to suppress false positives, use an explicit cast.
+  std::string s;
+  s = static_cast<char>(6); // OK
+}
new file mode 100644
--- /dev/null
+++ b/tools/clang-tidy/test/misc-string-integer-assignment.json
@@ -0,0 +1,1 @@
+"[[\"warning\", \"an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility\", \"misc-string-integer-assignment\"], [\"warning\", \"an integer is interpreted as a character code when assigning it to a string; if this is intended, cast the integer to the appropriate character type; if you want a string representation, use the appropriate conversion facility\", \"misc-string-integer-assignment\"]]"
\ No newline at end of file
--- a/tools/clang-tidy/test/structures.h
+++ b/tools/clang-tidy/test/structures.h
@@ -41,22 +41,25 @@ public:
   basic_string(const T *p, size_t count);
   basic_string(size_t count, char ch);
   ~basic_string() {}
   size_t size() const;
   bool empty() const;
   size_t find (const char* s, size_t pos = 0) const;
   const T *c_str() const;
   _Type& assign(const T *s);
+  basic_string<T> &operator=(T ch);
   basic_string<T> *operator+=(const basic_string<T> &) {}
   friend basic_string<T> operator+(const basic_string<T> &, const basic_string<T> &) {}
 };
 typedef basic_string<char> string;
 typedef basic_string<wchar_t> wstring;
 
+string to_string(int value);
+
 template <typename T>
 struct default_delete {};
 
 template <typename T, typename D = default_delete<T>>
 class unique_ptr {
  public:
   unique_ptr();
   ~unique_ptr();