Skip to content

BUG on CC0043 and CC0092 (ChangeAnyToAll) when invocation has elvis operator #613

@giggio

Description

@giggio

Here is the test case that will fail:

using System;
using System.Linq;
class TypeSymbol
{
    public System.Collections.Generic.IList<int> AllInterfaces { get; set; }
}
class TypeName
{
    void Foo()
    {
        var typeSymbol = new TypeSymbol();
        if (!typeSymbol?.AllInterfaces.Any(i => i.ToString() == "") ?? true) return;
    }
}

Both CC0043 and CC0092 will crash with an NullReferenceException when the Any or All invocation is complex, composed of more than one Member Access. So, instead of A.Any, which works, A.B.Any will fail. But only when composed with an elvis operator, like A?.B.Any.

This is happening at the UnusedParametersAnalyzer.cs, line 153. (See it here).

Here is the code that is causing the analysis to fail, from UnusedParametersAnalyzer.cs:

if (!typeSymbol?.AllInterfaces.Any(i => i.ToString() == "System.Runtime.Serialization.ISerializable") ?? true) return false;

The error is being raised at line 62 (see it here):

var otherInvocationSymbol = semanticModel.GetSpeculativeSymbolInfo(invocation.SpanStart, otherInvocation, SpeculativeBindingOption.BindAsExpression);

These are the variable values at that point:

invocation.ToString()
".AllInterfaces.Any(i => i.ToString() == \"System.Runtime.Serialization.ISerializable\")"
otherInvocation.ToString()
".AllInterfaces.All(i => i.ToString() == \"System.Runtime.Serialization.ISerializable\")"
invocation.Expression.ToString()
".AllInterfaces.Any"

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions