fredoverflow
fredoverflow

Reputation: 263148

Can I treat a specific warning as an error?

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?

Upvotes: 24

Views: 10533

Answers (4)

Johannes Wentu
Johannes Wentu

Reputation: 989

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>

Upvotes: 2

Mr. Furious Canada
Mr. Furious Canada

Reputation: 81

/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.)

Upvotes: 6

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9140

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)).

Upvotes: 45

Zac Howland
Zac Howland

Reputation: 15872

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.

Upvotes: 0

Related Questions