Skip to content

Commit 689aa1e

Browse files
Tackle symbol comparison warnings
1 parent 9dda935 commit 689aa1e

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

CodeConverter/CSharp/CommonConversions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public CommonConversions(Document document, SemanticModel semanticModel,
7575
foreach (var name in declarator.Names) {
7676

7777
var declaredSymbol = SemanticModel.GetDeclaredSymbol(name);
78-
if (symbolsToSkip?.Contains(declaredSymbol) == true) continue;
78+
if (symbolsToSkip?.Contains(declaredSymbol, SymbolEqualityComparer.IncludeNullability) == true) continue;
7979
var declaredSymbolType = declaredSymbol.GetSymbolType();
8080
var equalsValueClauseSyntax = await ConvertEqualsValueClauseSyntaxAsync(declarator, name, vbInitValue, declaredSymbolType, declaredSymbol, initializerOrMethodDecl);
8181
var v = SyntaxFactory.VariableDeclarator(ConvertIdentifier(name.Identifier), null, equalsValueClauseSyntax);
@@ -107,7 +107,7 @@ public bool ShouldPreferExplicitType(VBSyntax.ExpressionSyntax exp,
107107
exp = op.Syntax as VBSyntax.ExpressionSyntax;
108108
var vbInitConstantValue = SemanticModel.GetConstantValue(exp);
109109
isNothingLiteral = vbInitConstantValue.HasValue && vbInitConstantValue.Value == null || exp is VBSyntax.LiteralExpressionSyntax les && les.IsKind(SyntaxKind.NothingLiteralExpression);
110-
bool shouldPreferExplicitType = expConvertedType != null && (expConvertedType.HasCsKeyword() || !expConvertedType.Equals(op.Type));
110+
bool shouldPreferExplicitType = expConvertedType != null && (expConvertedType.HasCsKeyword() || !expConvertedType.Equals(op.Type, SymbolEqualityComparer.IncludeNullability));
111111
return shouldPreferExplicitType;
112112
}
113113

@@ -172,7 +172,7 @@ private CSSyntax.VariableDeclarationSyntax CreateVariableDeclaration(VariableDec
172172
CSSyntax.EqualsValueClauseSyntax equalsValueClauseSyntax, IMethodSymbol initSymbol, CSSyntax.VariableDeclaratorSyntax v)
173173
{
174174
var requireExplicitType = requireExplicitTypeForAll ||
175-
vbInitializerType != null && !Equals(declaredSymbolType, vbInitializerType);
175+
vbInitializerType != null && !SymbolEqualityComparer.IncludeNullability.Equals(declaredSymbolType, vbInitializerType);
176176
bool useVar = equalsValueClauseSyntax != null && !preferExplicitType && !requireExplicitType;
177177
var typeSyntax = initSymbol == null || !initSymbol.IsAnonymousFunction()
178178
? GetTypeSyntax(declaredSymbolType, useVar)
@@ -267,7 +267,7 @@ public SyntaxToken ConvertIdentifier(SyntaxToken id, bool isAttribute = false, S
267267
text = idSymbol.ContainingType.Name;
268268
if (normalizedText.EndsWith("Attribute", StringComparison.OrdinalIgnoreCase))
269269
text = text.Remove(text.Length - "Attribute".Length);
270-
} else if (idSymbol.IsKind(SymbolKind.Parameter) && idSymbol.ContainingSymbol.IsAccessorWithValueInCsharp() && ((idSymbol.IsImplicitlyDeclared && idSymbol.Name.WithHalfWidthLatinCharacters().Equals("value", StringComparison.OrdinalIgnoreCase)) || idSymbol.Equals(idSymbol.ContainingSymbol.GetParameters().FirstOrDefault(x => !x.IsImplicitlyDeclared)))) {
270+
} else if (idSymbol.IsKind(SymbolKind.Parameter) && idSymbol.ContainingSymbol.IsAccessorWithValueInCsharp() && ((idSymbol.IsImplicitlyDeclared && idSymbol.Name.WithHalfWidthLatinCharacters().Equals("value", StringComparison.OrdinalIgnoreCase)) || idSymbol.Equals(idSymbol.ContainingSymbol.GetParameters().FirstOrDefault(x => !x.IsImplicitlyDeclared), SymbolEqualityComparer.IncludeNullability))) {
271271
// The case above is basically that if the symbol is a parameter, and the corresponding definition is a property set definition
272272
// AND the first explicitly declared parameter is this symbol, we need to replace it with value.
273273
text = "value";

CodeConverter/CSharp/DefiniteAssignmentAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public static bool IsDefinitelyAssignedBeforeRead(ISymbol localSymbol, DataFlowA
77
{
88
if (!methodFlow.ReadInside.Contains(localSymbol)) return true;
99
var unassignedVariables = methodFlow.GetVbUnassignedVariables();
10-
return unassignedVariables != null && !unassignedVariables.Contains(localSymbol);
10+
return unassignedVariables != null && !unassignedVariables.Contains(localSymbol, SymbolEqualityComparer.IncludeNullability);
1111
}
1212
}

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ private static Dictionary<ITypeSymbol, string> CreateConvertMethodsLookupByRetur
6666

6767
var convertMethods = convertType.GetMembers().Where(m =>
6868
m.Name.StartsWith("To", StringComparison.Ordinal) && m.GetParameters().Length == 1);
69+
6970
var methodsByType = convertMethods
7071
.GroupBy(m => new { ReturnType = m.GetReturnType(), Name = $"{ConvertType.FullName}.{m.Name}" })
71-
.ToDictionary(m => m.Key.ReturnType, m => m.Key.Name);
72+
.ToDictionary(m => m.Key.ReturnType, m => m.Key.Name, SymbolEqualityComparer.IncludeNullability);
73+
7274
return methodsByType;
7375
}
7476

CodeConverter/Common/SymbolRenamer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private static (ISymbol Original, string NewName)[] GetMethodSymbolsWithNewNames
8080
bool specialSymbolUsingName, bool caseSensitive)
8181
{
8282
var stringComparer = caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
83+
#pragma warning disable RS1024 // Compare symbols correctly - analzyer bug, I'm intentionally using my own comparer, not the default ambiguous one
8384
var methodsBySignature = methodSymbols
8485
.ToLookup(m => m.GetUnqualifiedMethodSignature(caseSensitive))
8586
.Where(g => g.Count() > 1)
@@ -91,6 +92,7 @@ private static (ISymbol Original, string NewName)[] GetMethodSymbolsWithNewNames
9192
!specialSymbolUsingName).ToArray();
9293
return symbolsWithNewNames;
9394
}).ToArray();
95+
#pragma warning restore RS1024 // Compare symbols correctly
9496

9597
foreach (var newMethodNames in methodsBySignature.Select(m => m.NewName))
9698
{

0 commit comments

Comments
 (0)