Fix new (nullability) compiler warnings

This commit is contained in:
Tamas Vajk
2021-02-10 10:40:57 +01:00
parent 71f095d6d4
commit 4f383be13b
9 changed files with 74 additions and 22 deletions

View File

@@ -170,6 +170,10 @@ namespace Semmle.Autobuild.Shared
{
var pi = GetProcessStartInfo(cmd, args, workingDirectory, environment, false);
using var p = Process.Start(pi);
if (p is null)
{
return -1;
}
p.WaitForExit();
return p.ExitCode;
}

View File

@@ -84,7 +84,7 @@ namespace Semmle.Autobuild.Shared
/// </summary>
/// <param name="sln">The solution file.</param>
/// <returns>A compatible file, or throws an exception.</returns>
public static VcVarsBatFile FindCompatibleVcVars(IBuildActions actions, ISolution sln) =>
public static VcVarsBatFile? FindCompatibleVcVars(IBuildActions actions, ISolution sln) =>
FindCompatibleVcVars(actions, sln.ToolsVersion.Major);
/// <summary>
@@ -92,9 +92,9 @@ namespace Semmle.Autobuild.Shared
/// </summary>
/// <param name="targetVersion">The tools version.</param>
/// <returns>A compatible file, or null.</returns>
public static VcVarsBatFile FindCompatibleVcVars(IBuildActions actions, int targetVersion) =>
targetVersion < 10 ?
VcVarsAllBatFiles(actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault() :
VcVarsAllBatFiles(actions).Where(b => b.ToolsVersion >= targetVersion).OrderBy(b => b.ToolsVersion).FirstOrDefault();
public static VcVarsBatFile? FindCompatibleVcVars(IBuildActions actions, int targetVersion) =>
targetVersion < 10
? VcVarsAllBatFiles(actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault()
: VcVarsAllBatFiles(actions).Where(b => b.ToolsVersion >= targetVersion).OrderBy(b => b.ToolsVersion).FirstOrDefault();
}
}

View File

@@ -47,7 +47,7 @@ namespace Semmle.Autobuild.Shared
var root = projFile.DocumentElement;
if (root.Name == "Project")
if (root?.Name == "Project")
{
if (root.HasAttribute("Sdk"))
{
@@ -77,10 +77,17 @@ namespace Semmle.Autobuild.Shared
// `<ProjectFile Include="X"/>` and `<ProjectFiles Include="X"/>` is valid
var mgr = new XmlNamespaceManager(projFile.NameTable);
mgr.AddNamespace("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003");
var projectFileIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFile/@Include", mgr).OfType<XmlNode>();
var projectFilesIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFiles/@Include", mgr).OfType<XmlNode>();
var projectFileIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFile/@Include", mgr)
?.OfType<XmlNode>() ?? Array.Empty<XmlNode>();
var projectFilesIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFiles/@Include", mgr)
?.OfType<XmlNode>() ?? Array.Empty<XmlNode>();
foreach (var include in projectFileIncludes.Concat(projectFilesIncludes))
{
if (include?.Value is null)
{
continue;
}
var includePath = builder.Actions.PathCombine(include.Value.Split('\\', StringSplitOptions.RemoveEmptyEntries));
ret.Add(new Project(builder, builder.Actions.PathCombine(DirectoryName, includePath)));
}

View File

@@ -90,7 +90,7 @@ namespace Semmle.Autobuild.Shared
.Select(p => p.ToolsVersion);
public Version ToolsVersion => ToolsVersions.Any()
? ToolsVersions.Max()
? ToolsVersions.Max()!
: new Version();
}
}

View File

