Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix analyzer [RCS1249](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1249) to report null-forgiving operators on expressions that are already known to be non-null ([PR](https://github.com/dotnet/roslynator/pull/1834))
- [CLI] Reference the compiled output of referenced projects that cannot be loaded into the workspace (for example F# projects), so their types are no longer reported as missing (`CS0103`/`CS0246`) during analysis ([PR](https://github.com/dotnet/roslynator/pull/1833))

## [5.0.0] - 2026-08-21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private static void AnalyzeSuppressNullableWarningExpression(SyntaxNodeAnalysisC
}

context.ReportDiagnostic(DiagnosticRules.UnnecessaryNullForgivingOperator, suppressExpression.OperatorToken);
return;
}
}
else if (node.IsKind(SyntaxKind.EqualsValueClause))
Expand All @@ -80,7 +81,10 @@ private static void AnalyzeSuppressNullableWarningExpression(SyntaxNodeAnalysisC
var property = (PropertyDeclarationSyntax)node.Parent;

if (IsNullableReferenceType(context, property.Type))
{
context.ReportDiagnostic(DiagnosticRules.UnnecessaryNullForgivingOperator, node);
return;
}
}
else if (parent.IsKind(SyntaxKind.VariableDeclarator))
{
Expand All @@ -99,17 +103,32 @@ private static void AnalyzeSuppressNullableWarningExpression(SyntaxNodeAnalysisC
if (parent.Parent.IsParentKind(SyntaxKind.FieldDeclaration))
{
context.ReportDiagnostic(DiagnosticRules.UnnecessaryNullForgivingOperator, node);
return;
}
else
{
context.ReportDiagnostic(DiagnosticRules.UnnecessaryNullForgivingOperator, suppressExpression.OperatorToken);
return;
}
}
}
}
}
}

if (IsNotNullWithoutSuppression(context, suppressExpression))
context.ReportDiagnostic(DiagnosticRules.UnnecessaryNullForgivingOperator, suppressExpression.OperatorToken);

static bool IsNotNullWithoutSuppression(SyntaxNodeAnalysisContext context, PostfixUnaryExpressionSyntax suppressExpression)
{
TypeInfo typeInfo = context.SemanticModel.GetSpeculativeTypeInfo(
suppressExpression.SpanStart,
suppressExpression.Operand.WithoutTrivia(),
SpeculativeBindingOption.BindAsExpression);

return typeInfo.Nullability.FlowState == NullableFlowState.NotNull;
}

static bool IsNullableReferenceType(SyntaxNodeAnalysisContext context, TypeSyntax type)
{
if (!type.IsKind(SyntaxKind.NullableType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,96 @@ public class RCS1249UnnecessaryNullForgivingOperatorTests : AbstractCSharpDiagno
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.UnnecessaryNullForgivingOperator;

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryNullForgivingOperator)]
public async Task Test_NonNullExpressions()
{
await VerifyDiagnosticAndFixAsync("""
#nullable enable

class C
{
private readonly object _value = new object();

private object P => new object();

private object GetValue() => new object();

object M()
{
object value = "value"[|!|];
value = new()[|!|];
value = _value[|!|];
value = P[|!|];
value = GetValue()[|!|];
return value;
}
}
""", """
#nullable enable

class C
{
private readonly object _value = new object();

private object P => new object();

private object GetValue() => new object();

object M()
{
object value = "value";
value = new();
value = _value;
value = P;
value = GetValue();
return value;
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryNullForgivingOperator)]
public async Task Test_FlowStateNotNull()
{
await VerifyDiagnosticAndFixAsync(@"
#nullable enable

using System.Collections.Generic;

class C
{
object M(Stack<object> stack)
{
object? value;

while (!stack.TryPop(out value))
{
}

return value[|!|];
}
}
", @"
#nullable enable

using System.Collections.Generic;

class C
{
object M(Stack<object> stack)
{
object? value;

while (!stack.TryPop(out value))
{
}

return value;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryNullForgivingOperator)]
public async Task Test_Property()
{
Expand Down Expand Up @@ -166,6 +256,27 @@ void M(string p)
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryNullForgivingOperator)]
public async Task TestNoDiagnostic_MaybeNullExpression()
{
await VerifyNoDiagnosticAsync(@"
#nullable enable

class C
{
object M(object? value)
{
return value!;
}

object M2()
{
return default(object)!;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryNullForgivingOperator)]
public async Task TestNoDiagnostic_MaybeNullWhenAttribute()
{
Expand Down