Skip to content

Commit

Permalink
Fix RCS1202 - check if expr flow state is not null (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Sep 27, 2024
1 parent b76aee0 commit dc7bf9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix analyzer [RCS1202](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1202) ([PR](https://github.com/dotnet/roslynator/pull/1542))

## [4.12.6] - 2024-09-23

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ private static void AnalyzeAsExpression(SyntaxNodeAnalysisContext context)
if (topExpression is null)
return;

if (semanticModel.GetTypeInfo(expression, cancellationToken).Nullability.FlowState == NullableFlowState.NotNull)
return;

if (semanticModel
.GetTypeSymbol(asExpression, cancellationToken)?
.IsReferenceType != true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,34 @@ public void M()
(this as I).M();
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidNullReferenceException)]
public async Task TestNoDiagnostic_ExpressionIsDefinitelyNotNull()
{
await VerifyNoDiagnosticAsync(@"
#nullable enable
public interface I
{
void M() { }
}
public class P : I;
public class C
{
public required P P { get; set; }
public void M()
{
if (this.P is not null)
{
(this.P as I).M();
}
}
}
");
}
}

0 comments on commit dc7bf9d

Please sign in to comment.