Skip to content

Commit b4dc141

Browse files
authored
Merge pull request #6700 from mavasani/PrFeedback6690
Address remaining feedback from #6690
2 parents 29f0d48 + 0468c5f commit b4dc141

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/OperatorOverloadsHaveNamedAlternates.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System.Collections.Generic;
44
using System.Collections.Immutable;
5+
using System.Diagnostics;
56
using System.Linq;
67
using Analyzer.Utilities;
78
using Analyzer.Utilities.Extensions;
@@ -92,6 +93,8 @@ public override void Initialize(AnalysisContext context)
9293

9394
private static void AnalyzeMethod(IMethodSymbol methodSymbol, SymbolAnalysisContext symbolContext)
9495
{
96+
Debug.Assert(methodSymbol.MethodKind is MethodKind.UserDefinedOperator or MethodKind.Conversion);
97+
9598
// FxCop compat: only analyze externally visible symbols by default.
9699
// Note all the descriptors/rules for this analyzer have the same ID and category and hence
97100
// will always have identical configured visibility.

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/UseLiteralsWhereAppropriate.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

3+
using System;
34
using System.Collections.Immutable;
45
using System.Linq;
56
using Analyzer.Utilities;
@@ -57,51 +58,53 @@ public override void Initialize(AnalysisContext context)
5758

5859
var constantIncompatibleTypes = builder.ToImmutable();
5960

60-
context.RegisterSymbolStartAction(context =>
61+
#pragma warning disable IDE0039 // Use local function
62+
Action<OperationAnalysisContext> operationAction = context =>
6163
{
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))
6378
{
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;
8183

82-
var initializerValue = fieldInitializerValue.ConstantValue.Value;
84+
if (fieldInitializerValue.Kind == OperationKind.InterpolatedString &&
85+
!IsConstantInterpolatedStringSupported(fieldInitializerValue.Syntax.SyntaxTree.Options))
86+
{
87+
return;
88+
}
8389

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)
8696
{
97+
context.ReportDiagnostic(lastField.CreateDiagnostic(EmptyStringRule, lastField.Name));
8798
return;
8899
}
89100

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);
105108
}, SymbolKind.Field);
106109
});
107110
}

src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/BasicOverrideEqualsOnOverloadingOperatorEquals.vb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ Namespace Microsoft.CodeQuality.VisualBasic.Analyzers.ApiDesignGuidelines
6161
End Sub
6262

6363
Private Shared Function HasEqualityOperator(type As INamedTypeSymbol) As Boolean
64-
For Each method In type.GetMembers().OfType(Of IMethodSymbol)
65-
If method.MethodKind = MethodKind.UserDefinedOperator AndAlso
64+
For Each member In type.GetMembers()
65+
Dim method = TryCast(member, IMethodSymbol)
66+
If method?.MethodKind = MethodKind.UserDefinedOperator AndAlso
6667
CaseInsensitiveComparison.Equals(method.Name, WellKnownMemberNames.EqualityOperatorName) Then
6768

6869
Return True

0 commit comments

Comments
 (0)