Skip to content

Commit

Permalink
Improve analyzer SimplifyCoalesceExpression (RCS1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Feb 1, 2020
1 parent 88dd4ce commit 4c4281e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static BinaryExpressionPart GetRedundantPart(
return BinaryExpressionPart.Left;
case SyntaxKind.DefaultExpression:
{
if (IsDefaultOfReferenceType((DefaultExpressionSyntax)left, semanticModel, cancellationToken))
if (IsDefaultOfReferenceOrNullableType((DefaultExpressionSyntax)left, semanticModel, cancellationToken))
return BinaryExpressionPart.Left;

break;
Expand Down Expand Up @@ -129,10 +129,12 @@ private static BinaryExpressionPart GetRedundantPart(
switch (rightKind)
{
case SyntaxKind.NullLiteralExpression:
return BinaryExpressionPart.Right;
{
return BinaryExpressionPart.Right;
}
case SyntaxKind.DefaultExpression:
{
if (IsDefaultOfReferenceType((DefaultExpressionSyntax)right, semanticModel, cancellationToken))
if (IsDefaultOfReferenceOrNullableType((DefaultExpressionSyntax)right, semanticModel, cancellationToken))
return BinaryExpressionPart.Right;

break;
Expand All @@ -148,12 +150,15 @@ private static BinaryExpressionPart GetRedundantPart(
return BinaryExpressionPart.None;
}

private static bool IsDefaultOfReferenceType(DefaultExpressionSyntax defaultExpression, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool IsDefaultOfReferenceOrNullableType(DefaultExpressionSyntax defaultExpression, SemanticModel semanticModel, CancellationToken cancellationToken)
{
TypeSyntax type = defaultExpression.Type;

if (type != null)
{
if (type.IsKind(SyntaxKind.NullableType))
return true;

ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken);

if (typeSymbol?.IsErrorType() == false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp.CodeFixes;
using Xunit;

namespace Roslynator.CSharp.Analysis.Tests
{
public class RCS1143SimplifyCoalesceExpressionTests : AbstractCSharpFixVerifier
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.SimplifyCoalesceExpression;

public override DiagnosticAnalyzer Analyzer { get; } = new SimplifyCoalesceExpressionAnalyzer();

public override CodeFixProvider FixProvider { get; } = new BinaryExpressionCodeFixProvider();

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyCoalesceExpression)]
public async Task Test_DefaultOfNullableType()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M()
{
C x = null;
int? y = x?.M2() [|?? default(int?)|];
}
int M2() => default;
}
", @"
class C
{
void M()
{
C x = null;
int? y = x?.M2();
}
int M2() => default;
}
");
}
}
}

0 comments on commit 4c4281e

Please sign in to comment.