[cfe-dev] RFC: clang-tidy readability check to reduce clutter: unnecessary use of auto and ->

Fabio Fracassi via cfe-dev cfe-dev at lists.llvm.org
Sat Nov 7 01:40:19 PST 2015


Hi,

On 06/11/2015 19:08, Richard via cfe-dev wrote:
[...]
> N2541 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2541.htm>
> introduced auto and the trailing return type syntax.
>
> Now people are using auto and -> on function signatures where the
> return type is stated explicitly anyway.  This results in code like:
>
> auto main() -> int
> {
>    return 0;
> }
so?

> Srsly?  Is this really improving anything over
>
> int main()
> {
>    return 0;
> }
no, but it is not worse either.

> To me this is introducing unnecessary clutter
it is more uniform, and for mathematically inclined people it is also 
more natural.
just because it is a new syntax it doesn't  make it more cluttered or 
anything.

Herb Sutter recommends this style in his Almost Always Auto article:

*auto*f *(double) -> int*; *auto*f *(double)*{ /*...*/ }; *auto*f = 
[=]*(double)*{ /*...*/ };

I actually like the style especially when declaring interfaces:

struct X {
     auto foo(int) -> bool;
     auto bar(double, Y) -> Z;
     ...
};

because it naturally aligns the most crucial information, the function 
name at the beginning of the line and mostly at the same column.

> and isn't the reason why
> auto and -> for function return types were introduced.
certainly not, but it is a nice side effect.

>
> I would like to propose a clang-tidy readability check that turns the
> former into the latter when the return type doesn't use anything that
> needs auto.  In other words, this code would be left alone:
>
> template <typename T, typename U>
> auto add(T x, U y) -> decltype(x + y)
> {
>    return x + y;
> }
>
> There is one note in N2541 that states how the new syntax eliminates
> an ambiguity with this example:
>
> auto f() -> int (*)[4];
> // function returning a pointer to array[4] of int,
> // not function returning array[4] of pointer to int.
>
> This seems to be another case that should be left unchanged.
>
> Proposed check name: readability-redundant-auto-return-type
>
> What are your thoughts?
I was thinking of exactly the opposite, turn all "classic" declarations 
into the new style :)

I guess this is really a matter of taste.
I would hope that clang-tidy (or would this be in the realm of 
clang-format?) would offer both options.

Best
Fabio




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151107/b2876c6b/attachment.html>


More information about the cfe-dev mailing list