Skip to content

Commit

Permalink
Add analyzer ConvertExpressionBodyToBlockBody (RCS1016i)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Mar 13, 2021
1 parent 089dbed commit 67a0fc5
Show file tree
Hide file tree
Showing 9 changed files with 972 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/Analyzers/Analyzers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,22 @@ foreach (var item in items) // [|Id|]
</Sample>
</Samples>
</Option>
<Option Identifier="ConvertExpressionBodyToBlockBody">
<Title>Convert expression-body to block body.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
<Before><![CDATA[public string Foo() => null; // [|Id|]]]></Before>
<After><![CDATA[public string Foo()
{
return null;
}
]]></After>
</Sample>
</Samples>
</Option>
</Options>
</Analyzer>
<Analyzer Identifier="AvoidMultilineExpressionBody">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
if (!analysis.Success)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& methodDeclaration.SyntaxTree.IsMultiLineSpan(methodDeclaration.HeaderSpan()))
{
Expand All @@ -80,6 +83,12 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& methodDeclaration.SyntaxTree.IsMultiLineSpan(methodDeclaration.HeaderSpan()))
{
Expand Down Expand Up @@ -107,6 +116,12 @@ private static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& propertyDeclaration.SyntaxTree.IsMultiLineSpan(propertyDeclaration.HeaderSpan()))
{
Expand All @@ -133,6 +148,12 @@ private static void AnalyzeIndexerDeclaration(SyntaxNodeAnalysisContext context)

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& indexerDeclaration.SyntaxTree.IsMultiLineSpan(indexerDeclaration.HeaderSpan()))
{
Expand Down Expand Up @@ -166,6 +187,9 @@ private static void AnalyzeOperatorDeclaration(SyntaxNodeAnalysisContext context
if (!analysis.Success)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& operatorDeclaration.SyntaxTree.IsMultiLineSpan(operatorDeclaration.HeaderSpan()))
{
Expand All @@ -180,6 +204,12 @@ private static void AnalyzeOperatorDeclaration(SyntaxNodeAnalysisContext context

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& operatorDeclaration.SyntaxTree.IsMultiLineSpan(operatorDeclaration.HeaderSpan()))
{
Expand Down Expand Up @@ -214,6 +244,9 @@ private static void AnalyzeConversionOperatorDeclaration(SyntaxNodeAnalysisConte
if (!analysis.Success)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& operatorDeclaration.SyntaxTree.IsMultiLineSpan(operatorDeclaration.HeaderSpan()))
{
Expand All @@ -228,6 +261,12 @@ private static void AnalyzeConversionOperatorDeclaration(SyntaxNodeAnalysisConte

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& operatorDeclaration.SyntaxTree.IsMultiLineSpan(operatorDeclaration.HeaderSpan()))
{
Expand Down Expand Up @@ -262,6 +301,9 @@ private static void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext cont
if (!analysis.Success)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& constructorDeclaration.SyntaxTree.IsMultiLineSpan(constructorDeclaration.HeaderSpan()))
{
Expand All @@ -276,6 +318,12 @@ private static void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext cont

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& constructorDeclaration.SyntaxTree.IsMultiLineSpan(constructorDeclaration.HeaderSpan()))
{
Expand Down Expand Up @@ -310,6 +358,9 @@ private static void AnalyzeDestructorDeclaration(SyntaxNodeAnalysisContext conte
if (!analysis.Success)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& destructorDeclaration.SyntaxTree.IsMultiLineSpan(destructorDeclaration.HeaderSpan()))
{
Expand All @@ -324,6 +375,12 @@ private static void AnalyzeDestructorDeclaration(SyntaxNodeAnalysisContext conte

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& destructorDeclaration.SyntaxTree.IsMultiLineSpan(destructorDeclaration.HeaderSpan()))
{
Expand Down Expand Up @@ -358,6 +415,9 @@ private static void AnalyzeLocalFunctionStatement(SyntaxNodeAnalysisContext cont
if (!analysis.Success)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& localFunction.SyntaxTree.IsMultiLineSpan(localFunction.HeaderSpan()))
{
Expand All @@ -372,6 +432,12 @@ private static void AnalyzeLocalFunctionStatement(SyntaxNodeAnalysisContext cont

if (expressionBody?.ContainsDirectives == false)
{
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine.IsEnabled(context)
&& localFunction.SyntaxTree.IsMultiLineSpan(localFunction.HeaderSpan()))
{
Expand Down Expand Up @@ -405,11 +471,19 @@ private static void AnalyzeAccessorDeclaration(SyntaxNodeAnalysisContext context
{
ArrowExpressionClauseSyntax expressionBody = accessor.ExpressionBody;

if (expressionBody?.ContainsDirectives == false
&& AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine.IsEnabled(context)
&& expressionBody.Expression?.IsMultiLine() == true)
if (expressionBody?.ContainsDirectives == false)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine, expressionBody);
if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBody, expressionBody);
return;
}

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine.IsEnabled(context)
&& expressionBody.Expression?.IsMultiLine() == true)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.ReportOnly.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine, expressionBody);
}
}
}
}
Expand All @@ -434,6 +508,9 @@ private static void AnalyzeAccessorDeclarationBlock(
if (expression == null)
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBody.IsEnabled(context))
return;

if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine.IsEnabled(context)
&& expression.IsMultiLine())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public static partial class AnalyzerOptionDiagnosticDescriptors
helpLinkUri: AnalyzerOptionDiagnosticIdentifiers.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine,
customTags: Array.Empty<string>());

/// <summary>RCS1016i</summary>
public static readonly DiagnosticDescriptor ConvertExpressionBodyToBlockBody = DiagnosticDescriptorFactory.Default.Create(
id: AnalyzerOptionDiagnosticIdentifiers.ConvertExpressionBodyToBlockBody,
title: "Convert expression-body to block body.",
messageFormat: "Convert expression-body to block body.",
category: DiagnosticCategories.AnalyzerOption,
defaultSeverity: DiagnosticSeverity.Hidden,
isEnabledByDefault: false,
description: null,
helpLinkUri: AnalyzerOptionDiagnosticIdentifiers.ConvertExpressionBodyToBlockBody,
customTags: Array.Empty<string>());

/// <summary>RCS1018i</summary>
public static readonly DiagnosticDescriptor RemoveAccessibilityModifiers = DiagnosticDescriptorFactory.Default.Create(
id: AnalyzerOptionDiagnosticIdentifiers.RemoveAccessibilityModifiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static partial class AnalyzerOptionDiagnosticIdentifiers
public const string UseImplicitlyTypedArray = "RCS1014i";
public const string ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine = "RCS1016a";
public const string ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine = "RCS1016b";
public const string ConvertExpressionBodyToBlockBody = "RCS1016i";
public const string RemoveAccessibilityModifiers = "RCS1018i";
public const string RemoveEmptyLineBetweenClosingBraceAndSwitchSection = "RCS1036a";
public const string DoNotRenamePrivateStaticReadOnlyFieldToCamelCaseWithUnderscore = "RCS1045a";
Expand Down
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/AnalyzerOptions.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static partial class AnalyzerOptions
internal static readonly AnalyzerOptionDescriptor UseIsNullPatternInsteadOfInequalityOperator = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.UseIsNullPatternInsteadOfInequalityOperator, DiagnosticDescriptors.UseIsNullPatternInsteadOfComparisonOrViceVersa, "roslynator.RCS1248.enable_inequality_operator");
internal static readonly AnalyzerOptionDescriptor ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine, DiagnosticDescriptors.ConvertBlockBodyToExpressionBodyOrViceVersa, "roslynator.RCS1016.use_block_body_when_declaration_is_multiline");
internal static readonly AnalyzerOptionDescriptor UseImplicitlyTypedArray = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.UseImplicitlyTypedArray, DiagnosticDescriptors.UseExplicitlyTypedArrayOrViceVersa, "roslynator.RCS1014.invert");
internal static readonly AnalyzerOptionDescriptor ConvertExpressionBodyToBlockBody = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBody, DiagnosticDescriptors.ConvertBlockBodyToExpressionBodyOrViceVersa, "roslynator.RCS1016.invert");
internal static readonly AnalyzerOptionDescriptor RemoveAccessibilityModifiers = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.RemoveAccessibilityModifiers, DiagnosticDescriptors.AddAccessibilityModifiersOrViceVersa, "roslynator.RCS1018.invert");
internal static readonly AnalyzerOptionDescriptor RemoveArgumentListFromObjectCreation = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.RemoveArgumentListFromObjectCreation, DiagnosticDescriptors.AddArgumentListToObjectCreationOrViceVersa, "roslynator.RCS1050.invert");
internal static readonly AnalyzerOptionDescriptor UseStringEmptyInsteadOfEmptyStringLiteral = new AnalyzerOptionDescriptor(AnalyzerOptionDiagnosticDescriptors.UseStringEmptyInsteadOfEmptyStringLiteral, DiagnosticDescriptors.UseEmptyStringLiteralInsteadOfStringEmptyOrViceVersa, "roslynator.RCS1078.invert");
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/CSharp/AnalyzerOptionsAnalyzer.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
return ImmutableArray.Create(AnalyzerOptionDiagnosticDescriptors.UseImplicitlyTypedArrayWhenTypeIsObvious, AnalyzerOptionDiagnosticDescriptors.UseImplicitlyTypedArray, AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine, AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine, AnalyzerOptionDiagnosticDescriptors.RemoveAccessibilityModifiers, AnalyzerOptionDiagnosticDescriptors.RemoveEmptyLineBetweenClosingBraceAndSwitchSection, AnalyzerOptionDiagnosticDescriptors.DoNotRenamePrivateStaticReadOnlyFieldToCamelCaseWithUnderscore, AnalyzerOptionDiagnosticDescriptors.RemoveArgumentListFromObjectCreation, AnalyzerOptionDiagnosticDescriptors.RemoveParenthesesFromConditionOfConditionalExpressionWhenExpressionIsSingleToken, AnalyzerOptionDiagnosticDescriptors.UseStringEmptyInsteadOfEmptyStringLiteral, AnalyzerOptionDiagnosticDescriptors.RemoveCallToConfigureAwait, AnalyzerOptionDiagnosticDescriptors.ConvertBitwiseOperationToHasFlagCall, AnalyzerOptionDiagnosticDescriptors.SimplifyConditionalExpressionWhenItIncludesNegationOfCondition, AnalyzerOptionDiagnosticDescriptors.ConvertMethodGroupToAnonymousFunction, AnalyzerOptionDiagnosticDescriptors.DoNotUseElementAccessWhenExpressionIsInvocation, AnalyzerOptionDiagnosticDescriptors.UseIsNullPatternInsteadOfInequalityOperator, AnalyzerOptionDiagnosticDescriptors.UseComparisonInsteadOfIsNullPattern);
return ImmutableArray.Create(AnalyzerOptionDiagnosticDescriptors.UseImplicitlyTypedArrayWhenTypeIsObvious, AnalyzerOptionDiagnosticDescriptors.UseImplicitlyTypedArray, AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine, AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine, AnalyzerOptionDiagnosticDescriptors.ConvertExpressionBodyToBlockBody, AnalyzerOptionDiagnosticDescriptors.RemoveAccessibilityModifiers, AnalyzerOptionDiagnosticDescriptors.RemoveEmptyLineBetweenClosingBraceAndSwitchSection, AnalyzerOptionDiagnosticDescriptors.DoNotRenamePrivateStaticReadOnlyFieldToCamelCaseWithUnderscore, AnalyzerOptionDiagnosticDescriptors.RemoveArgumentListFromObjectCreation, AnalyzerOptionDiagnosticDescriptors.RemoveParenthesesFromConditionOfConditionalExpressionWhenExpressionIsSingleToken, AnalyzerOptionDiagnosticDescriptors.UseStringEmptyInsteadOfEmptyStringLiteral, AnalyzerOptionDiagnosticDescriptors.RemoveCallToConfigureAwait, AnalyzerOptionDiagnosticDescriptors.ConvertBitwiseOperationToHasFlagCall, AnalyzerOptionDiagnosticDescriptors.SimplifyConditionalExpressionWhenItIncludesNegationOfCondition, AnalyzerOptionDiagnosticDescriptors.ConvertMethodGroupToAnonymousFunction, AnalyzerOptionDiagnosticDescriptors.DoNotUseElementAccessWhenExpressionIsInvocation, AnalyzerOptionDiagnosticDescriptors.UseIsNullPatternInsteadOfInequalityOperator, AnalyzerOptionDiagnosticDescriptors.UseComparisonInsteadOfIsNullPattern);
}
}

Expand Down
Loading

0 comments on commit 67a0fc5

Please sign in to comment.