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

Matthijs van Duin via cfe-dev cfe-dev at lists.llvm.org
Thu Nov 12 05:35:28 PST 2015


One thing that bothered me though is that the keyword 'auto' was reused
simply because... it was there. There's nothing "auto" going on in any
sense of the word.

This can be fixed though. I guess I may go to hell for it, but I found a
magic trick:

#define let auto

together with custom syntax highlighting rules making let a statement (and
auto a type rather than a storage specifier, for the rare case you need it).

Watch it in action:

let frobnicate( int x ) -> int { ... }

let constexpr addone( auto x ) {  return x+1;  }  // g++ extension I think?

And it works excellently for initialized variable declarations as well:

let x = 42;
let const debug = false;

for( let &x : things ) { ... }

Annoyingly not everything can be written like this, although after a while
of writing in this style, the inconsistency of exceptions have actually
become an eye-sore to the point I'd now sooner write
let i = u64 {};
than
u64 i;
even though I'm normally a fan of conciseness.

You can admire this and other horrible coding sins in some actual code e.g.
here:
  https://github.com/dutchanddutch/jbang/blob/master/src/hw-subarctic.cc
though without the custom syntax highlighting rules it can't be fully
appreciated.

*runs for cover* ;-)

Matthijs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151112/625318b4/attachment.html>


More information about the cfe-dev mailing list