Skip to content

Commit a66f6dc

Browse files
committed
Fix null warnings clean up code
1 parent 3bc2fa6 commit a66f6dc

9 files changed

+1582
-1628
lines changed

CheckedExceptions/AnalyzerSettings.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ namespace Sundstrom.CheckedExceptions;
22

33
using System.Text.Json.Serialization;
44

5-
public partial class AnalyzerSettings
5+
public record AnalyzerSettings
66
{
7-
[JsonPropertyName("ignoredExceptions")]
8-
public IEnumerable<string> IgnoredExceptions { get; set; } = new List<string>();
7+
public IReadOnlyList<string> IgnoredExceptions { get; }
98

10-
[JsonPropertyName("informationalExceptions")]
11-
public IDictionary<string, ExceptionMode> InformationalExceptions { get; set; } = new Dictionary<string, ExceptionMode>();
9+
public IReadOnlyDictionary<string, ExceptionMode> InformationalExceptions { get; }
10+
11+
public AnalyzerSettings(IReadOnlyList<string> ignoredExceptions, IReadOnlyDictionary<string, ExceptionMode> informationalExceptions)
12+
{
13+
IgnoredExceptions = ignoredExceptions;
14+
InformationalExceptions = informationalExceptions;
15+
}
16+
17+
public static AnalyzerSettings CreateWithDefaults() => new(new List<string>(), new Dictionary<string, ExceptionMode>());
1218
}
1319

1420
[JsonConverter(typeof(JsonStringEnumConverter))]

CheckedExceptions/AttributeHelper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
namespace Sundstrom.CheckedExceptions;
2-
1+
namespace Sundstrom.CheckedExceptions;
2+
3+
using System.Diagnostics;
4+
35
using Microsoft.CodeAnalysis;
46
using Microsoft.CodeAnalysis.CSharp.Syntax;
57

@@ -11,7 +13,8 @@ public static class AttributeHelper
1113
return null;
1214

1315
// Get the symbol to which the attribute is applied
14-
var declaredSymbol = semanticModel.GetDeclaredSymbol(attributeSyntax.Parent?.Parent);
16+
Debug.Assert(attributeSyntax.Parent?.Parent is not null);
17+
var declaredSymbol = semanticModel.GetDeclaredSymbol(attributeSyntax.Parent?.Parent!);
1518

1619
if (declaredSymbol is null)
1720
return null;

CheckedExceptions/CheckedExceptions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>

CheckedExceptions/CheckedExceptionsAnalyzer.DuplicateDetection.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Immutable;
2+
using System.Diagnostics;
23

34
using Microsoft.CodeAnalysis;
45
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -46,23 +47,23 @@ private void CheckForDuplicateThrowsAttributes(IEnumerable<AttributeSyntax> thro
4647
{
4748
var semanticModel = context.SemanticModel;
4849

49-
HashSet<INamedTypeSymbol> exceptionTypesList = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
50+
HashSet<INamedTypeSymbol>? exceptionTypesSet = null;
5051

51-
foreach (var throwsAttribute in throwsAttributes)
52+
foreach (AttributeSyntax throwsAttribute in throwsAttributes)
5253
{
53-
var exceptionTypes = GetExceptionTypes(throwsAttribute, semanticModel);
54+
Debug.Assert(throwsAttribute is not null);
55+
56+
IEnumerable<INamedTypeSymbol> exceptionTypes = GetExceptionTypes(throwsAttribute!, semanticModel);
57+
58+
exceptionTypesSet ??= new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
59+
5460
foreach (var exceptionType in exceptionTypes)
5561
{
56-
if (exceptionTypesList.FirstOrDefault(x => x.Equals(exceptionType, SymbolEqualityComparer.Default)) is not null)
62+
if (!exceptionTypesSet.Add(exceptionType))
5763
{
58-
var duplicateAttributeSyntax = throwsAttribute;
59-
if (duplicateAttributeSyntax is not null)
60-
{
61-
context.ReportDiagnostic(Diagnostic.Create(RuleDuplicateDeclarations, duplicateAttributeSyntax.GetLocation(), exceptionType.Name));
62-
}
64+
AttributeSyntax duplicateAttributeSyntax = throwsAttribute!;
65+
context.ReportDiagnostic(Diagnostic.Create(RuleDuplicateDeclarations, duplicateAttributeSyntax.GetLocation(), exceptionType.Name));
6366
}
64-
65-
exceptionTypesList.Add(exceptionType);
6667
}
6768
}
6869
}

CheckedExceptions/CheckedExceptionsAnalyzer.GeneralThrows.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,18 @@ private static void CheckForGeneralExceptionThrows(ImmutableArray<AttributeData>
3131

3232
#region Lambda expression and Local functions
3333

34-
private void CheckForGeneralExceptionThrows(SyntaxNodeAnalysisContext context, List<AttributeSyntax> throwsAttributes)
34+
private void CheckForGeneralExceptionThrows(IEnumerable<AttributeSyntax> throwsAttributes, SyntaxNodeAnalysisContext context)
3535
{
36-
string generalExceptionName = "Exception";
37-
3836
// Check for general Throws(typeof(Exception)) attributes
3937
foreach (var attribute in throwsAttributes)
4038
{
4139
var exceptionTypeName = GetExceptionTypeName(attribute, context.SemanticModel);
42-
if (exceptionTypeName == generalExceptionName)
40+
if (nameof(Exception).Equals(exceptionTypeName, StringComparison.Ordinal))
4341
{
4442
context.ReportDiagnostic(Diagnostic.Create(
4543
RuleGeneralThrows,
4644
attribute.GetLocation(),
47-
generalExceptionName));
45+
nameof(Exception)));
4846
}
4947
}
5048
}

