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

Fix property/field lazy init (RCS1250) #1205

Merged
merged 1 commit into from Sep 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix [RCS1164](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1164) ([#1196](https://github.com/JosefPihrt/Roslynator/pull/1196)).
- Fix [RCS1241](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1241) ([#1197](https://github.com/JosefPihrt/Roslynator/pull/1197)).
- Fix [RCS1250](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1250) ([#1205](https://github.com/JosefPihrt/Roslynator/pull/1205)).

## [4.5.0] - 2023-08-27

Expand Down
Expand Up @@ -215,11 +215,29 @@ private static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext co
}
case SyntaxKind.CoalesceExpression:
{
if (UseImplicitObjectCreationWhenTypeIsNotObvious(context))
ObjectCreationTypeStyle style = context.GetObjectCreationTypeStyle();

if (style == ObjectCreationTypeStyle.Implicit)
{
var coalesceExpression = (BinaryExpressionSyntax)parent;
AnalyzeExpression(context, objectCreation, coalesceExpression.Left);
}
else if (style == ObjectCreationTypeStyle.ImplicitWhenTypeIsObvious
&& parent.IsParentKind(SyntaxKind.EqualsValueClause))
{
if (parent.Parent.Parent is VariableDeclaratorSyntax variableDeclarator)
{
if (variableDeclarator.Parent is VariableDeclarationSyntax variableDeclaration
&& variableDeclaration.IsParentKind(SyntaxKind.FieldDeclaration))
{
AnalyzeType(context, objectCreation, variableDeclaration.Type);
}
}
else if (parent.Parent.Parent is PropertyDeclarationSyntax propertyDeclaration)
{
AnalyzeType(context, objectCreation, propertyDeclaration.Type);
}
}

break;
}
Expand Down Expand Up @@ -391,8 +409,40 @@ private static void AnalyzeImplicitObjectCreationExpression(SyntaxNodeAnalysisCo
case SyntaxKind.CoalesceAssignmentExpression:
case SyntaxKind.AddAssignmentExpression:
case SyntaxKind.SubtractAssignmentExpression:
{
if (UseExplicitObjectCreationWhenTypeIsNotObvious(context))
ReportDiagnostic(context, implicitObjectCreation);

break;
}
case SyntaxKind.CoalesceExpression:
{
if (parent.IsParentKind(SyntaxKind.EqualsValueClause))
{
switch (parent.Parent.Parent)
{
case VariableDeclaratorSyntax variableDeclarator:
{
if (variableDeclarator.Parent is VariableDeclarationSyntax)
{
if (UseExplicitObjectCreation(context))
ReportDiagnostic(context, implicitObjectCreation);

return;
}

break;
}
case PropertyDeclarationSyntax:
{
if (UseExplicitObjectCreation(context))
ReportDiagnostic(context, implicitObjectCreation);

return;
}
}
}

if (UseExplicitObjectCreationWhenTypeIsNotObvious(context))
ReportDiagnostic(context, implicitObjectCreation);

Expand Down
Expand Up @@ -853,6 +853,62 @@ void M()
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task Test_PreferImplicitWhenTypeIsObvious_PropertyLazyInitialization()
{
await VerifyDiagnosticAndFixAsync(@"
#nullable enable

public record R(C? P = null)
{
public C P { get; init; } = P ?? new [|C|]();
}

public class C
{
}
", @"
#nullable enable

public record R(C? P = null)
{
public C P { get; init; } = P ?? new();
}

public class C
{
}
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task Test_PreferImplicitWhenTypeIsObvious_FieldLazyInitialization()
{
await VerifyDiagnosticAndFixAsync(@"
#nullable enable

public record R(C? P = null)
{
public C F = P ?? new [|C|]();
}

public class C
{
}
", @"
#nullable enable

public record R(C? P = null)
{
public C F = P ?? new();
}

public class C
{
}
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious));
}

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