Skip to content

Commit ca48078

Browse files
authored
Do not report fluentassertion diagnostic when there is a condition expression before the Should invocation (#279)
1 parent 992a2ca commit ca48078

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

Diff for: src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs

+10-26
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public class OtherComponent
410410
}
411411

412412
[TestMethod]
413-
[NotImplemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/276")]
413+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/276")]
414414
public void ShouldNotFailToAnalyze2()
415415
{
416416
const string source = @"
@@ -434,34 +434,18 @@ public class MyResponse
434434
public IEnumerable<MyCollectionType>? MyCollection { get; set; }
435435
}
436436
public class MyCollectionType { }";
437-
const string fixedSource = @"
438-
#nullable enable
439-
using FluentAssertions;
440-
using FluentAssertions.Extensions;
441-
using System.Linq;
442-
using System.Collections.Generic;
443-
444-
public class TestClass
445-
{
446-
public static void Main()
447-
{
448-
var response = new MyResponse();
449-
response.MyCollection?.Should().HaveCount(2);
450-
}
451-
}
452437

453-
public class MyResponse
454-
{
455-
public IEnumerable<MyCollectionType>? MyCollection { get; set; }
456-
}
457-
public class MyCollectionType { }";
458-
459-
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
438+
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
460439
.WithSources(source)
461-
.WithFixedSources(fixedSource)
462-
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
463-
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
440+
.WithAllAnalyzers()
464441
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
442+
.WithExpectedDiagnostics(new DiagnosticResult()
443+
{
444+
Id = NullConditionalAssertionAnalyzer.DiagnosticId,
445+
Message = NullConditionalAssertionAnalyzer.Message,
446+
Severity = DiagnosticSeverity.Warning,
447+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 13, 9) }
448+
})
465449
);
466450
}
467451
}

Diff for: src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
4949
{
5050
return;
5151
}
52+
53+
if (assertion.Parent.Kind is OperationKind.ConditionalAccess)
54+
{
55+
return; // Handled by NullConditionalAssertionAnalyzer
56+
}
5257

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

0 commit comments

Comments
 (0)