And also enable it by default to be consistent with e.g. modernize-use-using.
This helps e.g. when running this check on client code where the macro is
provided by the system, so there is no easy way to modify it.
Reviewed By: JonasToth
Differential Revision: https://reviews.llvm.org/D53454
llvm-svn: 344871
} // namespace
+void RedundantSmartptrGetCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoreMacros", IgnoreMacros);
+}
+
void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
// Only register the matchers for C++; the functionality currently does not
// provide any benefit to other languages, despite being benign.
bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
+ if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
+ return;
+
const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
if (IsPtrToPtr && IsMemberExpr) {
class RedundantSmartptrGetCheck : public ClangTidyCheck {
public:
RedundantSmartptrGetCheck(StringRef Name, ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
+ : ClangTidyCheck(Name, Context),
+ IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {}
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+ const bool IgnoreMacros;
};
} // namespace readability
Improvements to clang-tidy
--------------------------
+- The :doc:`readability-redundant-smartptr-get
+ <clang-tidy/checks/readability-redundant-smartptr-get>` check does not warn
+ about calls inside macros anymore by default.
+
- New :doc:`abseil-duration-division
<clang-tidy/checks/abseil-duration-division>` check.
*ptr->get() ==> **ptr
if (ptr.get() == nullptr) ... => if (ptr == nullptr) ...
+
+.. option:: IgnoreMacros
+
+ If this option is set to non-zero (default is `1`), the check will not warn
+ about calls inside macros.
--- /dev/null
+// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t -- \
+// RUN: -config="{CheckOptions: [{key: readability-redundant-smartptr-get.IgnoreMacros, value: 0}]}" \
+// RUN: -- -std=c++11
+
+namespace std {
+
+template <typename T>
+struct shared_ptr {
+ T &operator*() const;
+ T *operator->() const;
+ T *get() const;
+ explicit operator bool() const noexcept;
+};
+
+} // namespace std
+
+#define MACRO(p) p.get()
+
+void Positive() {
+ std::shared_ptr<int> x;
+ if (MACRO(x) == nullptr)
+ ;
+ // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: redundant get() call on smart pointer
+};
// CHECK-FIXES: if (NULL == x);
}
+#define MACRO(p) p.get()
+
void Negative() {
struct NegPtr {
int* get();
bool bb = ip.get() == nullptr;
bb = !ip.get();
bb = ip.get() ? true : false;
+ std::unique_ptr<int> x;
+ if (MACRO(x) == nullptr)
+ ;
}