dotnet/roslyn

Invalid IDE0059 Suggestion

Open

#44,010 opened on May 6, 2020

View on GitHub
 (12 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-IDEBugDeveloper CommunityFeature - IDE0059IDE-CodeStylehelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

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)

Contributor guide