CheckedExceptions/CheckedExceptionsAnalyzer.Inheritance.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
using System.Collections.Immutable;
2+
using System.Diagnostics;
23

34
using Microsoft.CodeAnalysis;
4-
using Microsoft.CodeAnalysis.CSharp.Syntax;
55
using Microsoft.CodeAnalysis.Diagnostics;
66

77
namespace Sundstrom.CheckedExceptions;
88

99
partial class CheckedExceptionsAnalyzer
1010
{
11-
#region Method
12-
1311
private void CheckForCompatibilityWithBaseOrInterface(SymbolAnalysisContext context, ImmutableArray<AttributeData> throwsAttributes)
1412
{
1513
var method = (IMethodSymbol)context.Symbol;
@@ -22,7 +20,8 @@ MethodKind.EventAdd or
2220
MethodKind.EventRemove))
2321
return;
2422

25-
var declaredExceptions = GetDistictExceptionTypes(throwsAttributes).ToImmutableHashSet(SymbolEqualityComparer.Default);
23+
ImmutableHashSet<ISymbol> declaredExceptions = GetDistictExceptionTypes(throwsAttributes).Where(x => x is not null).ToImmutableHashSet(SymbolEqualityComparer.Default)!;
24+
Debug.Assert(!declaredExceptions.Any(x => x is null));
2625

2726
if (declaredExceptions.Count == 0)
2827
return;
@@ -41,7 +40,7 @@ MethodKind.EventAdd or
4140
}
4241
}
4342

