23

The following is a simplified version of a pattern I sometimes see in my students' code:

bool foobar(int a, int b)
{
    if (a < b) return true;
}

The real code is more complicated, of course. Visual Studio reports a warning C4715 (not all control paths return a value), and I would like to treat all warnings C4715 as errors. Is that possible?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 4
    Surely you should just treat all warnings as errors. :) – GManNickG Jan 20 '11 at 18:20
  • 1
    @GMan: Though in the general case I agree. It is sometimes necessary to be able to specific things. – Martin York Jan 20 '11 at 18:59
  • @GManNickG - i recently turned on `-Werror` for a project of mine and realized it has a down-side, which is that when i'm developing the code it's helpful to have mistakes categorized as errors or warnings. I can see "aha, 2 warnings and 1 error" and start anticipating what i probably did wrong better than if i just see "3 errors". Really `-Werror` seems only necessary if that's the only way to get people to eliminate warnings in the code. – orion elenzil Dec 14 '18 at 18:30

4 Answers4

44

This should do the trick: #pragma warning (error: 4715).
Or the /we4715 command line option (see /w, /W0, /W1, /W2, /W3, /W4, /w1, /w2, /w3, /w4, /Wall, /wd, /we, /wo, /Wv, /WX (Warning Level) (courtesy of Tom Sigerdas)).

Eugen Constantin Dinca
  • 8,994
  • 2
  • 34
  • 51
6

/we4715 works for me.

In Visual Studio 2013 anyway, it is in the UI under Project Settings -> Configuration Properties -> C/C++ -> *Advanced *-> Treat Specific Warnings as Errors. Add "4715".

Docs: http://msdn.microsoft.com/en-us/library/thxezb7y.aspx

(Please note that this page lists the wrong UI property for VS2013.)

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
2

I added the following to the (VB)project file and it worked:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">  
<WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036,41997</WarningsAsErrors>
</PropertyGroup>
Johannes Wentu
  • 931
  • 1
  • 14
  • 28
0

Set the compiler warning level to level 4 (in Visual Studio) and it will treat all warnings as errors. It is good practice to have your students compile their code with no warnings and no errors anyway :)

Also, turn on the /WX compiler option.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • 4
    No it won't. Not unless you tell it to. It's a good practice to set both, but setting to level 4 is not sufficient to make it error out on a warning. – Edward Strange Jan 20 '11 at 18:28
  • I forgot to mention the /WX option. Edited to fix. – Zac Howland Jan 20 '11 at 18:40
  • @Zac: on the other hand, at level 4, VS really screams for a whole lot of things that are **not** errors. I much prefer level 3 and `/WX` together. – Matthieu M. Jan 20 '11 at 18:48
  • 2
    @Matthieu: /W4 is fine, but /Wall is the annoying one, giving warnings for stuff in system headers. – Steve M Jan 20 '11 at 18:53
  • 1
    @Matthieu: It will only scream at you for warnings you create at level 4. I avoid /Wall as there is nothing I can do to fix Microsoft's implementation of the standard library, yet it will scream at you for it. – Zac Howland Jan 20 '11 at 19:15
  • @Zac: I thought it signalled issues in the STL headers too.. may have mistaken it for /Wall :) – Matthieu M. Jan 21 '11 at 07:07