Antrax
Antrax

Reputation: 13

Possible mistaken empty statement warning

I'm new to coding and I've been having warning with all day with "If (Jtag.activeConnection);" It gives me "Possible mistaken empty statement". It doesn't effect my program but every video I've seen people never get that warning and I would like to just clear It. The following is my code

public partial class Form1 : MetroFramework.Forms.MetroForm
{
    XRPC Jtag = new XRPC();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Jtag.Connect();
        if (Jtag.activeConnection) ;
    }
}

Upvotes: 1

Views: 13367

Answers (4)

ForceVII
ForceVII

Reputation: 393

It's because you have ; at end of your statement. So just delete it.

Upvotes: -1

A876
A876

Reputation: 518

Both halves of this question still seem unanswered.

1) I don't know why he wants to accomplish this:

if (Jtag.activeConnection) {}

Maybe he just wants to get that expression evaluated (for its side-effect(s)). If so, I expect he could just do this instead:

Jtag.activeConnection;

Or, if that doesn't work, he could settle for doing this:

bool dummy = Jtag.activeConnection;

(Though one or both might provoke a warning about the unused result.)

2) About the "Possible mistaken empty statement" warning: Maybe he prefers to style his null if branches with ;. Sometimes I might prefer ; over { } too, like this:

if (expression)
    ;
else
    ;

That seems legit, according to MSDN C# Language Specification. It compiles. But using an empty expression as an if- or else-branch provokes a compiler warning: "Possible mistaken empty statement" (CS0642). (It is bad style because the lone ; is ambiguous.) (Sadly, there is no explicit, unambiguous noop; statement.)

I see no [intelligent] way to make the warning go away, for just those lines. Some IDEs have compact single-instance overrides for unwanted warnings (embedded in comment text at the end of the same line). VS2005 - VS2015 only offers that I can surround the offending line(s) with #pragma warning disable 0642 and #pragma warning restore 0642, which is too ugly to contemplate. (I also refuse to use the #pragma or a compile option on the whole program - it is too risky. I want to suppress each specific warning individually.) ((C++ offers #pragma warning suppress, which suppresses the specified error once (in the very next line) while only adding one ugly line. C# must have decided to never implement it.))

VS doesn't object if I style it this way (even though it does nothing), but I prefer not too. (It is too tall.)

if (expression)
{
}
else
{
}

And I don't want to say it this way either. (It is compact, but the standard automatic pretty-printing does not indent the { } to where the ; was, even though i think it should.)

if (expression)
{ }
else
{ }

Upvotes: -2

Brett Caswell
Brett Caswell

Reputation: 1504

You're receiving the compiler warning (CS0642)

because...

A semicolon after a conditional statement may cause your code to execute differently than intended

It is a warning, and you can suppress it via compiler options, pragma directives, or correct it by providing a block to run your condition statements in.

I recommend the ladder; here is how that looks:

Jtag.Connect();
if (Jtag.activeConnection) 
{
    // by virtue of having the blocks, and removing the semi-colon.. the warning goes away.
}

Upvotes: 0

FFMG
FFMG

Reputation: 1257

Well, your if statement is empty

if (Jtag.activeConnection) ;

So the compiler is asking you to confirm if that's really what you wanted to do.

Normally it should be something like

if (Jtag.activeConnection)
{
  // do something
}

Upvotes: 7

Related Questions