|
1 | 1 | // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. |
2 | 2 |
|
| 3 | +using System; |
3 | 4 | using System.Collections.Immutable; |
4 | 5 | using System.Linq; |
5 | 6 | using Analyzer.Utilities; |
@@ -57,51 +58,53 @@ public override void Initialize(AnalysisContext context) |
57 | 58 |
|
58 | 59 | var constantIncompatibleTypes = builder.ToImmutable(); |
59 | 60 |
|
60 | | - context.RegisterSymbolStartAction(context => |
| 61 | +#pragma warning disable IDE0039 // Use local function |
| 62 | + Action<OperationAnalysisContext> operationAction = context => |
61 | 63 | { |
62 | | - context.RegisterOperationAction(context => |
| 64 | + var fieldInitializer = context.Operation as IFieldInitializerOperation; |
| 65 | + |
| 66 | + // Diagnostics are reported on the last initialized field to retain the previous FxCop behavior |
| 67 | + // Note all the descriptors/rules for this analyzer have the same ID and category and hence |
| 68 | + // will always have identical configured visibility. |
| 69 | + var lastField = fieldInitializer?.InitializedFields.LastOrDefault(); |
| 70 | + var fieldInitializerValue = fieldInitializer?.Value; |
| 71 | + if (fieldInitializerValue == null || |
| 72 | + lastField == null || |
| 73 | + lastField.IsConst || |
| 74 | + !lastField.IsReadOnly || |
| 75 | + !fieldInitializerValue.ConstantValue.HasValue || |
| 76 | + !context.Options.MatchesConfiguredVisibility(DefaultRule, lastField, context.Compilation, defaultRequiredVisibility: SymbolVisibilityGroup.Internal | SymbolVisibilityGroup.Private) || |
| 77 | + !context.Options.MatchesConfiguredModifiers(DefaultRule, lastField, context.Compilation, defaultRequiredModifiers: SymbolModifiers.Static)) |
63 | 78 | { |
64 | | - var fieldInitializer = context.Operation as IFieldInitializerOperation; |
65 | | - |
66 | | - // Diagnostics are reported on the last initialized field to retain the previous FxCop behavior |
67 | | - // Note all the descriptors/rules for this analyzer have the same ID and category and hence |
68 | | - // will always have identical configured visibility. |
69 | | - var lastField = fieldInitializer?.InitializedFields.LastOrDefault(); |
70 | | - var fieldInitializerValue = fieldInitializer?.Value; |
71 | | - if (fieldInitializerValue == null || |
72 | | - lastField == null || |
73 | | - lastField.IsConst || |
74 | | - !lastField.IsReadOnly || |
75 | | - !fieldInitializerValue.ConstantValue.HasValue || |
76 | | - !context.Options.MatchesConfiguredVisibility(DefaultRule, lastField, context.Compilation, defaultRequiredVisibility: SymbolVisibilityGroup.Internal | SymbolVisibilityGroup.Private) || |
77 | | - !context.Options.MatchesConfiguredModifiers(DefaultRule, lastField, context.Compilation, defaultRequiredModifiers: SymbolModifiers.Static)) |
78 | | - { |
79 | | - return; |
80 | | - } |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + var initializerValue = fieldInitializerValue.ConstantValue.Value; |
81 | 83 |
|
82 | | - var initializerValue = fieldInitializerValue.ConstantValue.Value; |
| 84 | + if (fieldInitializerValue.Kind == OperationKind.InterpolatedString && |
| 85 | + !IsConstantInterpolatedStringSupported(fieldInitializerValue.Syntax.SyntaxTree.Options)) |
| 86 | + { |
| 87 | + return; |
| 88 | + } |
83 | 89 |
|
84 | | - if (fieldInitializerValue.Kind == OperationKind.InterpolatedString && |
85 | | - !IsConstantInterpolatedStringSupported(fieldInitializerValue.Syntax.SyntaxTree.Options)) |
| 90 | + // Though null is const we don't fire the diagnostic to be FxCop Compact |
| 91 | + if (initializerValue != null && |
| 92 | + !constantIncompatibleTypes.Contains(fieldInitializerValue.Type)) |
| 93 | + { |
| 94 | + if (fieldInitializerValue.Type?.SpecialType == SpecialType.System_String && |
| 95 | + ((string)initializerValue).Length == 0) |
86 | 96 | { |
| 97 | + context.ReportDiagnostic(lastField.CreateDiagnostic(EmptyStringRule, lastField.Name)); |
87 | 98 | return; |
88 | 99 | } |
89 | 100 |
|
90 | | - // Though null is const we don't fire the diagnostic to be FxCop Compact |
91 | | - if (initializerValue != null && |
92 | | - !constantIncompatibleTypes.Contains(fieldInitializerValue.Type)) |
93 | | - { |
94 | | - if (fieldInitializerValue.Type?.SpecialType == SpecialType.System_String && |
95 | | - ((string)initializerValue).Length == 0) |
96 | | - { |
97 | | - context.ReportDiagnostic(lastField.CreateDiagnostic(EmptyStringRule, lastField.Name)); |
98 | | - return; |
99 | | - } |
100 | | - |
101 | | - context.ReportDiagnostic(lastField.CreateDiagnostic(DefaultRule, lastField.Name)); |
102 | | - } |
103 | | - }, |
104 | | - OperationKind.FieldInitializer); |
| 101 | + context.ReportDiagnostic(lastField.CreateDiagnostic(DefaultRule, lastField.Name)); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + context.RegisterSymbolStartAction(context => |
| 106 | + { |
| 107 | + context.RegisterOperationAction(operationAction, OperationKind.FieldInitializer); |
105 | 108 | }, SymbolKind.Field); |
106 | 109 | }); |
107 | 110 | } |
|
0 commit comments