Skip to content

Commit

Permalink
Do not simplify default expression (RCS1244) (dotnet#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored and JochemHarmes committed Oct 30, 2023
1 parent 44077d6 commit 96514c9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix formatting of argument list ([#952](https://github.com/josefpihrt/roslynator/pull/952).
- Do not remove async/await when 'using declaration' is used ([#953](https://github.com/josefpihrt/roslynator/pull/953).
- Convert if-else to return statement when pattern matching is used ([RCS1073](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1073.md)) ([#956](https://github.com/josefpihrt/roslynator/pull/956).
- Do not simplify 'default' expression if the type is inferred ([RCS1244](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/1244.md)) ([#956](https://github.com/josefpihrt/roslynator/pull/966).

-----
<!-- Content below does not adhere to 'Keep a Changelog' format -->
Expand Down
36 changes: 23 additions & 13 deletions src/Analyzers/CSharp/Analysis/DefaultExpressionAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,38 @@ private static bool IsTypeInferredFromDefaultExpression2(
{
SyntaxNode node = ((ExpressionSyntax)anonymousFunction).WalkUpParentheses().Parent;

if (node is ArgumentSyntax argument
&& node.IsParentKind(SyntaxKind.ArgumentList)
&& node.Parent.IsParentKind(SyntaxKind.InvocationExpression)
&& !IsGenericInvocation((InvocationExpressionSyntax)node.Parent.Parent))
if (node is ArgumentSyntax argument)
{
IParameterSymbol parameterSymbol = context.SemanticModel.DetermineParameter(argument);

if (parameterSymbol?.OriginalDefinition.Type is INamedTypeSymbol namedTypeSymbol)
if (node.IsParentKind(SyntaxKind.ArgumentList)
&& node.Parent.IsParentKind(SyntaxKind.InvocationExpression)
&& !IsGenericInvocation((InvocationExpressionSyntax)node.Parent.Parent))
{
ITypeParameterSymbol typeParameterSymbol = namedTypeSymbol.TypeParameters.LastOrDefault();
IParameterSymbol parameterSymbol = context.SemanticModel.DetermineParameter(argument);

if (typeParameterSymbol != null
&& parameterSymbol.ContainingSymbol.OriginalDefinition is IMethodSymbol methodSymbol2)
if (parameterSymbol?.OriginalDefinition.Type is INamedTypeSymbol namedTypeSymbol)
{
foreach (ITypeParameterSymbol typeParameterSymbol2 in methodSymbol2.TypeParameters)
ITypeParameterSymbol typeParameterSymbol = namedTypeSymbol.TypeParameters.LastOrDefault();

if (typeParameterSymbol != null
&& parameterSymbol.ContainingSymbol.OriginalDefinition is IMethodSymbol methodSymbol2)
{
if (string.Equals(typeParameterSymbol.Name, typeParameterSymbol2.Name, StringComparison.Ordinal))
return true;
foreach (ITypeParameterSymbol typeParameterSymbol2 in methodSymbol2.TypeParameters)
{
if (string.Equals(typeParameterSymbol.Name, typeParameterSymbol2.Name, StringComparison.Ordinal))
return true;
}
}
}
}
}
else if (node is EqualsValueClauseSyntax)
{
if (node.IsParentKind(SyntaxKind.VariableDeclarator)
&& node.Parent.Parent is VariableDeclarationSyntax variableDeclaration)
{
return variableDeclaration.Type.IsVar;
}
}
}

return false;
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1244SimplifyDefaultExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,23 @@ void M()
_ = default(C) == 0;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyDefaultExpression)]
public async Task TestNoDiagnostic_LambdaExpression()
{
await VerifyNoDiagnosticAsync(@"
class C
{
void M()
{
var x = () =>
{
return default(object);
};
}
}
");
}
}
Expand Down

0 comments on commit 96514c9

Please sign in to comment.