Skip to content

Commit 8a07a0f

Browse files
authored
Fix wrong ElementAt diagnostic (#49)
1 parent 1af5811 commit 8a07a0f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/AwesomeAssertions.Analyzers.Tests/Tips/CollectionTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,28 @@ public void CollectionShouldContainSingle_TestAnalyzer_GenericIEnumerableShouldR
844844
public void CollectionShouldHaveElementAt_AccessPropertyOfIndexedValue_TestNoAnalyzer(string assertion) =>
845845
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));
846846

847+
[TestMethod]
848+
public void CollectionShouldHaveElementAt_AccessObjectPropertyOfIndexedValue_TestNoAnalyzer()
849+
{
850+
// I cannot see the relevant difference to CollectionShouldHaveElementAt_AccessPropertyOfIndexedValue_TestNoAnalyzer,
851+
// but it makes a difference.
852+
string source = """
853+
using System.Collections.Generic;
854+
using AwesomeAssertions;
855+
856+
public record Value(object InstanceValue);
857+
858+
public sealed class TestClass
859+
{
860+
public void TestMethod(List<Value> list)
861+
{
862+
list[0].InstanceValue.Should().Be(1);
863+
}
864+
}
865+
""";
866+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
867+
}
868+
847869
[TestMethod]
848870
[AssertionDiagnostic("var first = actual[0]; first[6].Should().Be(expectedItem{0});")]
849871
[Implemented]

src/AwesomeAssertions.Analyzers/Tips/AwesomeAssertionsAnalyzer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,12 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, AwesomeA
388388
return;
389389
}
390390
}
391-
392-
if (subject.TryGetSingleChild<IPropertyReferenceOperation>(out var previousPropertyReference) && !previousPropertyReference.Property.IsIndexer)
391+
392+
if (subject is IPropertyReferenceOperation propertyReferenceOperation)
393+
{
394+
return;
395+
}
396+
else if (subject.TryGetSingleChild<IPropertyReferenceOperation>(out var previousPropertyReference) && !previousPropertyReference.Property.IsIndexer)
393397
{
394398
return;
395399
}

0 commit comments

Comments
 (0)