Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 401
fredoverflow Avatar asked Jan 20 '11 18:01

fredoverflow


People also ask

Should I treat warnings as errors?

Yes, even once in a while you will encounter the occasional warning you'd be better off leaving as a warning or even disabling completely. Those should be the exception to the rule, though. Here's some practical advise: at the start of a new project, start treating all warnings as errors, by default.

Is warning an error?

An error in MySQL informs you that you actually did something wrong, describes the problem, and stops the process or query. A warning will not stop anything, but is there to tell you that something happened that is not expected (or there may be a potential issue) and it's not critical enough to terminate.

Should warnings be ignored?

They are not errors from the viewpoint of a programming language, but they may be software bugs. However, many compilers can be customized so that their warnings don't stop the compilation process. Warnings must not be ignored. You'd better fix every possible error before starting software testing.


2 Answers

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

like image 138
Eugen Constantin Dinca Avatar answered Oct 04 '22 16:10

Eugen Constantin Dinca


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

like image 44
Mr. Furious Canada Avatar answered Oct 04 '22 14:10

Mr. Furious Canada