aaronpuchert created this revision.
aaronpuchert added reviewers: aaron.ballman, rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
aaronpuchert requested review of this revision.

We don't have a source location for trailing return types at the moment,
and the current fallback (the identifier location) doesn't work for
lambdas. So instead we take the location of the right paranthesis, which
seems as close to the trailing return type as we can get.

Fixes PR47732.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90129

Files:
  clang/lib/Sema/SemaType.cpp
  clang/test/SemaCXX/return.cpp


Index: clang/test/SemaCXX/return.cpp
===================================================================
--- clang/test/SemaCXX/return.cpp
+++ clang/test/SemaCXX/return.cpp
@@ -65,6 +65,10 @@
     trailing_return_type() -> // expected-warning {{'const' type qualifier on 
return type has no effect}}
     const int;
 
+auto trailing_return_type_lambda =
+    [](const int &x) -> // expected-warning {{'const' type qualifier on return 
type has no effect}}
+    const int { return x; };
+
 const int ret_array()[4]; // expected-error {{cannot return array}}
 }
 
Index: clang/lib/Sema/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -3084,12 +3084,16 @@
 static void diagnoseRedundantReturnTypeQualifiers(Sema &S, QualType RetTy,
                                                   Declarator &D,
                                                   unsigned FunctionChunkIndex) 
{
-  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
+  const DeclaratorChunk::FunctionTypeInfo &FTI =
+      D.getTypeObject(FunctionChunkIndex).Fun;
+  if (FTI.hasTrailingReturnType()) {
     // FIXME: TypeSourceInfo doesn't preserve location information for
     // qualifiers.
+    SourceLocation Loc = D.getIdentifierLoc();
+    if (Loc.isInvalid())
+      Loc = FTI.getRParenLoc();
     S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type,
-                                RetTy.getLocalCVRQualifiers(),
-                                D.getIdentifierLoc());
+                                RetTy.getLocalCVRQualifiers(), Loc);
     return;
   }
 


Index: clang/test/SemaCXX/return.cpp
===================================================================
--- clang/test/SemaCXX/return.cpp
+++ clang/test/SemaCXX/return.cpp
@@ -65,6 +65,10 @@
     trailing_return_type() -> // expected-warning {{'const' type qualifier on return type has no effect}}
     const int;
 
+auto trailing_return_type_lambda =
+    [](const int &x) -> // expected-warning {{'const' type qualifier on return type has no effect}}
+    const int { return x; };
+
 const int ret_array()[4]; // expected-error {{cannot return array}}
 }
 
Index: clang/lib/Sema/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -3084,12 +3084,16 @@
 static void diagnoseRedundantReturnTypeQualifiers(Sema &S, QualType RetTy,
                                                   Declarator &D,
                                                   unsigned FunctionChunkIndex) {
-  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
+  const DeclaratorChunk::FunctionTypeInfo &FTI =
+      D.getTypeObject(FunctionChunkIndex).Fun;
+  if (FTI.hasTrailingReturnType()) {
     // FIXME: TypeSourceInfo doesn't preserve location information for
     // qualifiers.
+    SourceLocation Loc = D.getIdentifierLoc();
+    if (Loc.isInvalid())
+      Loc = FTI.getRParenLoc();
     S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type,
-                                RetTy.getLocalCVRQualifiers(),
-                                D.getIdentifierLoc());
+                                RetTy.getLocalCVRQualifiers(), Loc);
     return;
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to