44-
private void AnalyzeMissingThrowsFromBaseMember(SymbolAnalysisContext context, IMethodSymbol method, ImmutableHashSet<ISymbol?> declaredExceptions, IMethodSymbol baseMethod, ImmutableHashSet<ISymbol?> baseExceptions)
43+
private void AnalyzeMissingThrowsFromBaseMember(SymbolAnalysisContext context, IMethodSymbol method, ImmutableHashSet<ISymbol> declaredExceptions, IMethodSymbol baseMethod, ImmutableHashSet<ISymbol?> baseExceptions)
4544
{
4645
foreach (var baseException in baseExceptions.OfType<ITypeSymbol>())
4746
{
@@ -51,7 +50,7 @@ private void AnalyzeMissingThrowsFromBaseMember(SymbolAnalysisContext context, I
5150

5251
var isCovered = declaredExceptions.Any(declared =>
5352
{
54-
if (declared.Equals(baseException, SymbolEqualityComparer.Default))
53+
if (baseException.Equals(declared, SymbolEqualityComparer.Default))
5554
return true;
5655

5756
var declaredNamed = declared as INamedTypeSymbol;
@@ -76,13 +75,13 @@ private void AnalyzeMissingThrowsFromBaseMember(SymbolAnalysisContext context, I
7675
}
7776
}
7877

79-
private static void AnalyzeMissingThrowsOnBaseMember(SymbolAnalysisContext context, IMethodSymbol method, ImmutableHashSet<ISymbol?> declaredExceptions, IMethodSymbol baseMethod, ImmutableHashSet<ISymbol?> baseExceptions)
78+
private static void AnalyzeMissingThrowsOnBaseMember(SymbolAnalysisContext context, IMethodSymbol method, ImmutableHashSet<ISymbol> declaredExceptions, IMethodSymbol baseMethod, ImmutableHashSet<ISymbol?> baseExceptions)
8079
{
8180
foreach (var declared in declaredExceptions)
8281
{
83-
var isCompatible = baseExceptions.Any(baseEx =>
84-
declared.Equals(baseEx, SymbolEqualityComparer.Default) ||
85-
((INamedTypeSymbol)declared).InheritsFrom((INamedTypeSymbol)baseEx));
82+
var isCompatible = baseExceptions.Any(baseEx => baseEx is not null &&
83+
(declared.Equals(baseEx, SymbolEqualityComparer.Default)
84+
|| ((INamedTypeSymbol)declared).InheritsFrom((INamedTypeSymbol)baseEx)));
8685

8786
if (!isCompatible)
8887
{
@@ -102,13 +101,11 @@ private static void AnalyzeMissingThrowsOnBaseMember(SymbolAnalysisContext conte
102101

103102
private bool IsTooGenericException(ITypeSymbol ex)
104103
{
105-
var namedType = ex as INamedTypeSymbol;
106-
if (namedType == null)
107-
return false;
108-
109-
var fullName = namedType.ToDisplayString();
104+
if (ex is not INamedTypeSymbol namedTypeSymbol) return false;
105+
106+
var fullName = namedTypeSymbol.ToDisplayString();
110107

111-
return fullName == "System.Exception" || fullName == "System.SystemException";
108+
return fullName.Equals(typeof(Exception).FullName, StringComparison.Ordinal) || fullName.Equals(typeof(SystemException).FullName, StringComparison.Ordinal);
112109
}
113110

114111
private IEnumerable<IMethodSymbol> GetBaseOrInterfaceMethods(IMethodSymbol method)
@@ -120,18 +117,18 @@ private IEnumerable<IMethodSymbol> GetBaseOrInterfaceMethods(IMethodSymbol metho
120117

121118
if (method.AssociatedSymbol is IPropertySymbol prop && prop.OverriddenProperty is not null)
122119
{
123-
if (SymbolEqualityComparer.Default.Equals(method, prop.GetMethod))
124-
results.Add(prop.OverriddenProperty.GetMethod);
125-
else if (SymbolEqualityComparer.Default.Equals(method, prop.SetMethod))
126-
results.Add(prop.OverriddenProperty.SetMethod);
120+
if (SymbolEqualityComparer.Default.Equals(method, prop.GetMethod) && prop.OverriddenProperty.GetMethod is { } getMethodSymbol)
121+
results.Add(getMethodSymbol);
122+
else if (SymbolEqualityComparer.Default.Equals(method, prop.SetMethod) && prop.OverriddenProperty.SetMethod is { } setMethodSymbol)
123+
results.Add(setMethodSymbol);
127124
}
128125

129126
if (method.AssociatedSymbol is IEventSymbol ev && ev.OverriddenEvent is not null)
130127
{
131-
if (SymbolEqualityComparer.Default.Equals(method, ev.AddMethod))
132-
results.Add(ev.OverriddenEvent.AddMethod);
133-
else if (SymbolEqualityComparer.Default.Equals(method, ev.RemoveMethod))
134-
results.Add(ev.OverriddenEvent.RemoveMethod);
128+
if (SymbolEqualityComparer.Default.Equals(method, ev.AddMethod) && ev.OverriddenEvent.AddMethod is { } addMethodSymbol)
129+
results.Add(addMethodSymbol);
130+
else if (SymbolEqualityComparer.Default.Equals(method, ev.RemoveMethod) && ev.OverriddenEvent.RemoveMethod is { } removeMethodSymbol)
131+
results.Add(removeMethodSymbol);
135132
}
136133

137134
var type = method.ContainingType;
@@ -149,6 +146,4 @@ private IEnumerable<IMethodSymbol> GetBaseOrInterfaceMethods(IMethodSymbol metho
149146

150147
return results;
151148
}
152-
153-
#endregion
154149
}

0 commit comments

Comments
 (0)