#287 return statement style

open
nobody
5
2015-01-03
2014-06-09
Barry Allard
No

One more draft proposal...

For example, in most of our code we use return (...) standard form because of the inherent ability to trace returns using the awesomeness of macro hackery. Further a lot of OpenBSD and other code-bases use it.

Looking over the grammars for just C & C++, it seems like replacements could be made if expressions are parsed.

--return=remove-parens

return ...;

--return=no-space-before-parens

return(...);

--return=parens

return (...);

--return=inner-pad-parens

return ( ... );

--return=no-space-before-and-inner-pad-parens

return( ... );

Test cases:

return (int)(1.2345); 
return (int)1.2345; 
return ((int)(1.2345)); 
return ((int)1.2345); 
return ((0));
return (0);
return (-1) ;
return;
return ;

Discussion

  • Barry Allard
    Barry Allard
    2014-06-09

    FWIW, here's the macro:

    /* Yes, it's hacky and doesn't give the value like a debugger but it's a handy option. */
    /* Note for void fns: s/return;/return ((void)0);/g */
    #if defined(TRACE_RETURN) && ! defined(NDEBUG)
    #include <stdio.h>
    #define return(...) { \
      fprintf(stderr, "%s:%d return %s\n" , __FILE__, __LINE__, #__VA_ARGS__ ); \
      return __VA_ARGS__; \
    }
    #endif