LAA1002: DictionariesOrSetsShouldBeOrderedToEnumerate

Since there is no explicit order to enumerate dictionaries or sets, enumerating them breaks IAction's determinism. They should be explicitly ordered before being enumerated.

Applied interfaces are:

  • System.Collections.Generic.IDictionary<K, V>
  • System.Collections.Generic.IReadOnlyDictionary<K, V>
  • System.Collections.Generic.ISet<V>
  • System.Collections.Immutable.IImmutableSet<V>
  • System.Collections.IDictionary

Examples

The following code is warned by the rule LAA1002:

public IAccountStateDelta Execute(IActionContext context)
{
    var states = context.PreviousStates;
    if (states.GetState(_address) is Bencodex.Types.Dictionary d)
    {
        string list = d.Keys
            .OfType<Bencodex.Types.Text>(k => k.Value);  // LAA1002
        states.SetState(_targetAddress, (Bencodex.Types.Text)list);
    }

    return states;
}

This can be addressed like below:

public IAccountStateDelta Execute(IActionContext context)
{
    var states = context.PreviousStates;
    if (states.GetState(_address) is Bencodex.Types.Dictionary d)
    {
        string list = d.Keys
            .OfType<Bencodex.Types.Text>(k => k.Value)
            .OrderBy(k => k);  // Fixed
        states.SetState(_targetAddress, (Bencodex.Types.Text)list);
    }

    return states;
}
  • Improve this Doc
In This Article
Back to top Copyright © 2018–2021 Planetarium