Merge pull request #5091 from tamasvajk/feature/cleanup-nullable

C#: Fix nullable warnings and some code quality issues
This commit is contained in:
Tamás Vajk
2021-02-05 12:07:42 +01:00
committed by GitHub
24 changed files with 32 additions and 46 deletions

View File

@@ -1,5 +1,4 @@
using System;
using Microsoft.CodeAnalysis;
using System.Linq;
using System.Collections.Generic;
using System.IO;

View File

@@ -481,7 +481,7 @@ namespace Semmle.Extraction.CIL.Entities
// TODO: Find a solution to this.
// For now, just log the error
Cx.Cx.ExtractionError("A CIL instruction jumps outside the current method", "", Extraction.Entities.GeneratedLocation.Create(Cx.Cx), "", Util.Logging.Severity.Warning);
Cx.Cx.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Cx.Cx), "", Util.Logging.Severity.Warning);
}
}
}

View File

@@ -1,4 +1,3 @@
using Microsoft.CodeAnalysis;
using System.Linq;
using System.IO;

View File

@@ -22,7 +22,7 @@ namespace Semmle.Extraction.CIL.Entities
private class FullyQualifiedNameParser
{
public string ShortName { get; internal set; }
public string? AssemblyName { get; internal set; }
public string? AssemblyName { get; private set; }
public IEnumerable<string>? TypeArguments { get; internal set; }
public string? UnboundGenericTypeName { get; internal set; }
public string ContainerName { get; internal set; }

View File

@@ -66,7 +66,7 @@ namespace Semmle.Extraction.CIL.Entities
private void Populate()
{
if (isContainerNamespace)
if (ContainingNamespace is object)
{
Cx.Populate(ContainingNamespace);
}

View File

@@ -1,7 +1,6 @@
using System.Reflection.Metadata;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
namespace Semmle.Extraction.CIL.Entities
{

View File

@@ -1,12 +1,10 @@
using System;
using Microsoft.CodeAnalysis;
using System.Reflection.Metadata;
using System.Collections.Immutable;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.Metadata;
namespace Semmle.Extraction.CIL.Entities
{

View File

@@ -1,7 +1,5 @@
using System;
using Microsoft.CodeAnalysis;
using System.Reflection.Metadata;
using System.Collections.Immutable;
using System.Linq;
using System.Collections.Generic;
using System.IO;

View File

@@ -76,7 +76,7 @@ namespace Semmle.Extraction.PDB
out provider,
out _))
{
return new MetadataPdbReader(provider);
return new MetadataPdbReader(provider!);
}
}

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Linq;
namespace Semmle.Extraction.PDB

View File

@@ -6,7 +6,6 @@ using Microsoft.DiaSymReader;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.Metadata;
using System.IO;
using System.Reflection;
namespace Semmle.Extraction.PDB
{

View File

@@ -1,9 +1,8 @@
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Reflection.PortableExecutable;
namespace Semmle.Extraction.PDB
{
internal class PdbReader
internal static class PdbReader
{
/// <summary>
/// Returns the PDB information associated with an assembly.

View File

@@ -3,7 +3,7 @@ namespace Semmle.Extraction.CSharp
/// <summary>
/// A command-line driver for the extractor.
/// </summary>
public class Driver
public static class Driver
{
public static int Main(string[] args)
{

View File

@@ -1,6 +1,4 @@
using Semmle.Extraction.CommentProcessing;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Semmle.Extraction.Entities;
using System.IO;
@@ -21,7 +19,7 @@ namespace Semmle.Extraction.CSharp.Entities
public string Text { get { return symbol.Item2; } }
public string RawText { get; private set; }
private Extraction.Entities.Location location;
private Location location;
public override void Populate(TextWriter trapFile)
{

View File

@@ -1,8 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.IO;
using System.Linq;
namespace Semmle.Extraction.CSharp.Entities
{

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

View File

@@ -53,7 +53,7 @@ namespace Semmle.Extraction.CommentProcessing
if (l2 == null)
return 1;
var diff = l1.SourceTree == l2.SourceTree ? 0 : l1.SourceTree.FilePath.CompareTo(l2.SourceTree.FilePath);
var diff = l1.SourceTree == l2.SourceTree ? 0 : l1.SourceTree!.FilePath.CompareTo(l2.SourceTree!.FilePath);
if (diff != 0)
return diff;
diff = l1.SourceSpan.Start - l2.SourceSpan.Start;
@@ -384,9 +384,12 @@ namespace Semmle.Extraction.CommentProcessing
/// <param name="line">The line to add.</param>
public void AddCommentLine(ICommentLine line)
{
Location = !lines.Any() ?
line.Location :
Location.Create(line.Location.SourceTree, new TextSpan(Location.SourceSpan.Start, line.Location.SourceSpan.End - Location.SourceSpan.Start));
Location = !lines.Any()
? line.Location
: Location.Create(
line.Location.SourceTree!,
new TextSpan(Location.SourceSpan.Start, line.Location.SourceSpan.End - Location.SourceSpan.Start));
lines.Add(line);
}
}

View File

@@ -215,7 +215,7 @@ namespace Semmle.Extraction
}
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
ExtractionError("Uncaught exception", ex.Message, Entities.Location.Create(this), ex.StackTrace);
ExtractionError($"Uncaught exception. {ex.Message}", null, Entities.Location.Create(this), ex.StackTrace);
}
}
}
@@ -246,7 +246,7 @@ namespace Semmle.Extraction
public ICommentGenerator CommentGenerator { get; } = new CommentProcessor();
private IExtractionScope scope;
private readonly IExtractionScope scope;
public bool IsAssemblyScope => scope is AssemblyScope;
@@ -423,7 +423,7 @@ namespace Semmle.Extraction
/// <param name="location">The location of the error.</param>
/// <param name="stackTrace">An optional stack trace of the error, or null.</param>
/// <param name="severity">The severity of the error.</param>
public void ExtractionError(string message, string entityText, Entities.Location location, string? stackTrace = null, Severity severity = Severity.Error)
public void ExtractionError(string message, string? entityText, Entities.Location? location, string? stackTrace = null, Severity severity = Severity.Error)
{
var msg = new Message(message, entityText, location, stackTrace, severity);
ExtractionError(msg);
@@ -447,7 +447,7 @@ namespace Semmle.Extraction
}
else
{
ExtractionError(message, "", Entities.Location.Create(this));
ExtractionError(message, null, Entities.Location.Create(this));
}
}
@@ -531,7 +531,7 @@ namespace Semmle.Extraction
}
else
{
message = new Message("Uncaught exception", ex.Message, Entities.Location.Create(context), ex.StackTrace);
message = new Message($"Uncaught exception. {ex.Message}", null, Entities.Location.Create(context), ex.StackTrace);
}
context.ExtractionError(message);

View File

@@ -19,7 +19,7 @@ namespace Semmle.Extraction.Entities
}
else
{
assembly = init.MetadataModule.ContainingAssembly;
assembly = init.MetadataModule!.ContainingAssembly;
var identity = assembly.Identity;
var idString = identity.Name + " " + identity.Version;
assemblyPath = cx.Extractor.GetAssemblyFile(idString);

View File

@@ -14,7 +14,8 @@ namespace Semmle.Extraction.Entities
protected override void Populate(TextWriter trapFile)
{
trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText, msg.Location ?? Location.Create(cx), msg.StackTrace);
trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText ?? string.Empty,
msg.Location ?? Location.Create(cx), msg.StackTrace ?? string.Empty);
}
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;

