From a7395b860bc247c9e4e917bf5786c04d4cccf1d7 Mon Sep 17 00:00:00 2001 From: Alexander Shaposhnikov Date: Wed, 31 Aug 2022 09:13:21 +0000 Subject: [PATCH] [clang-tidy] Skip copy assignment operators with nonstandard return types Skip copy assignment operators with nonstandard return types since they cannot be defaulted. Test plan: ninja check-clang-tools Differential revision: https://reviews.llvm.org/D133006 --- clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp | 8 +++++++- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++- .../clang-tidy/checkers/modernize/use-equals-default-copy.cpp | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp index f321c0b..2de677d 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp @@ -8,6 +8,7 @@ #include "UseEqualsDefaultCheck.h" #include "../utils/LexerUtils.h" +#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" @@ -247,7 +248,12 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) { // isCopyAssignmentOperator() allows the parameter to be // passed by value, and in this case it cannot be // defaulted. - hasParameter(0, hasType(lValueReferenceType()))) + hasParameter(0, hasType(lValueReferenceType())), + // isCopyAssignmentOperator() allows non lvalue reference + // return types, and in this case it cannot be defaulted. + returns(qualType(hasCanonicalType( + allOf(lValueReferenceType(pointee(type())), + unless(matchers::isReferenceToConst())))))) .bind(SpecialFunction), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index e0907b2..7c91ea0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -145,7 +145,8 @@ Changes in existing checks check. The check now skips unions since in this case a default constructor with empty body - is not equivalent to the explicitly defaulted one. The check is restricted to c++11-or-later. + is not equivalent to the explicitly defaulted one. The check also skips copy assignment + operators with nonstandard return types. The check is restricted to c++11-or-later. Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default-copy.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default-copy.cpp index 78a03f0..70fc521 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default-copy.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default-copy.cpp @@ -444,6 +444,13 @@ IL &WRT::operator=(const WRT &Other) { return *this; } +// Wrong return type. +struct WRTConstRef { + const WRTConstRef &operator = (const WRTConstRef &) { + return *this; + } +}; + // Try-catch. struct ITC { ITC(const ITC &Other) -- 2.7.4