Closed
Description
I suspect this might be related to the other issue about the syntax tree?
public class MyClass
{
public string Message { get; }
}
var list = new List<MyClass>();
list[0].Message.Should().Be(""); // throws exception
System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.MemberAccessExpressionSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ElementAccessExpressionSyntax'.
at FluentAssertions.Analyzers.NodeReplacement.RemoveAndRetrieveIndexerArgumentsNodeReplacement.ComputeNew(LinkedListNode`1 listNode)
at FluentAssertions.Analyzers.FluentAssertionsCodeFixProvider.GetNewStatement(ExpressionStatementSyntax statement,NodeReplacement replacement)
at FluentAssertions.Analyzers.CollectionShouldHaveElementAtCodeFix.GetNewStatement(ExpressionStatementSyntax statement,FluentAssertionsDiagnosticProperties properties)
at async FluentAssertions.Analyzers.FluentAssertionsCodeFixProvider.RewriteAssertion(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
If I rewrite the assertion to be valid for all enumerables
list.ElementAt(0).Message.Should().Be("");
it is simplified into the invalid
list.Message.Should().HaveElementAt(0, "");