Skip to content

Commit dc7bf9d

Browse files
authored
Fix RCS1202 - check if expr flow state is not null (#1542)
1 parent b76aee0 commit dc7bf9d

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix analyzer [RCS1202](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1202) ([PR](https://github.com/dotnet/roslynator/pull/1542))
13+
1014
## [4.12.6] - 2024-09-23
1115

1216
### Added

src/Analyzers/CSharp/Analysis/AvoidNullReferenceExceptionAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ private static void AnalyzeAsExpression(SyntaxNodeAnalysisContext context)
198198
if (topExpression is null)
199199
return;
200200

201+
if (semanticModel.GetTypeInfo(expression, cancellationToken).Nullability.FlowState == NullableFlowState.NotNull)
202+
return;
203+
201204
if (semanticModel
202205
.GetTypeSymbol(asExpression, cancellationToken)?
203206
.IsReferenceType != true)

src/Tests/Analyzers.Tests/RCS1202AvoidNullReferenceExceptionTests2.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,34 @@ public void M()
146146
(this as I).M();
147147
}
148148
}
149+
");
150+
}
151+
152+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidNullReferenceException)]
153+
public async Task TestNoDiagnostic_ExpressionIsDefinitelyNotNull()
154+
{
155+
await VerifyNoDiagnosticAsync(@"
156+
#nullable enable
157+
158+
public interface I
159+
{
160+
void M() { }
161+
}
162+
163+
public class P : I;
164+
165+
public class C
166+
{
167+
public required P P { get; set; }
168+
169+
public void M()
170+
{
171+
if (this.P is not null)
172+
{
173+
(this.P as I).M();
174+
}
175+
}
176+
}
149177
");
150178
}
151179
}

0 commit comments

Comments
 (0)