"Unreachable Code detected" stumps me > solved

Hi all, this little code here stumps me. It works just fine but I get compiler warnings.
Maybe I am not seeing the forest for the trees :smiley:

The compiler throws a “warning CS0162: Unreachable code detected” on me in the “for(…)” loop. It seems to be disturbed by the return in the loop as if it thinks it will always return at first try (which may, or may not be true depending on the result of the if check).

It als gives me a “warning CS0642: Possible mistaken empty statement” warning on the “if(..)” line which I think is the root cause. However, I don’t get how it can be an “empty” statement (always true).

public SerializedProperty GetSerializedPropertyOfEffect(string id, string propertyName)
{
    var so = new SerializedObject(this);
    
    var effects = so.FindProperty("_effects");
    for (int i = 0; i < effects.arraySize; i++) // <- i++ is "unreachable"
    {
        var effect = effects.GetArrayElementAtIndex(i);
        if (effect.FindPropertyRelative("Id").stringValue == id); // <- is an "empty" statement
        {
            var property = effect.FindPropertyRelative(propertyName);
            return property;
        }
    }

    return null;
}

Just for fun I tried this:

public SerializedProperty GetSerializedPropertyOfEffect(string id, string propertyName)
{
    var so = GetSerializedObject();
    
    var effects = so.FindProperty("_effects");
    for (int i = 0; i < effects.arraySize; i++) // <- it still thinks "i++" is unreachable.
    {
        var effect = effects.GetArrayElementAtIndex(i);
        // v- IDE tells me this will always be true which confirms my suspicion. But WHY?
        if (i%2 == 0);
        {
            var property = effect.FindPropertyRelative(propertyName);
            return property;
        }
    }

    return null;
}

I assume it has something to do with “.arraySize” of SerializedProperty but if I decompile it I can not really see an issue:

/// <summary>
///   <para>The number of elements in the array.</para>
/// </summary>
public int arraySize
{
  get
  {
	this.Verify(SerializedProperty.VerifyFlags.IteratorNotAtEnd);
	return this.GetInspectableArraySize();
  }
  set
  {
	this.Verify();
	this.ResizeArray(value);
  }
}

[MethodImpl(MethodImplOptions.InternalCall)]
private extern int GetInspectableArraySize();

I mean, the array size is not a constant (why should it be) but then how does to compiler determine that it will always be 0 (which I assume it does based on the warnings I see)?

I can solve this on another way (and probably will even though the code is working just fine). I am just curious. What the heck am I missing? :smiley:

UPDATE: Never mind, 10+ years of coding and still bested by an extra “;” :smiley: :smiley: :smiley:
Can you find it ? :wink:

The first warning is due to your second warning. You shouldn’t put a semi-colon after an if statement. It turns this:

if (effect.FindPropertyRelative("Id").stringValue == id); // bad
{
	var property = effect.FindPropertyRelative(propertyName);
	return property;
}

Into this technically:

if (effect.FindPropertyRelative("Id").stringValue == id) { ; }

var property = effect.FindPropertyRelative(propertyName);
return property;

(Which is valid C# by the way).

Meaning you will always return after the first iteration of the loop, hence the i++ being unreachable, as it would never actually loop.

So long story short, don’t put semi-colons immediately after your if-statements.

Haha thanks, yes. I noticed just after posting :smiley:
Should have read the compiler warning more carefully, was all there.

I think what threw me off was that it was actually working but that was just coincidence that the list only had one entry that was the one I needed.

I guess it’s true. Sometimes you really don’t see the forest for the trees :slight_smile:

^. ^ ^ This is what accounts for #6 below. Been there, done the silly semicolon trick. :slight_smile:

Remember the six stages of debugging:

  1. That can’t happen.
  2. That doesn’t happen on my machine.
  3. That shouldn’t happen.
  4. Why does that happen?
  5. Oh, I see.
  6. How did that ever work?