Skip to content

Commit

Permalink
Convert more syntax to implicit object creation (RCS1250) (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Jun 2, 2022
1 parent 5d86817 commit 5e61531
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
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
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

0 comments on commit 5e61531

Please sign in to comment.