diff --git a/.editorconfig b/.editorconfig index 9516bd4f2..ec779b49f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -152,6 +152,7 @@ dotnet_diagnostic.MEF006.severity = silent dotnet_diagnostic.IDE2003.severity = silent #cleared null error types +dotnet_diagnostic.CS8714.severity = error dotnet_diagnostic.CS8765.severity = error dotnet_diagnostic.CS8766.severity = error dotnet_diagnostic.CS8767.severity = error diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index 20cecc2ce..64e2d090f 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -1799,7 +1799,8 @@ namespace ICSharpCode.Decompiler.CSharp internal static void AddAnnotationsToDeclaration(IMethod method, EntityDeclaration entityDecl, ILFunction function) { int i = 0; - var parameters = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index); + //if the variable is a parameter is a parameter then index "should not" be null, would indicate an deeper under lying problem if it is so we assert not null (!) + var parameters = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index!.Value); foreach (var parameter in entityDecl.GetChildrenByRole(Roles.Parameter)) { if (parameters.TryGetValue(i, out var v)) diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 41fb3870e..5923a37a0 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -2544,7 +2544,8 @@ namespace ICSharpCode.Decompiler.CSharp IEnumerable MakeParameters(IReadOnlyList parameters, ILFunction function) { - var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index); + //if the variable is a parameter is a parameter then index "should not" be null, would indicate an deeper under lying problem if it is so we assert not null (!) + var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index!.Value); int i = 0; foreach (var parameter in parameters) { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs b/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs index b11edf399..73b76dc1f 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs @@ -340,7 +340,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// Gets the first child with the specified role. /// Returns the role's null object if the child is not found. /// - public T GetChildByRole(Role role) where T : AstNode? + public T? GetChildByRole(Role role) where T : AstNode { if (role == null) throw new ArgumentNullException(nameof(role)); @@ -348,7 +348,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax for (var cur = firstChild; cur != null; cur = cur.nextSibling) { if ((cur.flags & roleIndexMask) == roleIndex) - return (T)cur; + return (T?)cur; } return role.NullObject; } @@ -368,16 +368,16 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return new AstNodeCollection(this, role); } - protected void SetChildByRole(Role role, T newChild) where T : AstNode + protected void SetChildByRole(Role role, T? newChild) where T : AstNode { - AstNode oldChild = GetChildByRole(role); - if (oldChild.IsNull) + AstNode? oldChild = GetChildByRole(role); + if (oldChild?.IsNull != false) AddChild(newChild, role); else oldChild.ReplaceWith(newChild); } - public void AddChild(T child, Role role) where T : AstNode + public void AddChild(T? child, Role role) where T : AstNode { if (role == null) throw new ArgumentNullException(nameof(role)); diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs index 98a86fd69..069e03e7a 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs @@ -97,7 +97,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax bool isParams; bool isScopedRef; - public CSharpTokenNode ThisKeyword { + public CSharpTokenNode? ThisKeyword { get { if (hasThisModifier) { @@ -141,21 +141,21 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } - public AstType Type { + public AstType? Type { get { return GetChildByRole(Roles.Type); } set { SetChildByRole(Roles.Type, value); } } public string Name { get { - return GetChildByRole(Roles.Identifier).Name; + return GetChildByRole(Roles.Identifier)?.Name ?? ""; } set { SetChildByRole(Roles.Identifier, Identifier.Create(value)); } } - public Identifier NameToken { + public Identifier? NameToken { get { return GetChildByRole(Roles.Identifier); } @@ -164,11 +164,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } - public CSharpTokenNode AssignToken { + public CSharpTokenNode? AssignToken { get { return GetChildByRole(Roles.Assign); } } - public Expression DefaultExpression { + public Expression? DefaultExpression { get { return GetChildByRole(Roles.Expression); } set { SetChildByRole(Roles.Expression, value); } }