title: Warn when returning const copy of object
body: <p>Look at the function declarations below:</p>
  
  <pre><code>int Foo();        // [1] Ok
  const int &amp;Foo(); // [2] Ok
  const int Foo();  // [3] Not Ok. Same as [1]. Compiler can warn.
  
  Bar Foo();        // [4] Ok
  const Bar &amp;Foo(); // [5] Ok
  const Bar Foo();  // [6] Ok, not same as [4] or [5], but more likely a typo then intended design.
  </code></pre>
  
  <p><strong>Is it possible to ask Clang and/or GCC to warn about [6]?</strong></p>
  
  <p>I know this is legal, but I don't think it is very useful. I am pretty sure that all declarations like this in our code base are typos, and I would like to detect those at compile time.</p>
  
  <p><strong>Update:</strong>
  It seems to be some confusion about the const copy in [6]. Here is example of how little protection this is. Relying on the calling code not to make a copy is a large flaw in the design. Its is not robust at all. My question still stands, <em>how can I get a warning about this</em>, not what should I do when I receive the warning. Maybe I'll just ignore it :)</p>
  
  <p><strong><a href="http://cpp.sh/7ey7u" rel="nofollow">Live demo</a></strong></p>