Skip to content

Commit

Permalink
Fix RCS1084 (#1085)
Browse files Browse the repository at this point in the history
Co-authored-by: Josef Pihrt <josef.pihrt@outlook.com>
  • Loading branch information
jamesHargreaves12 and josefpihrt authored Jun 1, 2023
1 parent ee6668f commit 31bf7e7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Improve inversion of logical expressions to handling additional cases ([#1086](https://github.com/josefpihrt/roslynator/pull/1086)).
- Fix [RCS1084](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1084.md) ([#1085](https://github.com/josefpihrt/roslynator/pull/1085)).

## [4.3.0] - 2023-04-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ public static async Task<Document> RefactorAsync(
//RCS1084 UseCoalesceExpressionInsteadOfConditionalExpression
newNode = nullCheck.Expression;
coalesce = true;

// If the types are polymorphic then the LHS of the null coalesce must be cast to the base type.
ITypeSymbol newNodeType = semanticModel.GetTypeSymbol(newNode);
ITypeSymbol whenNullType = semanticModel.GetTypeSymbol(whenNull);
ITypeSymbol overallType = semanticModel.GetTypeInfo(conditionalExpression).ConvertedType;

if (overallType?.SupportsExplicitDeclaration() == true
&& !SymbolEqualityComparer.Default.Equals(newNodeType, whenNullType))
{
TypeSyntax castType = overallType.ToTypeSyntax().WithSimplifierAnnotation();

if ((semanticModel.GetNullableContext(conditionalExpression.SpanStart) & NullableContext.AnnotationsEnabled) != 0)
castType = NullableType(castType);

newNode = CastExpression(
castType,
newNode.WithoutTrivia())
.WithTriviaFrom(newNode);
}
}
else if (semanticModel
.GetTypeSymbol(nullCheck.Expression, cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,88 @@ void M()
}
", source, expected);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfConditionalExpression)]
public async Task Test_PolymorphicType_WithNullable()
{
await VerifyDiagnosticAndFixAsync(@"
#nullable enable
class C
{
private interface IBase { }
private class A: IBase { }
private class B: IBase { }
void M()
{
A? a = null;
IBase c = [|a != null ? a : new B()|];
}
}
",@"
#nullable enable
class C
{
private interface IBase { }
private class A: IBase { }
private class B: IBase { }
void M()
{
A? a = null;
IBase c = (IBase?)a ?? new B();
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfConditionalExpression)]
public async Task Test_PolymorphicType()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
private interface IBase { }
private class A: IBase { }
private class B: IBase { }
void M()
{
A a = null;
IBase c = [|a != null ? a : new B()|];
}
}
",@"
class C
{
private interface IBase { }
private class A: IBase { }
private class B: IBase { }
void M()
{
A a = null;
IBase c = (IBase)a ?? new B();
}
}
");
}

[Theory, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfConditionalExpression)]
[InlineData("(ni != null) ? ni.Value : 1", "ni ?? 1")]
Expand Down

0 comments on commit 31bf7e7

Please sign in to comment.