Skip to content

Commit

Permalink
Do not report fluentassertion diagnostic when there is a condition ex…
Browse files Browse the repository at this point in the history
…pression before the Should invocation (#279)
  • Loading branch information
Meir017 committed Jan 4, 2024
1 parent 992a2ca commit ca48078
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public class OtherComponent
}

[TestMethod]
[NotImplemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/276")]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/276")]
public void ShouldNotFailToAnalyze2()
{
const string source = @"
Expand All @@ -434,34 +434,18 @@ public class MyResponse
public IEnumerable<MyCollectionType>? MyCollection { get; set; }
}
public class MyCollectionType { }";
const string fixedSource = @"
#nullable enable
using FluentAssertions;
using FluentAssertions.Extensions;
using System.Linq;
using System.Collections.Generic;
public class TestClass
{
public static void Main()
{
var response = new MyResponse();
response.MyCollection?.Should().HaveCount(2);
}
}

public class MyResponse
{
public IEnumerable<MyCollectionType>? MyCollection { get; set; }
}
public class MyCollectionType { }";

DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithSources(source)
.WithFixedSources(fixedSource)
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithAllAnalyzers()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
.WithExpectedDiagnostics(new DiagnosticResult()
{
Id = NullConditionalAssertionAnalyzer.DiagnosticId,
Message = NullConditionalAssertionAnalyzer.Message,
Severity = DiagnosticSeverity.Warning,
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 13, 9) }
})
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
{
return;
}

if (assertion.Parent.Kind is OperationKind.ConditionalAccess)
{
return; // Handled by NullConditionalAssertionAnalyzer
}

var subject = invocation.Arguments[0].Value;

Expand Down

0 comments on commit ca48078

Please sign in to comment.