[PATCH] D29393: [clang-tidy] Don't warn about call to unresolved operator*

Malcolm Parsons via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 1 06:23:53 PST 2017


malcolm.parsons created this revision.
Herald added a subscriber: JDevlieghere.

The misc-unconventional-assign-operator check had a false positive
warning when the 'operator*' in 'return *this' was unresolved.

Change matcher to allow calls to unresolved operator.

Fixes PR31531.


https://reviews.llvm.org/D29393

Files:
  clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
  test/clang-tidy/misc-unconventional-assign-operator.cpp


Index: test/clang-tidy/misc-unconventional-assign-operator.cpp
===================================================================
--- test/clang-tidy/misc-unconventional-assign-operator.cpp
+++ test/clang-tidy/misc-unconventional-assign-operator.cpp
@@ -87,3 +87,21 @@
     return n;
   }
 };
+
+enum E { e };
+E operator*(E, E);
+
+template <typename>
+struct UnresolvedOperator {
+  UnresolvedOperator &operator=(const UnresolvedOperator &) { return *this; }
+};
+
+UnresolvedOperator<int> UnresolvedOperatorInt;
+
+template <typename>
+struct Template {
+  Template &operator=(const Template &) { return this; }
+  // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: operator=() should always return '*this'
+};
+
+Template<int> TemplateInt;
Index: clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
===================================================================
--- clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
+++ clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
@@ -58,7 +58,10 @@
       this);
 
   const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
-      unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr()))))));
+      anyOf(unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())),
+            cxxOperatorCallExpr(argumentCountIs(1),
+                                callee(unresolvedLookupExpr()),
+                                hasArgument(0, cxxThisExpr())))))));
   const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
 
   Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29393.86623.patch
Type: text/x-patch
Size: 1608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170201/d46864a6/attachment-0001.bin>


More information about the cfe-commits mailing list