Skip to content

Commit

Permalink
Add support for EditorConfig options (fix #758)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Mar 12, 2021
1 parent 42c6792 commit da88ce6
Show file tree
Hide file tree
Showing 123 changed files with 1,618 additions and 1,234 deletions.
2 changes: 2 additions & 0 deletions src/Analyzers/Analyzers.Template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<Option Identifier="">
<Title></Title>
<Id></Id>
<Name></Name>
<FullName></FullName>
<Kind></Kind>
<IsEnabledByDefault>false</IsEnabledByDefault>
<SupportsFadeOut>false</SupportsFadeOut>
Expand Down
29 changes: 25 additions & 4 deletions src/Analyzers/Analyzers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ foreach (var item in items) // [|Id|]
<Option Identifier="UseImplicitlyTypedArrayWhenTypeIsObvious">
<Title>Use implicitly typed array (when type is obvious).</Title>
<Id>a</Id>
<OptionKey>use_implicit_type_when_obvious</OptionKey>
<Kind>Change</Kind>
<SupportsFadeOut>true</SupportsFadeOut>
<Samples>
Expand All @@ -275,6 +276,7 @@ foreach (var item in items) // [|Id|]
<Option Identifier="UseImplicitlyTypedArray">
<Title>Use implicitly typed array.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -334,6 +336,7 @@ foreach (var item in items) // [|Id|]
<Option Identifier="ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine">
<Title>Convert expression-body to block body when expression is multi-line.</Title>
<Id>a</Id>
<OptionKey>use_block_body_when_expression_is_multiline</OptionKey>
<Kind>Change</Kind>
<Samples>
<Sample>
Expand All @@ -358,6 +361,7 @@ foreach (var item in items) // [|Id|]
<Option Identifier="ConvertExpressionBodyToBlockBodyWhenDeclarationIsMultiLine">
<Title>Convert expression-body to block body when declaration is multi-line.</Title>
<Id>b</Id>
<OptionKey>use_block_body_when_declaration_is_multiline</OptionKey>
<Kind>Change</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -432,6 +436,7 @@ foreach (var item in items) // [|Id|]
<Option Identifier="RemoveAccessibilityModifiers">
<Title>Remove accessibility modifiers.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<SupportsFadeOut>true</SupportsFadeOut>
<Samples>
Expand Down Expand Up @@ -818,6 +823,7 @@ if (f)
<Option Identifier="RemoveEmptyLineBetweenClosingBraceAndSwitchSection">
<Id>a</Id>
<Title>Remove empty line between closing brace and switch section.</Title>
<OptionKey>remove_empty_line_between_closing_brace_and_switch_section</OptionKey>
<Kind>Enable</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -1012,6 +1018,7 @@ catch (Exception ex)
<Option Identifier="DoNotRenamePrivateStaticReadOnlyFieldToCamelCaseWithUnderscore">
<Title>Do not rename private static read-only field to camel case with underscore.</Title>
<Id>a</Id>
<OptionKey>suppress_private_static_readonly_field</OptionKey>
<Kind>Disable</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -1128,6 +1135,7 @@ if (!f)
<Option Identifier="RemoveArgumentListFromObjectCreation">
<Title>Remove argument list from object creation expression.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -1155,6 +1163,7 @@ if (!f)
<Id>a</Id>
<Title>Remove parentheses from condition of conditional expression (when condition is a single token).</Title>
<MessageFormat>Remove parentheses from condition of conditional expression.</MessageFormat>
<OptionKey>do_not_parenthesize_single_token</OptionKey>
<Kind>Change</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -1847,6 +1856,7 @@ object x = items.Peek();]]></After>
<Option Identifier="UseStringEmptyInsteadOfEmptyStringLiteral">
<Title>Use string.Empty instead of "".</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -2036,6 +2046,7 @@ public string Foo // [|Id|]
<Option Identifier="RemoveCallToConfigureAwait">
<Title>Remove call to 'ConfigureAwait'.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<SupportsFadeOut>true</SupportsFadeOut>
<Samples>
Expand Down Expand Up @@ -2139,6 +2150,7 @@ namespace Foo
<Option Identifier="ConvertBitwiseOperationToHasFlagCall">
<Title>Convert bitwise operation to 'HasFlag' call.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -2344,6 +2356,7 @@ else
<Option Identifier="SimplifyConditionalExpressionWhenItIncludesNegationOfCondition">
<Title>Simplify conditional expression (when it includes negation of condition).</Title>
<Id>a</Id>
<OptionKey>enable_negation_of_condition</OptionKey>
<Kind>Enable</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -4249,6 +4262,7 @@ int i = (x != null) ? x.Value.GetHashCode() : 0; // [|Id|]]]></Before>
<Option Identifier="ConvertMethodGroupToAnonymousFunction">
<Title>Convert method group to anonymous function.</Title>
<Id>i</Id>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -4428,10 +4442,14 @@ x = "";]]></Before>
<DefaultSeverity>Info</DefaultSeverity>
<IsEnabledByDefault>true</IsEnabledByDefault>
<SupportsFadeOut>true</SupportsFadeOut>
<Configuration>Use following EditorConfig option to suppress diagnostic from [Unity script methods](https://docs.unity3d.com/ScriptReference/MonoBehaviour.html):
```editorconfig
roslynator.RCS1213.suppress_unity_script_methods = true
```</Configuration>
<Options>
<Option Identifier="SuppressUnityScriptMethods">
<Title>Suppress Unity script methods.</Title>
<OptionKey>suppress_unity_script_methods</OptionKey>
<Kind>Disable</Kind>
<Summary>Suppress diagnostic from [Unity script methods](https://docs.unity3d.com/ScriptReference/MonoBehaviour.html).</Summary>
</Option>
</Options>
</Analyzer>
<Analyzer Identifier="UnnecessaryInterpolatedString">
<Id>RCS1214</Id>
Expand Down Expand Up @@ -5475,6 +5493,7 @@ public class C
<Option Identifier="DoNotUseElementAccessWhenExpressionIsInvocation">
<Title>Do not use element access when expression is invocation.</Title>
<Id>a</Id>
<OptionKey>suppress_when_expression_is_invocation</OptionKey>
<Kind>Disable</Kind>
<Samples>
<Sample>
Expand Down Expand Up @@ -5551,6 +5570,7 @@ void M()
<Option Identifier="UseIsNullPatternInsteadOfInequalityOperator">
<Id>a</Id>
<Title>Use 'is null' pattern instead of '!=' operator.</Title>
<OptionKey>enable_inequality_operator</OptionKey>
<Kind>Enable</Kind>
<Samples>
<Sample>
Expand All @@ -5562,6 +5582,7 @@ void M()
<Option Identifier="UseComparisonInsteadOfIsNullPattern">
<Id>i</Id>
<Title>Use comparison instead of 'is null' pattern.</Title>
<OptionKey>invert</OptionKey>
<Kind>Invert</Kind>
<Samples>
<Sample>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context, MemberDeclaration

if (explicitAccessibility == Accessibility.NotApplicable)
{
if (!context.IsAnalyzerSuppressed(AnalyzerOptions.RemoveAccessibilityModifiers))
if (AnalyzerOptions.RemoveAccessibilityModifiers.IsEnabled(context))
return;

Accessibility accessibility = GetAccessibility(context, declaration, modifiers);
Expand All @@ -188,7 +188,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context, MemberDeclaration
location,
Properties[accessibility]);
}
else if (!context.IsAnalyzerSuppressed(AnalyzerOptions.RemoveAccessibilityModifiers)
else if (AnalyzerOptions.RemoveAccessibilityModifiers.IsEnabled(context)
&& !declaration.IsKind(SyntaxKind.OperatorDeclaration, SyntaxKind.ConversionOperatorDeclaration))
{
Accessibility accessibility = SyntaxAccessibility.GetDefaultAccessibility(declaration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext co

if (argumentList == null)
{
if (context.IsAnalyzerSuppressed(AnalyzerOptions.RemoveArgumentListFromObjectCreation))
if (!AnalyzerOptions.RemoveArgumentListFromObjectCreation.IsEnabled(context))
{
var span = new TextSpan(objectCreationExpression.Type.Span.End, 0);

Expand All @@ -49,7 +49,7 @@ private static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext co
}
}
else if (!argumentList.Arguments.Any()
&& !context.IsAnalyzerSuppressed(AnalyzerOptions.RemoveArgumentListFromObjectCreation))
&& AnalyzerOptions.RemoveArgumentListFromObjectCreation.IsEnabled(context))
{
SyntaxToken openParen = argumentList.OpenParenToken;
SyntaxToken closeParen = argumentList.CloseParenToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ public override void Initialize(AnalysisContext context)
{
base.Initialize(context);

context.RegisterCompilationStartAction(startContext =>
{
if (startContext.IsAnalyzerSuppressed(AnalyzerOptions.RemoveCallToConfigureAwait))
{
if (startContext.Compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1") == null)
return;

startContext.RegisterSyntaxNodeAction(f => AddCallToConfigureAwait(f), SyntaxKind.AwaitExpression);
}
else
context.RegisterSyntaxNodeAction(
c =>
{
startContext.RegisterSyntaxNodeAction(f => RemoveCallToConfigureAwait(f), SyntaxKind.AwaitExpression);
}
});
if (AnalyzerOptions.RemoveCallToConfigureAwait.IsEnabled(c))
{
RemoveCallToConfigureAwait(c);
}
else if (c.Compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1") != null)
{
AddCallToConfigureAwait(c);
}
},
SyntaxKind.AwaitExpression);
}

private static void AddCallToConfigureAwait(SyntaxNodeAnalysisContext context)
Expand Down
14 changes: 7 additions & 7 deletions src/Analyzers/CSharp/Analysis/AnonymousMethodAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public override void Initialize(AnalysisContext context)
{
base.Initialize(context);

context.RegisterCompilationStartAction(startContext =>
{
if (startContext.IsAnalyzerSuppressed(DiagnosticDescriptors.UseLambdaExpressionInsteadOfAnonymousMethod))
return;

startContext.RegisterSyntaxNodeAction(f => AnalyzeAnonymousMethod(f), SyntaxKind.AnonymousMethodExpression);
});
context.RegisterSyntaxNodeAction(
c =>
{
if (DiagnosticDescriptors.UseLambdaExpressionInsteadOfAnonymousMethod.IsEffective(c))
AnalyzeAnonymousMethod(c);
},
SyntaxKind.AnonymousMethodExpression);
}

private static void AnalyzeAnonymousMethod(SyntaxNodeAnalysisContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ public override void Initialize(AnalysisContext context)
{
base.Initialize(context);

context.RegisterCompilationStartAction(startContext =>
{
if (!startContext.IsAnalyzerSuppressed(DiagnosticDescriptors.ConvertBlockBodyToExpressionBodyOrViceVersa)
&& !startContext.IsAnalyzerSuppressed(AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine))
context.RegisterSyntaxNodeAction(
c =>
{
return;
}

startContext.RegisterSyntaxNodeAction(f => AnalyzeArrowExpressionClause(f), SyntaxKind.ArrowExpressionClause);
});
if (AnalyzerOptions.ConvertExpressionBodyToBlockBodyWhenExpressionIsMultiLine.IsEnabled(c, checkParent: true) == false)
AnalyzeArrowExpressionClause(c);
},
SyntaxKind.ArrowExpressionClause);
}

private static void AnalyzeArrowExpressionClause(SyntaxNodeAnalysisContext context)
Expand Down
44 changes: 27 additions & 17 deletions src/Analyzers/CSharp/Analysis/BinaryOperatorAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,33 @@ public override void Initialize(AnalysisContext context)
{
base.Initialize(context);

context.RegisterCompilationStartAction(startContext =>
{
if (!startContext.IsAnalyzerSuppressed(DiagnosticDescriptors.UnnecessaryOperator))
context.RegisterSyntaxNodeAction(
c =>
{
startContext.RegisterSyntaxNodeAction(f => AnalyzeLessThanExpression(f), SyntaxKind.LessThanExpression);
startContext.RegisterSyntaxNodeAction(f => AnalyzeGreaterThanExpression(f), SyntaxKind.GreaterThanExpression);
}
if (DiagnosticDescriptors.UnnecessaryOperator.IsEffective(c))
AnalyzeLessThanExpression(c);
},
SyntaxKind.LessThanExpression);

startContext.RegisterSyntaxNodeAction(f => AnalyzeLessThanOrEqualExpression(f), SyntaxKind.LessThanOrEqualExpression);
startContext.RegisterSyntaxNodeAction(f => AnalyzeGreaterThanOrEqualExpression(f), SyntaxKind.GreaterThanOrEqualExpression);
startContext.RegisterSyntaxNodeAction(f => AnalyzeLogicalOrExpression(f), SyntaxKind.LogicalOrExpression);
context.RegisterSyntaxNodeAction(
c =>
{
if (DiagnosticDescriptors.UnnecessaryOperator.IsEffective(c))
AnalyzeGreaterThanExpression(c);
},
SyntaxKind.GreaterThanExpression);

if (!startContext.IsAnalyzerSuppressed(DiagnosticDescriptors.ExpressionIsAlwaysEqualToTrueOrFalse))
context.RegisterSyntaxNodeAction(
c =>
{
startContext.RegisterSyntaxNodeAction(f => AnalyzeSimpleMemberAccessExpression(f), SyntaxKind.SimpleMemberAccessExpression);
}
});
if (DiagnosticDescriptors.ExpressionIsAlwaysEqualToTrueOrFalse.IsEffective(c))
AnalyzeSimpleMemberAccessExpression(c);
},
SyntaxKind.SimpleMemberAccessExpression);

context.RegisterSyntaxNodeAction(c => AnalyzeLessThanOrEqualExpression(c), SyntaxKind.LessThanOrEqualExpression);
context.RegisterSyntaxNodeAction(c => AnalyzeGreaterThanOrEqualExpression(c), SyntaxKind.GreaterThanOrEqualExpression);
context.RegisterSyntaxNodeAction(c => AnalyzeLogicalOrExpression(c), SyntaxKind.LogicalOrExpression);
}

// x == double.NaN >>> double.IsNaN(x)
Expand Down Expand Up @@ -102,12 +112,12 @@ private static void AnalyzeGreaterThanOrEqualExpression(SyntaxNodeAnalysisContex
if (!info.Success)
return;

if (!context.IsAnalyzerSuppressed(DiagnosticDescriptors.ExpressionIsAlwaysEqualToTrueOrFalse)
if (DiagnosticDescriptors.ExpressionIsAlwaysEqualToTrueOrFalse.IsEffective(context)
&& IsAlwaysEqualToTrueOrFalse(greaterThanOrEqualExpression, info.Left, info.Right, context.SemanticModel, context.CancellationToken))
{
ReportExpressionAlwaysEqualToTrueOrFalse(context, "true");
}
else if (!context.IsAnalyzerSuppressed(DiagnosticDescriptors.UnnecessaryOperator)
else if (DiagnosticDescriptors.UnnecessaryOperator.IsEffective(context)
&& IsUnnecessaryRelationalOperator(info.Right, info.Left, context.SemanticModel, context.CancellationToken))
{
ReportUnnecessaryRelationalOperator(context, info.OperatorToken);
Expand Down Expand Up @@ -141,12 +151,12 @@ private static void AnalyzeLessThanOrEqualExpression(SyntaxNodeAnalysisContext c
if (!info.Success)
return;

if (!context.IsAnalyzerSuppressed(DiagnosticDescriptors.ExpressionIsAlwaysEqualToTrueOrFalse)
if (DiagnosticDescriptors.ExpressionIsAlwaysEqualToTrueOrFalse.IsEffective(context)
&& IsAlwaysEqualToTrueOrFalse(lessThanOrEqualExpression, info.Right, info.Left, context.SemanticModel, context.CancellationToken))
{
ReportExpressionAlwaysEqualToTrueOrFalse(context, "true");
}
else if (!context.IsAnalyzerSuppressed(DiagnosticDescriptors.UnnecessaryOperator)
else if (DiagnosticDescriptors.UnnecessaryOperator.IsEffective(context)
&& IsUnnecessaryRelationalOperator(info.Left, info.Right, context.SemanticModel, context.CancellationToken))
{
ReportUnnecessaryRelationalOperator(context, info.OperatorToken);
Expand Down
Loading

0 comments on commit da88ce6

Please sign in to comment.