Skip to content

Commit 31c1caf

Browse files
committed
Code quality improvements
1 parent 0cc94b3 commit 31c1caf

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyCacheExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal static class AssemblyCacheExtensions
1414
public static IOrderedEnumerable<AssemblyInfo> OrderAssemblyInfosByPreference(this IEnumerable<AssemblyInfo> assemblies, IEnumerable<string> frameworkPaths)
1515
{
1616
// prefer framework assemblies over others
17-
bool initialOrdering(AssemblyInfo info) => frameworkPaths.Any(framework => info.Filename.StartsWith(framework, StringComparison.OrdinalIgnoreCase));
17+
int initialOrdering(AssemblyInfo info) => frameworkPaths.Any(framework => info.Filename.StartsWith(framework, StringComparison.OrdinalIgnoreCase)) ? 1 : 0;
1818

1919
var ordered = assemblies is IOrderedEnumerable<AssemblyInfo> o
2020
? o.ThenBy(initialOrdering)

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,15 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
128128
DownloadMissingPackages(allNonBinaryFiles, dllPaths);
129129
}
130130

131-
var frameworkLocations = new List<string>();
131+
var frameworkLocations = new HashSet<string>();
132132

133133
// Find DLLs in the .Net / Asp.Net Framework
134134
// This block needs to come after the nuget restore, because the nuget restore might fetch the .NET Core/Framework reference assemblies.
135135
if (options.ScanNetFrameworkDlls)
136136
{
137-
var path = AddNetFrameworkDlls(dllPaths);
138-
frameworkLocations.Add(path);
139-
path = AddAspNetCoreFrameworkDlls(dllPaths);
140-
if (path != null)
141-
{
142-
frameworkLocations.Add(path);
143-
}
144-
path = AddMicrosoftWindowsDesktopDlls(dllPaths);
145-
if (path != null)
146-
{
147-
frameworkLocations.Add(path);
148-
}
137+
AddNetFrameworkDlls(dllPaths, frameworkLocations);
138+
AddAspNetCoreFrameworkDlls(dllPaths, frameworkLocations);
139+
AddMicrosoftWindowsDesktopDlls(dllPaths, frameworkLocations);
149140
}
150141

151142
assemblyCache = new AssemblyCache(dllPaths, frameworkLocations, progressMonitor);
@@ -239,7 +230,7 @@ private void RemoveNugetAnalyzerReferences()
239230
}
240231
}
241232

242-
private string AddNetFrameworkDlls(ISet<string> dllPaths)
233+
private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLocations)
243234
{
244235
// Multiple dotnet framework packages could be present.
245236
// The order of the packages is important, we're adding the first one that is present in the nuget cache.
@@ -252,14 +243,15 @@ private string AddNetFrameworkDlls(ISet<string> dllPaths)
252243
if (frameworkPath.Path is not null)
253244
{
254245
dllPaths.Add(frameworkPath.Path);
246+
frameworkLocations.Add(frameworkPath.Path);
255247
progressMonitor.LogInfo($"Found .NET Core/Framework DLLs in NuGet packages at {frameworkPath.Path}. Not adding installation directory.");
256248

257249
for (var i = frameworkPath.Index + 1; i < packagesInPrioOrder.Length; i++)
258250
{
259251
RemoveNugetPackageReference(packagesInPrioOrder[i], dllPaths);
260252
}
261253

262-
return frameworkPath.Path;
254+
return;
263255
}
264256

265257
string? runtimeLocation = null;
@@ -281,7 +273,7 @@ private string AddNetFrameworkDlls(ISet<string> dllPaths)
281273

282274
progressMonitor.LogInfo($".NET runtime location selected: {runtimeLocation}");
283275
dllPaths.Add(runtimeLocation);
284-
return runtimeLocation;
276+
frameworkLocations.Add(runtimeLocation);
285277
}
286278

287279
private void RemoveNugetPackageReference(string packagePrefix, ISet<string> dllPaths)
@@ -306,41 +298,38 @@ private void RemoveNugetPackageReference(string packagePrefix, ISet<string> dllP
306298
}
307299
}
308300

309-
private string? AddAspNetCoreFrameworkDlls(ISet<string> dllPaths)
301+
private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLocations)
310302
{
311303
if (!fileContent.IsNewProjectStructureUsed || !fileContent.UseAspNetCoreDlls)
312304
{
313-
return null;
305+
return;
314306
}
315307

316308
// First try to find ASP.NET Core assemblies in the NuGet packages
317309
if (GetPackageDirectory(FrameworkPackageNames.AspNetCoreFramework) is string aspNetCorePackage)
318310
{
319311
progressMonitor.LogInfo($"Found ASP.NET Core in NuGet packages. Not adding installation directory.");
320312
dllPaths.Add(aspNetCorePackage);
321-
return aspNetCorePackage;
313+
frameworkLocations.Add(aspNetCorePackage);
314+
return;
322315
}
323316

324317
if (Runtime.AspNetCoreRuntime is string aspNetCoreRuntime)
325318
{
326319
progressMonitor.LogInfo($"ASP.NET runtime location selected: {aspNetCoreRuntime}");
327320
dllPaths.Add(aspNetCoreRuntime);
328-
return aspNetCoreRuntime;
321+
frameworkLocations.Add(aspNetCoreRuntime);
329322
}
330-
331-
return null;
332323
}
333324

334-
private string? AddMicrosoftWindowsDesktopDlls(ISet<string> dllPaths)
325+
private void AddMicrosoftWindowsDesktopDlls(ISet<string> dllPaths, ISet<string> frameworkLocations)
335326
{
336327
if (GetPackageDirectory(FrameworkPackageNames.WindowsDesktopFramework) is string windowsDesktopApp)
337328
{
338329
progressMonitor.LogInfo($"Found Windows Desktop App in NuGet packages.");
339330
dllPaths.Add(windowsDesktopApp);
340-
return windowsDesktopApp;
331+
frameworkLocations.Add(windowsDesktopApp);
341332
}
342-
343-
return null;
344333
}
345334

346335
private string? GetPackageDirectory(string packagePrefix)

0 commit comments

Comments
 (0)