Skip to content

[clang] FunctionDecl::getReturnTypeSourceRange() returns invalid SourceRange for trailing return type functions #162649

@bricknerb

Description

@bricknerb

This probably effects a lot of code that uses trailing return type syntax.

To make it easily reproducible, I looked at this use case:

const int &return_const_ref(); // expected-note{{function 'return_const_ref' which returns const-qualified type 'const int &' declared here}}

With trailing return type it generates the note without a location.

Not using trailing return types (conventional prefix syntax):

const int& good();
void test_good() { good() = 10; }

Diagnoses with

<source>:2:27: error: cannot assign to return value because function 'good' returns a const value
    2 | void test_good() { good() = 10; }
      |                    ~~~~~~ ^
<source>:1:7: note: function 'good' which returns const-qualified type 'const int &' declared here
    1 | const int& good();
      |       ^~~~
1 error generated.
Compiler returned: 1

However, using trailing return types:

auto bad() -> const int&;
void test_bad() { bad() = 10; }

Diagnoses with

<source>:2:25: error: cannot assign to return value because function 'bad' returns a const value
    2 | void test_bad() { bad() = 10; }
      |                   ~~~~~ ^
note: function 'bad' which returns const-qualified type 'const int &' declared here
1 error generated.
Compiler returned: 1

See https://godbolt.org/z/nn4KTYceW

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions