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

Convert more syntax to implicit object creation (RCS1250) #910

Merged
merged 3 commits into from
Jun 2, 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
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Unreleased

* Convert more syntax to implicit object creation (RCS1250) ([PR](https://github.com/JosefPihrt/Roslynator/pull/910))

### 4.1.1 (2022-05-29)

* Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,42 @@ private static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext co
}
case SyntaxKind.ArrayInitializerExpression:
{
SyntaxDebug.Assert(parent.IsParentKind(SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression), parent.Parent);
SyntaxDebug.Assert(parent.IsParentKind(SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.EqualsValueClause), parent.Parent);

if (UseImplicitObjectCreation(context)
&& parent.IsParentKind(SyntaxKind.ArrayCreationExpression))
if (UseImplicitObjectCreation(context))
{
var arrayCreationExpression = (ArrayCreationExpressionSyntax)parent.Parent;
if (parent.IsParentKind(SyntaxKind.ArrayCreationExpression))
{
var arrayCreationExpression = (ArrayCreationExpressionSyntax)parent.Parent;

AnalyzeType(context, objectCreation, arrayCreationExpression.Type.ElementType);
}
else if (parent.IsParentKind(SyntaxKind.EqualsValueClause))
{
parent = parent.Parent.Parent;

if (parent.IsKind(SyntaxKind.VariableDeclarator))
{
parent = parent.Parent;

AnalyzeType(context, objectCreation, arrayCreationExpression.Type.ElementType);
if (parent is VariableDeclarationSyntax variableDeclaration)
{
if (parent.IsParentKind(SyntaxKind.FieldDeclaration))
{
AnalyzeArrayType(context, objectCreation, variableDeclaration.Type);
}
else if (parent.IsParentKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.UsingStatement))
{
if (!variableDeclaration.Type.IsVar)
AnalyzeArrayType(context, objectCreation, variableDeclaration.Type);
}
}
}
else if (parent.IsKind(SyntaxKind.PropertyDeclaration))
{
AnalyzeArrayType(context, objectCreation, ((PropertyDeclarationSyntax)parent).Type);
}
}
}

break;
Expand Down Expand Up @@ -316,7 +344,7 @@ private void AnalyzeImplicitObjectCreationExpression(SyntaxNodeAnalysisContext c
}
case SyntaxKind.ArrayInitializerExpression:
{
SyntaxDebug.Assert(parent.IsParentKind(SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression), parent.Parent);
SyntaxDebug.Assert(parent.IsParentKind(SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.EqualsValueClause), parent.Parent);

if (UseExplicitObjectCreation(context)
&& parent.IsParentKind(SyntaxKind.ArrayCreationExpression))
Expand Down Expand Up @@ -450,6 +478,21 @@ private static void AnalyzeType(
}
}

private static void AnalyzeArrayType(
SyntaxNodeAnalysisContext context,
ObjectCreationExpressionSyntax objectCreation,
TypeSyntax type,
bool isGenericType = false)
{
if (type is ArrayTypeSyntax arrayType)
{
type = arrayType.ElementType;

if (!type.IsVar)
AnalyzeExpression(context, objectCreation, type, isGenericType: isGenericType);
}
}

private static void AnalyzeExpression(
SyntaxNodeAnalysisContext context,
ObjectCreationExpressionSyntax objectCreation,
Expand Down
1 change: 1 addition & 0 deletions src/CSharp/CSharp/Extensions/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3229,6 +3229,7 @@ public static bool IsInExpressionTree(
#if DEBUG
case SyntaxKind.Argument:
case SyntaxKind.ArgumentList:
case SyntaxKind.ArrayRankSpecifier:
case SyntaxKind.EqualsValueClause:
case SyntaxKind.Interpolation:
case SyntaxKind.SwitchExpressionArm:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,60 @@ void M()
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task Test_PreferImplicitWhenTypeIsObvious_ArrayInitializerInFieldInitializer()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
public string[] f = { new [|string|](' ', 0) };
}
", @"
class C
{
public string[] f = { new(' ', 0) };
}
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task Test_PreferImplicitWhenTypeIsObvious_ArrayInitializerInPropertyInitializer()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
public string[] P { get; } = { new [|string|](' ', 0) };
}
", @"
class C
{
public string[] P { get; } = { new(' ', 0) };
}
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task Test_PreferImplicitWhenTypeIsObvious_ArrayInitializerInLocalVariableInitializer()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M()
{
string[] x = { new [|string|](' ', 0) };
}
}
", @"
class C
{
void M()
{
string[] x = { new(' ', 0) };
}
}
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task Test_ConvertImplicitToExplicit_ThrowStatement()
{
Expand Down