Can I treat a specific warning as an error?
Asked Answered
F

4

24

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?

Foldaway answered 20/1, 2011 at 18:16 Comment(3)
Surely you should just treat all warnings as errors. :)Mim
@GMan: Though in the general case I agree. It is sometimes necessary to be able to specific things.Henton
@Mim - 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.Kearse
P
45

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

Presumptive answered 20/1, 2011 at 18:22 Comment(3)
Does the pragma need to be in all files for it to count or any in the project?Tawnyatawsha
documentation: /w, /Wn, /WX, /Wall, /wln, /wdn, /wen, /won (Warning Level)Kero
@TomSirgedas Thank you for the documentation link.Presumptive
P
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.)

Poddy answered 4/4, 2014 at 19:5 Comment(0)
P
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>
Plains answered 23/4, 2020 at 7:29 Comment(0)
B
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.

Brynne answered 20/1, 2011 at 18:24 Comment(6)
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.Faraday
I forgot to mention the /WX option. Edited to fix.Brynne
@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.Determine
@Matthieu: /W4 is fine, but /Wall is the annoying one, giving warnings for stuff in system headers.Juvenescent
@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.Brynne
@Zac: I thought it signalled issues in the STL headers too.. may have mistaken it for /Wall :)Determine

© 2022 - 2024 — McMap. All rights reserved.