-4

I feel so silly asking this, but how do I instruct the compiler that it is not a mistake? I really intend to have an "empty" branch in my if statement, with a clear else at the end catching the bad values.

I can of course restructure the whole if around this, but it'll be less clear as a result, and working around a silly compiler warning seems weird. Also I'd prefer if I didn't have to disable the entire warning, just for this specific statement.

A876
  • 471
  • 5
  • 8
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 5
    So you want the warning... but you don't want the warning? This makes little sense to me. Also, I would argue that an empty `if` where only the `else` branch is used is bad form. I'd love to see an example which proves me wrong. – Ed S. May 27 '15 at 19:34
  • I want the warning in general, but I also want an empty branch in an `if` without the compiler saying it's a mistake. – Blindy May 27 '15 at 19:35
  • 4
    You said you could rewrite; could you provide a simple example in your question that demonstrates why this is less readable? – D. Ben Knoble May 27 '15 at 19:36
  • I can, but it's irrelevant to the question. I'm asking if anyone knows a way to instruct the compiler that the code I wrote is not a mistake, not starting a debate about proper coding form. – Blindy May 27 '15 at 19:37
  • 1
    You're right, it's not relevant. I'd still love to see it. Also, very often people don't ask for what it is that they actually want. More context may lead to a better overall solution. – Ed S. May 27 '15 at 19:39

5 Answers5

3
#pragma warning disable xxxx
if (condition)
{
  // north of the wall, nada, nothing
}
else
{
  // code
}
#pragma warning restore xxxx
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
  • Sure, however I specifically mentioned this as a possible solution and discarded it. Rehashing it seems pointless. – Blindy May 27 '15 at 19:47
  • 4
    "I'd prefer if I didn't have to disable the entire warning, just for this specific statement." - That is exactly what you are getting, you are disabling the warning for your "if" statement. – Riad Baghbanli May 27 '15 at 19:55
2

why not just putting a bang in front of your if condition so you can avoid using the else entirely?

if(!condition){
  //just work here
}

edit: updating answer based on question author comment (its multiple conditions)

if(condition1){
  // code here
}
else if(condition2){
  // code here
}
else if(!condition3){
  //error case here
}
JSelser
  • 3,510
  • 1
  • 19
  • 40
1

Well I knew it was something silly, simply using an empty block gets rid of it:

if(condition1)
  // code here
else if(condition2)
  // code here
else if(condition3)
{
    //nothing to do
}
else
  // error case here
Blindy
  • 65,249
  • 10
  • 91
  • 131
-1

To suppress compiler warnings, VS2010 has compiler options and #pragma options. Compiler options affect the whole program. #pragma options can affect the whole program. #pragma options can also locally turn specific warning(s) off just before targeted code, and then turn then back on just after. #pragma warning disable 0642 will hide this warning. #pragma warning restore 0642 will restore it. This is just ugly. Other IDEs that I have seen allow single-line overrides of warnings (specific text in a comment immediately after the provoking code), but VS2010 seems not to have done this. (I could be wrong. Anyone know?)

The only fix is worse than your "problem". Local #pragma directives will make your code uglier than it is, while removing the warning. Ultimately you are better off doing what the smart people suggested. (They sometimes miss the boat.) Getting your way is not necessarily winning. You have to admit that possibility in order to deserve answers on how to do it regardless of whether it is an improvement.

Basically this question is a PREFERENCE thing. You should have said that from the get-go, by showing (with code) what you are talking about, instead of abusing helpful people by making them guess and guess again.

The compiler warning suggests that you probably should NOT write this:

if (expression)
    ;  // Do nothing, because blah blah blah.
else
{
    code that;
    does something;
}

, but instead pick one of:

if (expression)
{
   // Do nothing, because blah blah blah.
}
else
{
    code that;
    does something;
}

or

if (expression)
{ }  // Do nothing, because blah blah blah.
else
{
    code that;
    does something;
}

You DID NOT "specifically mention this" (someone else's rewrite of code you never provided) "as a possible solution ...". Also you did not say whether you reject Riad's change of ; to { ... } or his surrounding #pragmas (which, by the way, are only necessary if you do NOT change ; to { ... }).

IN NO WAY do these variations amount to "restructur[ing] the whole if [statement] around [removing] this [warning]". (That would be if (!expression) { code that; does something; }.)

IN NO WAY are these variations ANY "less clear" than the original.

I wouldn't mind knowing an override that isn't ugly. I might want to write something like the following, because I like to omit all those silly surrounding braces-on-empty-lines that would make sections like this twice as tall:

if (condition1)
  action1;
else if (condition2)
  ;  // (do nothing)
else if (condition3)
  action3;
else if (condition4)
  ;  // (do nothing)
else if (condition5)
  action5;
else
  action99;

Apparently I have to use one of the uglier options to avoid those annoying "Possible mistaken empty statement" warnings.

A876
  • 471
  • 5
  • 8
-1

If you replace

;

with

((Action)(() => { }))();

, the warning should go away.

(Drawback: This might take time to execute at runtime, a waste. If so, it should only be used for tracing (if it works for that), and it should be removed or replaced before shipping.)

from: Simple C# Noop Statement

Community
  • 1
  • 1
A876
  • 471
  • 5
  • 8