Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not simplify default expression (RCS1244) #966

Merged
merged 2 commits into from
Oct 15, 2022
Merged
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 @@ -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