From 69e862106264e862701a27602d3523c14334a705 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 6 Nov 2023 15:07:25 +0100 Subject: [PATCH 1/2] C#: Fix compiler warning of possible null de-reference. --- .../extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs | 2 +- .../Entities/NonGeneratedSourceLocation.cs | 2 +- .../Semmle.Extraction/Entities/Base/CachedEntity`1.cs | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs index 08e8f391a342..a70b84dde1df 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs @@ -91,7 +91,7 @@ private void ExtractArguments(TextWriter trapFile) // The current argument is not named // so the previous ones were also not named // so the child index matches the parameter index. - isParamsParameter = Symbol?.AttributeConstructor?.Parameters[childIndex].IsParams == true; + isParamsParameter = Symbol.AttributeConstructor?.Parameters[childIndex].IsParams == true; argSyntax = ctorArguments[childIndex]; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/NonGeneratedSourceLocation.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/NonGeneratedSourceLocation.cs index f479f607e9cb..e3e3c0c6ae4a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/NonGeneratedSourceLocation.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/NonGeneratedSourceLocation.cs @@ -22,7 +22,7 @@ public override void Populate(TextWriter trapFile) Position.Span.Start.Line + 1, Position.Span.Start.Character + 1, Position.Span.End.Line + 1, Position.Span.End.Character); - var mapped = Symbol!.GetMappedLineSpan(); + var mapped = Symbol.GetMappedLineSpan(); if (mapped.HasMappedPath && mapped.IsValid) { var mappedLoc = Create(Context, Location.Create(mapped.Path, default, mapped.Span)); diff --git a/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs b/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs index 4ef36362733c..4b82d4cb81c8 100644 --- a/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs +++ b/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.IO; using Microsoft.CodeAnalysis; @@ -30,6 +31,7 @@ protected CachedEntity(Context context) : base(context) /// The type of the symbol. public abstract class CachedEntity : CachedEntity where TSymbol : notnull { + [NotNull] public TSymbol Symbol { get; } protected CachedEntity(Context context, TSymbol symbol) : base(context) @@ -52,7 +54,7 @@ public string DebugContents public override bool NeedsPopulation { get; } - public override int GetHashCode() => Symbol is null ? 0 : Symbol.GetHashCode(); + public override int GetHashCode() => Symbol.GetHashCode(); public override bool Equals(object? obj) { From 0cf00ebb5d3be250b842e9233064ff243455cb13 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Wed, 8 Nov 2023 10:59:06 +0100 Subject: [PATCH 2/2] C#: Re-introduce null check in cached entity hash code calculation. --- .../extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs b/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs index 4b82d4cb81c8..82ded71a9cd5 100644 --- a/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs +++ b/csharp/extractor/Semmle.Extraction/Entities/Base/CachedEntity`1.cs @@ -54,7 +54,7 @@ public string DebugContents public override bool NeedsPopulation { get; } - public override int GetHashCode() => Symbol.GetHashCode(); + public override int GetHashCode() => Symbol is null ? 0 : Symbol.GetHashCode(); public override bool Equals(object? obj) {