Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update
  • Loading branch information
josefpihrt committed Sep 27, 2024
commit ab07f3a9cb7e2dfb51ce3ec0f072d926ae4400b2
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();
}
}
}
");
}
}