[clang-tidy] Remove google-runtime-member-string-references

This is triggering on a pattern that's both too broad (const
std::string& members can be used safely) and too narrow (std::string is
not the only class with this problem). It has a very low true positive
rate, just remove it until we find a better solution for dangling string
references.

llvm-svn: 329292
This commit is contained in:
Benjamin Kramer 2018-04-05 14:51:01 +00:00
parent 92357a2336
commit be92ce14e1
9 changed files with 2 additions and 188 deletions

View File

@ -114,10 +114,6 @@ Checks:
Label: Find implementation-specific integral types
Description:
Name: google-runtime-int
- Category: Google Style Guide
Label: Find const string references
Description:
Name: google-runtime-member-string-references
- Category: Google Style Guide
Label: Find zero-length memsets
Description:

View File

@ -12,7 +12,6 @@ add_clang_library(clangTidyGoogleModule
IntegerTypesCheck.cpp
NonConstReferences.cpp
OverloadedUnaryAndCheck.cpp
StringReferenceMemberCheck.cpp
TodoCommentCheck.cpp
UnnamedNamespaceInHeaderCheck.cpp
UsingNamespaceDirectiveCheck.cpp

View File

@ -24,7 +24,6 @@
#include "IntegerTypesCheck.h"
#include "NonConstReferences.h"
#include "OverloadedUnaryAndCheck.h"
#include "StringReferenceMemberCheck.h"
#include "TodoCommentCheck.h"
#include "UnnamedNamespaceInHeaderCheck.h"
#include "UsingNamespaceDirectiveCheck.h"
@ -60,8 +59,6 @@ class GoogleModule : public ClangTidyModule {
"google-runtime-operator");
CheckFactories.registerCheck<runtime::NonConstReferences>(
"google-runtime-references");
CheckFactories.registerCheck<runtime::StringReferenceMemberCheck>(
"google-runtime-member-string-references");
CheckFactories.registerCheck<readability::AvoidCStyleCastsCheck>(
"google-readability-casting");
CheckFactories.registerCheck<readability::TodoCommentCheck>(

View File

@ -1,51 +0,0 @@
//===--- StringReferenceMemberCheck.cpp - clang-tidy ------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "StringReferenceMemberCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace runtime {
void StringReferenceMemberCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
// Only register the matchers for C++; the functionality currently does not
// provide any benefit to other languages, despite being benign.
if (!getLangOpts().CPlusPlus)
return;
// Look for const references to std::string or ::string.
auto String = anyOf(namedDecl(hasName("::std::string")),
namedDecl(hasName("::string")));
auto ConstString = qualType(isConstQualified(), hasDeclaration(String));
// Ignore members in template instantiations.
Finder->addMatcher(
fieldDecl(hasType(references(ConstString)), unless(isInstantiated()))
.bind("member"),
this);
}
void StringReferenceMemberCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Member = Result.Nodes.getNodeAs<FieldDecl>("member");
diag(Member->getLocStart(), "const string& members are dangerous; it is much "
"better to use alternatives, such as pointers or "
"simple constants");
}
} // namespace runtime
} // namespace google
} // namespace tidy
} // namespace clang

View File

@ -1,54 +0,0 @@
//===--- StringReferenceMemberCheck.h - clang-tidy ----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_STRINGREFERENCEMEMBERCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_STRINGREFERENCEMEMBERCHECK_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
namespace runtime {
/// Finds members of type `const string&`.
///
/// const string reference members are generally considered unsafe as they can
/// be created from a temporary quite easily.
///
/// \code
/// struct S {
/// S(const string &Str) : Str(Str) {}
/// const string &Str;
/// };
/// S instance("string");
/// \endcode
///
/// In the constructor call a string temporary is created from `const char *`
/// and destroyed immediately after the call. This leaves around a dangling
/// reference.
///
/// This check emit warnings for both `std::string` and `::string` const
/// reference members.
///
/// Corresponding cpplint.py check name: 'runtime/member_string_reference'.
class StringReferenceMemberCheck : public ClangTidyCheck {
public:
StringReferenceMemberCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace runtime
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_STRINGREFERENCEMEMBERCHECK_H

View File

@ -188,6 +188,8 @@ Improvements to clang-tidy
- The 'misc-unused-raii' check was renamed to :doc:`bugprone-unused-raii
<clang-tidy/checks/bugprone-unused-raii>`
- The 'google-runtime-member-string-references' check was removed.
Improvements to include-fixer
-----------------------------

View File

@ -1,25 +0,0 @@
.. title:: clang-tidy - google-runtime-member-string-references
google-runtime-member-string-references
=======================================
Finds members of type ``const string&``.
const string reference members are generally considered unsafe as they can be
created from a temporary quite easily.
.. code-block:: c++
struct S {
S(const string &Str) : Str(Str) {}
const string &Str;
};
S instance("string");
In the constructor call a string temporary is created from ``const char *`` and
destroyed immediately after the call. This leaves around a dangling reference.
This check emit warnings for both ``std::string`` and ``::string`` const
reference members.
Corresponding cpplint.py check name: `runtime/member_string_reference`.

View File

@ -110,7 +110,6 @@ Clang-Tidy Checks
google-readability-redundant-smartptr-get (redirects to readability-redundant-smartptr-get) <google-readability-redundant-smartptr-get>
google-readability-todo
google-runtime-int
google-runtime-member-string-references
google-runtime-operator
google-runtime-references
hicpp-avoid-goto

View File

@ -1,49 +0,0 @@
// RUN: %check_clang_tidy %s google-runtime-member-string-references %t
namespace std {
template<typename T>
class basic_string {};
typedef basic_string<char> string;
}
class string {};
struct A {
const std::string &s;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: const string& members are dangerous; it is much better to use alternatives, such as pointers or simple constants [google-runtime-member-string-references]
};
struct B {
std::string &s;
};
struct C {
const std::string s;
};
template <typename T>
struct D {
D();
const T &s;
const std::string &s2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: const string& members are dangerous
};
D<std::string> d;
struct AA {
const string &s;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: const string& members are dangerous
};
struct BB {
string &s;
};
struct CC {
const string s;
};
D<string> dd;