[PATCH] [clang-tidy] Add a checker that warns on const string & members.
Benjamin Kramer
benny.kra at gmail.com
Wed Jul 16 02:44:15 PDT 2014
================
Comment at: clang-tidy/google/CMakeLists.txt:8
@@ -7,2 +7,3 @@
GoogleTidyModule.cpp
+ MemberStringReferencesCheck.cpp
NamedParameterCheck.cpp
----------------
Daniel Jasper wrote:
> I'd probably call this StringReferenceMemberCheck, but I don't have a strong objection against this name.
Less backwards that suggestion is, rename the file I will.
================
Comment at: clang-tidy/google/GoogleTidyModule.cpp:35
@@ -33,1 +34,3 @@
CheckFactories.addCheckFactory(
+ "google-runtime-member-string-references",
+ new ClangTidyCheckFactory<runtime::MemberStringReferencesCheck>());
----------------
Daniel Jasper wrote:
> Where does "runtime" come from? Does that make sense?
We inherited the categories from cpplint.py and I've been following the existing style. I think it makes sense to have a direct mapping from cpplint categories to tidy checks.
================
Comment at: clang-tidy/google/MemberStringReferencesCheck.h:22
@@ +21,3 @@
+/// const string reference members are generally considered unsafe. This check
+/// emit warnings for both std::string and ::string const reference members.
+///
----------------
Daniel Jasper wrote:
> Just for my benefit, why are non-const string reference members ok?
The rationale is that you can initialize the following class with a const char *, creating a string temporary and leave a dangling reference. This behavior won't come up with non-const refs.
```
struct S {
S(const string &S) : Str(S) {}
const string &Str;
};
```
I'll add that info to the checker description comment.
http://reviews.llvm.org/D4522
More information about the cfe-commits
mailing list