llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.96k stars 11.53k forks source link

support AUTOSAR C++14 A7-5-1 bugprone-return-const-ref-from-parameter #85253

Closed HerrCai0907 closed 4 months ago

HerrCai0907 commented 6 months ago

AUTOSAR C++14 A7-5-1 A function shall not return a reference or a pointer to a parameter that is passed by reference to const.

example

struct S {
    S();
    S(S const&);
    ~S();
};

S const& f(S const &a) { return a; }

S f() {
    S const & a = f(S{}); // object referenced by 'a' has destructed after this statement
    return a; // bugprone
}
llvmbot commented 6 months ago

@llvm/issue-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

> AUTOSAR C++14 A7-5-1 > A function shall not return a reference or a pointer to a parameter that is passed by reference to const. [example](https://godbolt.org/z/9zMEco9E3) ```c++ struct S { S(); S(S const&); ~S(); }; S const& f(S const &a) { return a; } S f() { S const & a = f(S{}); // object referenced by 'a' has destructed after this statement return a; // bugprone } ```
sopyb commented 5 months ago

I began working on this check request. I expect to have a pull request ready by Wednesday, possibly sooner if I don't face any unforeseen issues.

Any other resources I could reference would be greatly appreciated.

PiotrZSL commented 5 months ago

I think this check should be regardless this is const reference, reference or rvalue reference.

S&& f(S &&a) { return std::move(a); }

S const & a = f(S{}); // object referenced by 'a' has destructed after this statement

check should detect functions that return reference to argument.

carlosgalvezp commented 5 months ago

Please note: we are not allowed to implement AUTOSAR rules:

https://discourse.llvm.org/t/clang-tidy-rfc-add-autosar-c-14-clang-tidy-module/59223 https://reviews.llvm.org/D112730

Implementing this functionality without using AUTOSAR as a reference would be fine, though. Is there a similar e.g. C++ Core Guideline that could be used instead?

HerrCai0907 commented 5 months ago

Please note: we are not allowed to implement AUTOSAR rules:

https://discourse.llvm.org/t/clang-tidy-rfc-add-autosar-c-14-clang-tidy-module/59223

https://reviews.llvm.org/D112730

Implementing this functionality without using AUTOSAR as a reference would be fine, though. Is there a similar e.g. C++ Core Guideline that could be used instead?

Could we avoid legacy issues if we don't mention any autosar related things?

carlosgalvezp commented 5 months ago

Could we avoid legacy issues if we don't mention any autosar related things?

Sure, if we can avoid any mention to AUTOSAR and not copy anything from that document (rule text, rationale, code examples) then I think it should be good.