From f919693b8097346683e9235ce215728c9a92004a Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 7 Oct 2024 21:19:13 +0300 Subject: [PATCH 1/4] bugfix: false positive on expressions containing Count --- src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index 20310a3..3b1a940 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -329,6 +329,7 @@ public class CollectionTests [AssertionDiagnostic(@"var array = new string[1, 1]; array.Length.Should().Be(0{0});")] [AssertionDiagnostic(@"var array = new string[2, 2]; array.Length.Should().Be(0{0});")] [AssertionDiagnostic(@"var array = new string[3, 3, 3]; array.Length.Should().Be(0{0});")] + [AssertionDiagnostic(@"int[] array1 = [1, 2, 3]; int[] array2 = [4, 5, 6]; var both = x.Concat(y).ToArray(); (array1.Length + array2.Length).Should().Be(both.Length{0});")] [Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/309")] public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder() .AppendLine("using System;") From 52d37954352d24d081c119a860a454d1bdbd4a00 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 7 Oct 2024 21:25:14 +0300 Subject: [PATCH 2/4] bugfix: false positive on expressions containing Count --- src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index 3b1a940..a9a5f6e 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -329,7 +329,7 @@ public class CollectionTests [AssertionDiagnostic(@"var array = new string[1, 1]; array.Length.Should().Be(0{0});")] [AssertionDiagnostic(@"var array = new string[2, 2]; array.Length.Should().Be(0{0});")] [AssertionDiagnostic(@"var array = new string[3, 3, 3]; array.Length.Should().Be(0{0});")] - [AssertionDiagnostic(@"int[] array1 = [1, 2, 3]; int[] array2 = [4, 5, 6]; var both = x.Concat(y).ToArray(); (array1.Length + array2.Length).Should().Be(both.Length{0});")] + [AssertionDiagnostic(@"int[] array1 = [1, 2, 3]; int[] array2 = [4, 5, 6]; var both = array1.Concat(array2).ToArray(); (array1.Length + array2.Length).Should().Be(both.Length{0});")] [Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/309")] public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder() .AppendLine("using System;") From 8b07915a3e2e34bd4ed6605f3678a2951f9bba08 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 7 Oct 2024 22:35:17 +0300 Subject: [PATCH 3/4] bugfix: false positive on expressions containing Count --- src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index a9a5f6e..f98b395 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -329,7 +329,7 @@ public class CollectionTests [AssertionDiagnostic(@"var array = new string[1, 1]; array.Length.Should().Be(0{0});")] [AssertionDiagnostic(@"var array = new string[2, 2]; array.Length.Should().Be(0{0});")] [AssertionDiagnostic(@"var array = new string[3, 3, 3]; array.Length.Should().Be(0{0});")] - [AssertionDiagnostic(@"int[] array1 = [1, 2, 3]; int[] array2 = [4, 5, 6]; var both = array1.Concat(array2).ToArray(); (array1.Length + array2.Length).Should().Be(both.Length{0});")] + [AssertionDiagnostic(@"int[] array1 = [1, 2, 3]; int[] array2 = [4, 5, 6]; int[] both = [..array1, ..array2]; (array1.Length + array2.Length).Should().Be(both.Length{0});")] [Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/309")] public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder() .AppendLine("using System;") From 50491484f9ccb6cd2a5e24bd45986a8663ef4b8f Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 7 Oct 2024 22:46:17 +0300 Subject: [PATCH 4/4] bugfix: false positive on expressions containing Count --- .../Tips/FluentAssertionsAnalyzer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs index 278b8b7..b63b97c 100644 --- a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs +++ b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs @@ -334,7 +334,8 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs } } - if (invocation.TryGetFirstDescendent(out var propertyBeforeShould)) + var argument = invocation.Arguments[0].Value.UnwrapConversion(); + if (argument is IPropertyReferenceOperation propertyBeforeShould) { switch (propertyBeforeShould.Property.Name) {