dotnet/roslyn

Invalid IDE0059 Suggestion

Aperta

#44.010 aperta il 6 mag 2020

 (12 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-IDEBugDeveloper CommunityFeature - IDE0059IDE-CodeStylehelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

This issue has been moved from a ticket on Developer Community.


I have the following C# method.

The second to last statement (parser++;) gives me an invalid IDE0059 Unecessary assignment of a value to 'parser' suggestion.

/// <summary>
/// Evaluates each parameter of a function's parameter list and returns
/// a list of those values. An empty list is returned if no parameters
/// were found. It is assumed the current position is at the opening
/// parenthesis of the argument list.
/// </summary>
/// <param name="parser">ParsingHelper object.</param>
/// <returns>A list of parameter values.</returns>
private Variable[] ParseParameters(ParsingHelper parser)
{
    List<Variable> parameters = new List<Variable>();

// Move past open parenthesis
    parser++;
    parser. SkipWhiteSpace();
    // If function has any parameters
    if (parser. Peek() != ')')
    {
        // Parse function parameter list
        int start = parser. Index;
        int parenCount = 1;

while (!parser. EndOfText)
        {
            if (parser. Peek() == ',')
            {
                // Note: Ignore commas inside parentheses. They could be
                // for a parameter list in a function inside the parameters
                if (parenCount == 1)
                {
                    parameters. Add(ParseParameter(parser, start));
                    start = parser. Index + 1;
                }
            }
            if (parser. Peek() == ')')
            {
                parenCount--;
                if (parenCount == 0)
                {
                    parameters. Add(ParseParameter(parser, start));
                    break;
                }
            }
            else if (parser. Peek() == '(')
            {
                parenCount++;
            }
            parser++;
        }
    }
    // Make sure we found a closing parenthesis
    if (parser. Peek() != ')')
        throw new ExpressionException(ErrClosingParenExpected, parser. Index);
    // Move past closing parenthesis
    parser++;
    // Return parameter list
    return parameters. ToArray();
}

At first glance, this seems correct because I'm apparently incrementing the value just before the method returns.

However, this class overloads the ++ operator to increment an internal counter. Therefore removing the statement would change the functionality.

public static ParsingHelper operator ++(ParsingHelper helper);

Original Comments

Visual Studio Feedback System on 5/4/2020, 02:05 AM:


Original Solutions

(no solutions)

Guida contributor