Evaluating Parser State The "parser state" of a module in rubberduck can be one of several values:

//note: ordering of the members is important
public enum ParserState
{
    /// &lt;summary&gt;
    /// Parse was requested but hasn't started yet.
    /// &lt;/summary&gt;
    Pending,
    /// &lt;summary&gt;
    /// Project references are being loaded into parser state.
    /// &lt;/summary&gt;
    LoadingReference,
    /// &lt;summary&gt;
    /// Code from modified modules is being parsed.
    /// &lt;/summary&gt;
    Parsing,
    /// &lt;summary&gt;
    /// Parse tree is waiting to be walked for identifier resolution.
    /// &lt;/summary&gt;
    Parsed,
    /// &lt;summary&gt;
    /// Resolving identifier references.
    /// &lt;/summary&gt;
    Resolving,
    /// &lt;summary&gt;
    /// Parser state is in sync with the actual code in the VBE.
    /// &lt;/summary&gt;
    Ready,
    /// &lt;summary&gt;
    /// Parsing could not be completed for one or more modules.
    /// &lt;/summary&gt;
    Error,
    /// &lt;summary&gt;
    /// Parsing completed, but identifier references could not be resolved for one or more modules.
    /// &lt;/summary&gt;
    ResolverError,
}


Now, Rubberduck uses each modules' state, and determines what the "overall" state is - and then displays that value in a status bar.

The rules are:


If all modules have the same state, overall state is that value.
If any module is in an error state, overall state is that error value.
Overall state can only be "ready" when all modules are "ready".
Overall state can only be "parsed" when all modules are "parsed".
Otherwise return the state of the most advanced non-ready module.


The code started simple, got complicated, then too simple, and now looks like this - basically it went back to "too complicated":

private static readonly ParserState[] States = Enum.GetValues(typeof(ParserState)).Cast&lt;ParserState&gt;().ToArray();
private ParserState EvaluateParserState()
{
    var moduleStates = _moduleStates.Values.ToList();
    var state = States.SingleOrDefault(value =&gt; moduleStates.All(ps =&gt; ps == value));

    if (state != default(ParserState))
    {
        // if all modules are in the same state, we have our result.
        Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", state, Thread.CurrentThread.ManagedThreadId);
        return state;
    }

    // error state takes precedence over every other state
    if (moduleStates.Any(ms =&gt; ms == ParserState.Error))
    {
        Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", ParserState.Error,
        Thread.CurrentThread.ManagedThreadId);
        return ParserState.Error;
    }
    if (moduleStates.Any(ms =&gt; ms == ParserState.ResolverError))
    {
        Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", ParserState.ResolverError,
        Thread.CurrentThread.ManagedThreadId);
        return ParserState.ResolverError;
    }

    // intermediate states are toggled when *any* module has them.
    var result = moduleStates.Min();
    if (moduleStates.Any(ms =&gt; ms == ParserState.Parsing))
    {
        result = ParserState.Parsing;
    }
    if (moduleStates.Any(ms =&gt; ms == ParserState.Resolving))
    {
        result = ParserState.Resolving;
    }

    if (result == ParserState.Ready &amp;&amp; moduleStates.Any(item =&gt; item != ParserState.Ready))
    {
        result = moduleStates.Except(new[] {ParserState.Ready}).Max();
    }

    Debug.Assert(result != ParserState.Ready || moduleStates.All(item =&gt; item == ParserState.Ready));

    Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", result,
    Thread.CurrentThread.ManagedThreadId);
    return result;
}


Does anything stick out? Could it be simplified? Any/all feedback welcome!
If I understand the rules correctly, the last rule "Otherwise return the state of the most advanced non-ready module." isn't met by your method. Having states {ParserState.LoadingReference, ParserState.Parsed, ParserState.Parsing, ParserState.Ready} I would expect the returned state equals to ParserState.Parsed but your method results in ParserState.Parsing.  
The rules 3 and 4 are superflous because they are satisfied by rule 1.
Because any error state takes precedence over every other state you should chewck this first. If all states are of the same error state it will result in the same result, but checking first the error states will be faster.  
this  

if (result == ParserState.Ready &amp;&amp; moduleStates.Any(item =&gt; item != ParserState.Ready))
{
    result = moduleStates.Except(new[] {ParserState.Ready}).Max();
}  


looks strange. result equals to the Min() of the modulStates, so let us assume it would equal to ParserState.Ready then it is garanteed that the second condition will result in true because you already checked if all states are the same.  Because of the the check of the same values the second condition of Debug.Assert(result != ParserState.Ready || moduleStates.All(item =&gt; item == ParserState.Ready)); is superflous as well.  
You should use the same variables name for the same type when using lambdas. One time you have ps =&gt; ps == value and the other times you use ms =&gt; ms == ParserState.Error.  
The method relies on a class variable which I would change. Add a parameter to that method declaration so it can stand on its own.  


Implementing the mentioned points will lead to  

private ParserState EvaluateParserState(IList&lt;ParserState&gt; moduleStates)
{
    // error state takes precedence over every other state
    if (moduleStates.Any(ms =&gt; ms == ParserState.Error))
    {
        Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", ParserState.Error,
        Thread.CurrentThread.ManagedThreadId);
        return ParserState.Error;
    }
    if (moduleStates.Any(ms =&gt; ms == ParserState.ResolverError))
    {
        Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", ParserState.ResolverError,
        Thread.CurrentThread.ManagedThreadId);
        return ParserState.ResolverError;
    }

    var state = States.SingleOrDefault(value =&gt; moduleStates.All(ms =&gt; ms == value));

    if (state != default(ParserState))
    {
        // if all modules are in the same state, we have our result.
        Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", state, Thread.CurrentThread.ManagedThreadId);
        return state;
    }

    var result = moduleStates.Except(new[] { ParserState.Ready }).Max();

    Debug.Assert(result != ParserState.Ready);

    Debug.WriteLine("ParserState evaluates to '{0}' (thread {1})", result,
    Thread.CurrentThread.ManagedThreadId);
    return result;
}