@@ -88,6 +88,11 @@ namespace Semmle.BuildAnalyser
var projDir = fileName.Directory;
var root = projFile.DocumentElement;
if (root is null)
{
throw new NotSupportedException("Project file without root is not supported.");
}
// Figure out if it's dotnet core
var netCoreProjectFile = root.GetAttribute("Sdk") == "Microsoft.NET.Sdk";
@@ -96,34 +101,52 @@ namespace Semmle.BuildAnalyser
{
var explicitCsFiles = root
.SelectNodes("/Project/ItemGroup/Compile/@Include", mgr)
.NodeList()
?.NodeList()
.Select(node => node.Value)
.Select(cs => Path.DirectorySeparatorChar == '/' ? cs.Replace("\\", "/") : cs)
.Select(f => Path.GetFullPath(Path.Combine(projDir.FullName, f)));
.Select(cs => GetFullPath(cs, projDir))
.Where(s => s is not null)
?? Enumerable.Empty<string>();
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", SearchOption.AllDirectories);
#nullable disable warnings
return (explicitCsFiles.Concat(additionalCsFiles).ToArray(), Array.Empty<string>());
#nullable restore warnings
}
var references = root
.SelectNodes("/msbuild:Project/msbuild:ItemGroup/msbuild:Reference/@Include", mgr)
.NodeList()
?.NodeList()
.Select(node => node.Value)
.ToArray();
.Where(s => s is not null)
.ToArray()
?? Array.Empty<string>();
var relativeCsIncludes = root
.SelectNodes("/msbuild:Project/msbuild:ItemGroup/msbuild:Compile/@Include", mgr)
.NodeList()
?.NodeList()
.Select(node => node.Value)
.ToArray();
.ToArray()
?? Array.Empty<string>();
var csFiles = relativeCsIncludes
.Select(cs => Path.DirectorySeparatorChar == '/' ? cs.Replace("\\", "/") : cs)
.Select(f => Path.GetFullPath(Path.Combine(projDir.FullName, f)))
.Select(cs => GetFullPath(cs, projDir))
.Where(s => s is not null)
.ToArray();
#nullable disable warnings
return (csFiles, references);
#nullable restore warnings
}
private static string? GetFullPath(string? file, DirectoryInfo? projDir)
{
if (file is null)
{
return null;
}
return Path.GetFullPath(Path.Combine(projDir?.FullName ?? string.Empty, Path.DirectorySeparatorChar == '/' ? file.Replace("\\", "/") : file));
}
private readonly string[] references;

View File

@@ -113,6 +113,12 @@ namespace Semmle.BuildAnalyser
{
using var p = Process.Start(pi);
if (p is null)
{
pm.FailedNugetCommand(pi.FileName, pi.Arguments, "Couldn't start process.");
return;
}
var output = p.StandardOutput.ReadToEnd();
var error = p.StandardError.ReadToEnd();

View File

@@ -247,7 +247,13 @@ namespace Semmle.Extraction
}
try
{
Directory.CreateDirectory(Path.GetDirectoryName(nested));
var directoryName = Path.GetDirectoryName(nested);
if (directoryName is null)
{
logger.Log(Severity.Warning, "Failed to get directory name from path '" + nested + "'.");
throw new InvalidOperationException();
}
Directory.CreateDirectory(directoryName);
}
catch (PathTooLongException)
{

View File

@@ -72,7 +72,7 @@ namespace SemmleTests.Semmle.Util
public void Move()
{
File.WriteAllText(shortPath, "abc");
Directory.CreateDirectory(Path.GetDirectoryName(longPath));
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
File.Delete(longPath);
File.Move(shortPath, longPath);
File.Move(longPath, shortPath);
@@ -84,7 +84,7 @@ namespace SemmleTests.Semmle.Util
{
File.WriteAllText(shortPath, "abc");
File.Delete(longPath);
Directory.CreateDirectory(Path.GetDirectoryName(longPath));
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
File.Move(shortPath, longPath);
File.WriteAllText(shortPath, "def");
FileUtils.MoveOrReplace(shortPath, longPath);
@@ -117,7 +117,7 @@ namespace SemmleTests.Semmle.Util
{
var buffer2 = new byte[10];
Directory.CreateDirectory(Path.GetDirectoryName(longPath));
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
{

View File

@@ -13,6 +13,12 @@ namespace Semmle.Util
{
stdout = new List<string>();
using var process = Process.Start(pi);
if (process is null)
{
return -1;
}
string? s;
do
{