Skip to content

Commit

Permalink
Fix RCS1244 (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Dec 1, 2022
1 parent 3c0ee27 commit dd6c4c4
Show file tree
Hide file tree
Showing 3 changed files with 23 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

- Fix [RCS1084](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1084.md) ([#1006](https://github.com/josefpihrt/roslynator/pull/1006)).
- Fix [RCS1244](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1244.md) ([#1007](https://github.com/josefpihrt/roslynator/pull/1007)).

## [4.2.0] - 2022-11-27

Expand Down
6 changes: 6 additions & 0 deletions src/Analyzers/CSharp/Analysis/DefaultExpressionAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public static void AnalyzeDefaultExpression(SyntaxNodeAnalysisContext context)
if (parent.IsParentKind(SyntaxKind.ObjectInitializerExpression))
return;

if (parent is BinaryExpressionSyntax coalesceExpression
&& coalesceExpression.Left == defaultExpression)
{
return;
}

TypeInfo typeInfo = context.SemanticModel.GetTypeInfo(expression, context.CancellationToken);

if (!SymbolEqualityComparer.Default.Equals(typeInfo.Type, typeInfo.ConvertedType))
Expand Down
16 changes: 16 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1244SimplifyDefaultExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,22 @@ void M()
};
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyDefaultExpression)]
public async Task TestNoDiagnostic_LambdaExpression2()
{
await VerifyNoDiagnosticAsync(@"
using System;
class C
{
T M<T>()
{
return default(T) ?? (T)Convert.ChangeType("""", typeof(T));
}
}
");
}
}

0 comments on commit dd6c4c4

Please sign in to comment.