From b652648db30330527954b87bb5282a9f698d5da2 Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Mon, 23 Sep 2024 16:30:39 -0400 Subject: [PATCH] more nullability updates focus CS8619 --- ICSharpCode.BamlDecompiler/Baml/BamlContext.cs | 4 ++-- ICSharpCode.Decompiler/CSharp/CallBuilder.cs | 4 ++-- .../CSharp/Resolver/MemberLookup.cs | 2 +- .../CSharp/StatementBuilder.cs | 6 +++--- ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs | 2 +- .../CSharp/Syntax/TypeSystemAstBuilder.cs | 2 +- .../DebugInfo/IDebugInfoProvider.cs | 4 ++-- .../Disassembler/ReflectionDisassembler.cs | 18 +++++++++--------- .../IL/ControlFlow/AwaitInCatchTransform.cs | 2 +- .../IL/Transforms/SwitchOnStringTransform.cs | 10 +++++----- .../IL/Transforms/TransformExpressionTrees.cs | 4 ++-- 11 files changed, 29 insertions(+), 29 deletions(-) diff --git a/ICSharpCode.BamlDecompiler/Baml/BamlContext.cs b/ICSharpCode.BamlDecompiler/Baml/BamlContext.cs index a310a2380..797db27cc 100644 --- a/ICSharpCode.BamlDecompiler/Baml/BamlContext.cs +++ b/ICSharpCode.BamlDecompiler/Baml/BamlContext.cs @@ -37,7 +37,7 @@ namespace ICSharpCode.BamlDecompiler.Baml public IDecompilerTypeSystem TypeSystem { get; } public KnownThings KnownThings { get; } - Dictionary assemblyMap = new Dictionary(); + Dictionary assemblyMap = new Dictionary(); public Dictionary AssemblyIdMap { get; } public Dictionary AttributeIdMap { get; } @@ -88,7 +88,7 @@ namespace ICSharpCode.BamlDecompiler.Baml return ctx; } - public (string FullAssemblyName, IModule Assembly) ResolveAssembly(ushort id) + public (string? FullAssemblyName, IModule? Assembly) ResolveAssembly(ushort id) { id &= 0xfff; if (!assemblyMap.TryGetValue(id, out var assembly)) diff --git a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs index 4b86a1334..15f5c8705 100644 --- a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs @@ -711,7 +711,7 @@ namespace ICSharpCode.Decompiler.CSharp return false; tokens = new List<(TokenKind Kind, int Index, int Alignment, string? Format)>(); int i = 0; - format = (string)crr.ConstantValue; + format = (string?)crr.ConstantValue; foreach (var (kind, data) in TokenizeFormatString(format)) { int index; @@ -1827,7 +1827,7 @@ namespace ICSharpCode.Decompiler.CSharp { ExpressionBuilder expressionBuilder = this.expressionBuilder; ExpressionWithResolveResult targetExpression; - (TranslatedExpression target, bool addTypeArguments, string methodName, ResolveResult result) = DisambiguateDelegateReference(method, invokeMethod, expectedTargetDetails, thisArg); + (TranslatedExpression target, bool addTypeArguments, string? methodName, ResolveResult result) = DisambiguateDelegateReference(method, invokeMethod, expectedTargetDetails, thisArg); if (target.Expression != null) { var mre = new MemberReferenceExpression(target, methodName); diff --git a/ICSharpCode.Decompiler/CSharp/Resolver/MemberLookup.cs b/ICSharpCode.Decompiler/CSharp/Resolver/MemberLookup.cs index d08f85522..b0a8f4ee8 100644 --- a/ICSharpCode.Decompiler/CSharp/Resolver/MemberLookup.cs +++ b/ICSharpCode.Decompiler/CSharp/Resolver/MemberLookup.cs @@ -189,7 +189,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver { var nestedTypes = type.GetNestedTypes(options: GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions); // GetDefinition() might return null if some IType has a strange implementation of GetNestedTypes. - entities.AddRange(nestedTypes.Select(t => t.GetDefinition()).Where(td => td != null)); + entities.AddRange(nestedTypes.Select(t => t.GetDefinition()).OfType()); } foreach (var entityGroup in entities.GroupBy(e => e.Name)) diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index e335af219..e648d584a 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -152,7 +152,7 @@ namespace ICSharpCode.Decompiler.CSharp return new IfElseStatement(condition, trueStatement, falseStatement).WithILInstruction(inst); } - internal IEnumerable CreateTypedCaseLabel(long i, IType type, List<(string Key, int Value)>? map = null) + internal IEnumerable CreateTypedCaseLabel(long i, IType type, List<(string? Key, int Value)>? map = null) { object value; // unpack nullable type, if necessary: @@ -204,7 +204,7 @@ namespace ICSharpCode.Decompiler.CSharp return TranslateSwitch(null, inst).WithILInstruction(inst); } - SwitchStatement TranslateSwitch(BlockContainer switchContainer, SwitchInstruction inst) + SwitchStatement TranslateSwitch(BlockContainer? switchContainer, SwitchInstruction inst) { var oldBreakTarget = breakTarget; breakTarget = switchContainer; // 'break' within a switch would only leave the switch @@ -385,7 +385,7 @@ namespace ICSharpCode.Decompiler.CSharp } /// Target container that a 'break;' statement would break out of - BlockContainer breakTarget; + BlockContainer? breakTarget; /// Dictionary from BlockContainer to label name for 'goto of_container'; readonly Dictionary endContainerLabels = new Dictionary(); diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs index 60d0791a6..eae240691 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs @@ -29,7 +29,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax { public static class Roles { - public static readonly Role Root = AstNode.RootRole; + public static readonly Role Root = AstNode.RootRole; // some pre defined constants for common roles public static readonly Role Identifier = new Role("Identifier", Syntax.Identifier.Null); diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs index 3b5320f86..8eeda4157 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs @@ -1291,7 +1291,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } return new CastExpression(ConvertType(type), new PrimitiveExpression(CSharpPrimitiveCast.Cast(enumBaseTypeCode, val, false))); - (long value, IField field) PrepareConstant(IField field) + (long value, IField? field) PrepareConstant(IField field) { if (!field.IsConst) return (-1, null); diff --git a/ICSharpCode.Decompiler/DebugInfo/IDebugInfoProvider.cs b/ICSharpCode.Decompiler/DebugInfo/IDebugInfoProvider.cs index 04de5d8d7..0e233d775 100644 --- a/ICSharpCode.Decompiler/DebugInfo/IDebugInfoProvider.cs +++ b/ICSharpCode.Decompiler/DebugInfo/IDebugInfoProvider.cs @@ -20,8 +20,8 @@ namespace ICSharpCode.Decompiler.DebugInfo public struct PdbExtraTypeInfo { - public string[] TupleElementNames; - public bool[] DynamicFlags; + public string?[]? TupleElementNames; + public bool[]? DynamicFlags; } public interface IDebugInfoProvider diff --git a/ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs b/ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs index 75a3c2877..9bec3d126 100644 --- a/ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs +++ b/ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs @@ -475,7 +475,7 @@ namespace ICSharpCode.Decompiler.Disassembler } } - class SecurityDeclarationDecoder : ICustomAttributeTypeProvider<(PrimitiveTypeCode, string)> + class SecurityDeclarationDecoder : ICustomAttributeTypeProvider<(PrimitiveTypeCode, string?)> { readonly ITextOutput output; readonly IAssemblyResolver resolver; @@ -488,32 +488,32 @@ namespace ICSharpCode.Decompiler.Disassembler this.module = module; } - public (PrimitiveTypeCode, string) GetPrimitiveType(PrimitiveTypeCode typeCode) + public (PrimitiveTypeCode, string?) GetPrimitiveType(PrimitiveTypeCode typeCode) { return (typeCode, null); } - public (PrimitiveTypeCode, string) GetSystemType() + public (PrimitiveTypeCode, string?) GetSystemType() { return (0, "type"); } - public (PrimitiveTypeCode, string) GetSZArrayType((PrimitiveTypeCode, string) elementType) + public (PrimitiveTypeCode, string?) GetSZArrayType((PrimitiveTypeCode, string?) elementType) { return (elementType.Item1, (elementType.Item2 ?? PrimitiveTypeCodeToString(elementType.Item1)) + "[]"); } - public (PrimitiveTypeCode, string) GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) + public (PrimitiveTypeCode, string?) GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) { throw new NotImplementedException(); } - public (PrimitiveTypeCode, string) GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) + public (PrimitiveTypeCode, string?) GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) { throw new NotImplementedException(); } - public (PrimitiveTypeCode, string) GetTypeFromSerializedName(string name) + public (PrimitiveTypeCode, string?) GetTypeFromSerializedName(string name) { if (resolver == null) throw new EnumUnderlyingTypeResolveException(); @@ -525,12 +525,12 @@ namespace ICSharpCode.Decompiler.Disassembler return (0, name); } - public PrimitiveTypeCode GetUnderlyingEnumType((PrimitiveTypeCode, string) type) + public PrimitiveTypeCode GetUnderlyingEnumType((PrimitiveTypeCode, string?) type) { return type.Item1; } - public bool IsSystemType((PrimitiveTypeCode, string) type) + public bool IsSystemType((PrimitiveTypeCode, string?) type) { return "type" == type.Item2; } diff --git a/ICSharpCode.Decompiler/IL/ControlFlow/AwaitInCatchTransform.cs b/ICSharpCode.Decompiler/IL/ControlFlow/AwaitInCatchTransform.cs index 031f0ff73..569ccede8 100644 --- a/ICSharpCode.Decompiler/IL/ControlFlow/AwaitInCatchTransform.cs +++ b/ICSharpCode.Decompiler/IL/ControlFlow/AwaitInCatchTransform.cs @@ -420,7 +420,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow // => // throw(ldloc result.Handler.Variable) internal static bool MatchExceptionCaptureBlock(ILTransformContext context, Block block, - ref ILVariable? objectVariable, out StLoc? typedExceptionVariableStore, out Block? captureBlock, out Block? throwBlock) + [NotNullWhen(true)] ref ILVariable? objectVariable, [NotNullWhen(true)] out StLoc? typedExceptionVariableStore, [NotNullWhen(true)] out Block? captureBlock, [NotNullWhen(true)] out Block? throwBlock) { bool DerivesFromException(IType t) => t.GetAllBaseTypes().Any(ty => ty.IsKnownType(KnownTypeCode.Exception)); diff --git a/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs b/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs index 4ac25bfc0..419ea000f 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs @@ -422,8 +422,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms return false; switchValueVar = switchValueVarCopy; int conditionOffset = 1; - Block currentCaseBlock = isInternedBlock; - var values = new List<(string, ILInstruction)>(); + Block? currentCaseBlock = isInternedBlock; + var values = new List<(string?, ILInstruction)>(); if (!switchValueVarCopy.IsSingleDefinition) return false; @@ -440,9 +440,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms break; if (!left.MatchLdLoc(switchValueVar)) break; - if (!right.MatchLdStr(out string value)) + if (!right.MatchLdStr(out string? value)) break; - if (!(caseBlockJump.MatchBranch(out var caseBlock) || caseBlockJump.MatchLeave((BlockContainer)currentCaseBlock.Parent))) + if (!(caseBlockJump.MatchBranch(out var caseBlock) || caseBlockJump.MatchLeave((BlockContainer?)currentCaseBlock.Parent))) break; if (!currentCaseBlock.Instructions[conditionOffset + 1].MatchBranch(out currentCaseBlock)) break; @@ -705,7 +705,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms return true; } - bool FixCasesWithoutValue(List sections, List<(string, int)> stringValues) + bool FixCasesWithoutValue(List sections, List<(string?, int)> stringValues) { bool HasLabel(SwitchSection section) { diff --git a/ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs b/ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs index 960095875..3a7e3f1e8 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs @@ -398,7 +398,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms { if (function.Kind == ILFunctionKind.ExpressionTree) { - function.DelegateType = UnwrapExpressionTree(function.DelegateType); + function.DelegateType = UnwrapExpressionTree(function.DelegateType!); function.Kind = ILFunctionKind.Delegate; } return function; @@ -536,7 +536,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms } } - (Func, IType) ConvertBind(CallInstruction invocation) + (Func?, IType) ConvertBind(CallInstruction invocation) { if (invocation.Arguments.Count != 2) return (null, SpecialType.UnknownType);