Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f383be

Browse files
committed
Fix new (nullability) compiler warnings
1 parent 71f095d commit 4f383be

9 files changed

Lines changed: 74 additions & 22 deletions

File tree

csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory,
170170
{
171171
var pi = GetProcessStartInfo(cmd, args, workingDirectory, environment, false);
172172
using var p = Process.Start(pi);
173+
if (p is null)
174+
{
175+
return -1;
176+
}
173177
p.WaitForExit();
174178
return p.ExitCode;
175179
}

csharp/autobuilder/Semmle.Autobuild.Shared/BuildTools.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ public static IEnumerable<VcVarsBatFile> VcVarsAllBatFiles(IBuildActions actions
8484
/// </summary>
8585
/// <param name="sln">The solution file.</param>
8686
/// <returns>A compatible file, or throws an exception.</returns>
87-
public static VcVarsBatFile FindCompatibleVcVars(IBuildActions actions, ISolution sln) =>
87+
public static VcVarsBatFile? FindCompatibleVcVars(IBuildActions actions, ISolution sln) =>
8888
FindCompatibleVcVars(actions, sln.ToolsVersion.Major);
8989

9090
/// <summary>
9191
/// Finds a VcVars that provides a compatible environment for the given tools version.
9292
/// </summary>
9393
/// <param name="targetVersion">The tools version.</param>
9494
/// <returns>A compatible file, or null.</returns>
95-
public static VcVarsBatFile FindCompatibleVcVars(IBuildActions actions, int targetVersion) =>
96-
targetVersion < 10 ?
97-
VcVarsAllBatFiles(actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault() :
98-
VcVarsAllBatFiles(actions).Where(b => b.ToolsVersion >= targetVersion).OrderBy(b => b.ToolsVersion).FirstOrDefault();
95+
public static VcVarsBatFile? FindCompatibleVcVars(IBuildActions actions, int targetVersion) =>
96+
targetVersion < 10
97+
? VcVarsAllBatFiles(actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault()
98+
: VcVarsAllBatFiles(actions).Where(b => b.ToolsVersion >= targetVersion).OrderBy(b => b.ToolsVersion).FirstOrDefault();
9999
}
100100
}

csharp/autobuilder/Semmle.Autobuild.Shared/Project.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Project(Autobuilder builder, string path) : base(builder, path)
4747

4848
var root = projFile.DocumentElement;
4949

50-
if (root.Name == "Project")
50+
if (root?.Name == "Project")
5151
{
5252
if (root.HasAttribute("Sdk"))
5353
{
@@ -77,10 +77,17 @@ public Project(Autobuilder builder, string path) : base(builder, path)
7777
// `<ProjectFile Include="X"/>` and `<ProjectFiles Include="X"/>` is valid
7878
var mgr = new XmlNamespaceManager(projFile.NameTable);
7979
mgr.AddNamespace("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003");
80-
var projectFileIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFile/@Include", mgr).OfType<XmlNode>();
81-
var projectFilesIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFiles/@Include", mgr).OfType<XmlNode>();
80+
var projectFileIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFile/@Include", mgr)
81+
?.OfType<XmlNode>() ?? Array.Empty<XmlNode>();
82+
var projectFilesIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFiles/@Include", mgr)
83+
?.OfType<XmlNode>() ?? Array.Empty<XmlNode>();
8284
foreach (var include in projectFileIncludes.Concat(projectFilesIncludes))
8385
{
86+
if (include?.Value is null)
87+
{
88+
continue;
89+
}
90+
8491
var includePath = builder.Actions.PathCombine(include.Value.Split('\\', StringSplitOptions.RemoveEmptyEntries));
8592
ret.Add(new Project(builder, builder.Actions.PathCombine(DirectoryName, includePath)));
8693
}

csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Solution(Autobuilder builder, string path, bool allowProject) : base(buil
9090
.Select(p => p.ToolsVersion);
9191

9292
public Version ToolsVersion => ToolsVersions.Any()
93-
? ToolsVersions.Max()
93+
? ToolsVersions.Max()!
9494
: new Version();
9595
}
9696
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/CsProjFile.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
8888
var projDir = fileName.Directory;
8989
var root = projFile.DocumentElement;
9090

91+
if (root is null)
92+
{
93+
throw new NotSupportedException("Project file without root is not supported.");
94+
}
95+
9196
// Figure out if it's dotnet core
9297

9398
var netCoreProjectFile = root.GetAttribute("Sdk") == "Microsoft.NET.Sdk";
@@ -96,34 +101,52 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
96101
{
97102
var explicitCsFiles = root
98103
.SelectNodes("/Project/ItemGroup/Compile/@Include", mgr)
99-
.NodeList()
104+
?.NodeList()
100105
.Select(node => node.Value)
101-
.Select(cs => Path.DirectorySeparatorChar == '/' ? cs.Replace("\\", "/") : cs)
102-
.Select(f => Path.GetFullPath(Path.Combine(projDir.FullName, f)));
106+
.Select(cs => GetFullPath(cs, projDir))
107+
.Where(s => s is not null)
108+
?? Enumerable.Empty<string>();
103109

104110
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", SearchOption.AllDirectories);
105111

112+
#nullable disable warnings
106113
return (explicitCsFiles.Concat(additionalCsFiles).ToArray(), Array.Empty<string>());
114+
#nullable restore warnings
107115
}
108116

109117
var references = root
110118
.SelectNodes("/msbuild:Project/msbuild:ItemGroup/msbuild:Reference/@Include", mgr)
111-
.NodeList()
119+
?.NodeList()
112120
.Select(node => node.Value)
113-
.ToArray();
121+
.Where(s => s is not null)
122+
.ToArray()
123+
?? Array.Empty<string>();
114124

115125
var relativeCsIncludes = root
116126
.SelectNodes("/msbuild:Project/msbuild:ItemGroup/msbuild:Compile/@Include", mgr)
117-
.NodeList()
127+
?.NodeList()
118128
.Select(node => node.Value)
119-
.ToArray();
129+
.ToArray()
130+
?? Array.Empty<string>();
120131

121132
var csFiles = relativeCsIncludes
122-
.Select(cs => Path.DirectorySeparatorChar == '/' ? cs.Replace("\\", "/") : cs)
123-
.Select(f => Path.GetFullPath(Path.Combine(projDir.FullName, f)))
133+
.Select(cs => GetFullPath(cs, projDir))
134+
.Where(s => s is not null)
124135
.ToArray();
125136

137+
#nullable disable warnings
126138
return (csFiles, references);
139+
#nullable restore warnings
140+
}
141+
142+
private static string? GetFullPath(string? file, DirectoryInfo? projDir)
143+
{
144+
if (file is null)
145+
{
146+
return null;
147+
}
148+
149+
return Path.GetFullPath(Path.Combine(projDir?.FullName ?? string.Empty, Path.DirectorySeparatorChar == '/' ? file.Replace("\\", "/") : file));
127150
}
128151

129152
private readonly string[] references;

csharp/extractor/Semmle.Extraction.CSharp.Standalone/NugetPackages.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ private void RestoreNugetPackage(string package, IProgressMonitor pm)
113113
{
114114
using var p = Process.Start(pi);
115115

116+
if (p is null)
117+
{
118+
pm.FailedNugetCommand(pi.FileName, pi.Arguments, "Couldn't start process.");
119+
return;
120+
}
121+
116122
var output = p.StandardOutput.ReadToEnd();
117123
var error = p.StandardError.ReadToEnd();
118124

csharp/extractor/Semmle.Extraction/TrapWriter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,13 @@ public static string NestPaths(ILogger logger, string? outerpath, string innerpa
247247
}
248248
try
249249
{
250-
Directory.CreateDirectory(Path.GetDirectoryName(nested));
250+
var directoryName = Path.GetDirectoryName(nested);
251+
if (directoryName is null)
252+
{
253+
logger.Log(Severity.Warning, "Failed to get directory name from path '" + nested + "'.");
254+
throw new InvalidOperationException();
255+
}
256+
Directory.CreateDirectory(directoryName);
251257
}
252258
catch (PathTooLongException)
253259
{

csharp/extractor/Semmle.Util.Tests/LongPaths.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void Delete()
7272
public void Move()
7373
{
7474
File.WriteAllText(shortPath, "abc");
75-
Directory.CreateDirectory(Path.GetDirectoryName(longPath));
75+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
7676
File.Delete(longPath);
7777
File.Move(shortPath, longPath);
7878
File.Move(longPath, shortPath);
@@ -84,7 +84,7 @@ public void Replace()
8484
{
8585
File.WriteAllText(shortPath, "abc");
8686
File.Delete(longPath);
87-
Directory.CreateDirectory(Path.GetDirectoryName(longPath));
87+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
8888
File.Move(shortPath, longPath);
8989
File.WriteAllText(shortPath, "def");
9090
FileUtils.MoveOrReplace(shortPath, longPath);
@@ -117,7 +117,7 @@ public void CreateLongStream()
117117
{
118118
var buffer2 = new byte[10];
119119

120-
Directory.CreateDirectory(Path.GetDirectoryName(longPath));
120+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
121121

122122
using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
123123
{

csharp/extractor/Semmle.Util/ProcessStartInfoExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public static int ReadOutput(this ProcessStartInfo pi, out IList<string> stdout)
1313
{
1414
stdout = new List<string>();
1515
using var process = Process.Start(pi);
16+
17+
if (process is null)
18+
{
19+
return -1;
20+
}
21+
1622
string? s;
1723
do
1824
{

0 commit comments

Comments
 (0)