Warnings: Assignment within conditional

dedorris's Avatar

dedorris

21 Apr, 2014 11:01 PM

My compiler gives me 4 warning messages coming from the Robotlegs 2.2.0 source code.

...\robotlegs\bender\extensions\localEventMap\impl\EventMap.as:127:
Warning: Assignment within conditional.  Did you mean == instead of =?
            while (eventConfig = currentListeners.pop())
...\robotlegs\bender\extensions\localEventMap\impl\EventMap.as:149:
Warning: Assignment within conditional.  Did you mean == instead of =?
            while (eventConfig = _listeners.pop())
...\robotlegs\bender\extensions\localEventMap\impl\EventMap.as:169:
Warning: Assignment within conditional.  Did you mean == instead of =?
            while (eventConfig = _suspendedListeners.pop()) 
...\robotlegs\bender\framework\impl\MessageDispatcher.as:148:
Warning: Assignment within conditional.  Did you mean == instead of =?
        while (handler = _handlers.pop())

I understand the difference between the assignment operator (=) and the equality operator (==) and reading the warnings, it seems like an easy fix.

Changing the operator in the EventMap class makes the warning go away (but might break other functionality I have yet to see).

But "fixing" the MessageDispatcher causes everything to break. The StageCrawler's processView method throws an error because the ContainerBinding is null.

So my question for you is this:
Is there a reason why assignment is happening in the test condition for these while loops?
Should I just ignore this warning and let ignorance be bliss?

  1. 1 Posted by dedorris on 21 Apr, 2014 11:35 PM

    dedorris's Avatar

    Okay, I think I understand what is happening.

    I was so distracted working backwards from the StageCraweler error that didn't properly investigate the conditionals.

    I now see that the pattern in each of these cases is:

    while(someObject = someArray.pop())

    Array.pop() returns the last index and removes it from the array. The test condition will become false when there are no more indexes to pop from the array.

    So Robotlegs did not use the wrong operator, they were just being tricky. I have refactored my Robotlegs source just to avoid the warnings, but at least I know now that they were intentional.

    Here is how I did my refactor.

    var someObject:Object;
    while (someArray.length > 0)
    {
        someObject = someArray.pop();
        //
    }
    
  2. dedorris closed this discussion on 22 Apr, 2014 03:16 AM.

  3. dedorris re-opened this discussion on 23 Jun, 2014 03:51 PM

  4. 2 Posted by dedorris on 23 Jun, 2014 03:51 PM

    dedorris's Avatar

    I was setting up a new Robotlegs project in FlashDevelop on a different computer and I ran into these warnings again. This time I used a different refactoring that keeps the intention of the code the same, but suppresses the warning.

    while ((someObject = someArray.pop()) != null)
    {
        //
    }
    
  5. dedorris closed this discussion on 23 Jun, 2014 03:51 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac