Bug 1475882 - clang-tidy: Enable misc-string-constructor check. r=andi

Finds string constructors that are suspicious and probably errors. There are currently no misc-string-constructor warnings in mozilla-central!

https://clang.llvm.org/extra/clang-tidy/checks/bugprone-string-constructor.html

MozReview-Commit-ID: LyJt6wqOhg9

--HG--
extra : source : 0b36b6b1b7931adec5846086a52080edb3ec5e7d
extra : histedit_source : 30249980b219d4813fc5503c87e84265ad354e2f
This commit is contained in:
Chris Peterson 2018-07-14 23:41:55 -07:00
parent 59cd83d07a
commit 126761ba3c
4 changed files with 23 additions and 1 deletions

View File

@ -54,6 +54,8 @@ clang_checkers:
publish: !!bool yes
- name: misc-macro-repeated-side-effects
publish: !!bool yes
- name: misc-string-constructor
publish: !!bool yes
- name: misc-suspicious-missing-comma
publish: !!bool yes
- name: misc-suspicious-semicolon

View File

@ -0,0 +1,17 @@
// https://clang.llvm.org/extra/clang-tidy/checks/bugprone-string-constructor.html
#include "structures.h"
void test()
{
// A common mistake is to swap parameters to the fill string-constructor.
std::string str('x', 50); // should be str(50, 'x')
// Calling the string-literal constructor with a length bigger than the
// literal is suspicious and adds extra random characters to the string.
std::string("test", 200); // Will include random characters after "test".
// Creating an empty string from constructors with parameters is considered
// suspicious. The programmer should use the empty constructor instead.
std::string("test", 0); // Creation of an empty string.
}

View File

@ -0,0 +1 @@
"[[\"warning\", \"string constructor parameters are probably swapped; expecting string(count, character)\", \"misc-string-constructor\"], [\"warning\", \"length is bigger then string literal size\", \"misc-string-constructor\"], [\"warning\", \"constructor creating an empty string\", \"misc-string-constructor\"]]"

View File

@ -37,7 +37,9 @@ class basic_string {
public:
typedef basic_string<T> _Type;
basic_string() {}
basic_string(const T *p);
basic_string(const T *p);
basic_string(const T *p, size_t count);
basic_string(size_t count, char ch);
~basic_string() {}
size_t size() const;
bool empty() const;