View File

@@ -15,11 +15,9 @@ namespace Semmle.Extraction.Entities
public class NonGeneratedSourceLocation : SourceLocation
{
protected NonGeneratedSourceLocation(Context cx, Microsoft.CodeAnalysis.Location? init)
protected NonGeneratedSourceLocation(Context cx, Microsoft.CodeAnalysis.Location init)
: base(cx, init)
{
if (init is null)
throw new ArgumentException("Location may not be null", nameof(init));
Position = init.GetLineSpan();
FileEntity = File.Create(Context, Position.Path);
}
@@ -32,7 +30,7 @@ namespace Semmle.Extraction.Entities
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, Microsoft.CodeAnalysis.Location.Create(mapped.Path, default, mapped.Span));

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Semmle.Util.Logging;

View File

@@ -13,22 +13,22 @@ namespace Semmle.Extraction
{
public Severity Severity { get; }
public string Text { get; }
public string StackTrace { get; }
public string EntityText { get; }
public string? StackTrace { get; }
public string? EntityText { get; }
public Entities.Location? Location { get; }
public Message(string text, string entityText, Entities.Location? location, string? stackTrace = null, Severity severity = Severity.Error)
public Message(string text, string? entityText, Entities.Location? location, string? stackTrace = null, Severity severity = Severity.Error)
{
Severity = severity;
Text = text;
StackTrace = stackTrace ?? "";
StackTrace = stackTrace;
EntityText = entityText;
Location = location;
}
public static Message Create(Context cx, string text, ISymbol symbol, string? stackTrace = null, Severity severity = Severity.Error)
{
return new Message(text, symbol.ToString() ?? "", Entities.Location.Create(cx, symbol.Locations.FirstOrDefault()), stackTrace, severity);
return new Message(text, symbol.ToString(), Entities.Location.Create(cx, symbol.Locations.FirstOrDefault()), stackTrace, severity);
}
public static Message Create(Context cx, string text, SyntaxNode node, string? stackTrace = null, Severity severity = Severity.Error)

View File

@@ -8,7 +8,7 @@ namespace Semmle.Util
/// <summary>
/// Holder for various Win32 functions.
/// </summary>
public class Win32
public static class Win32
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetFinalPathNameByHandle( // lgtm[cs/unmanaged-code]