Merge pull request #755 from calumgrant/cs/extractor-alerts

C#: Fix some LGTM alerts on the extractor
This commit is contained in:
Tom Hvitved
2019-01-14 10:47:44 +01:00
committed by GitHub
24 changed files with 46 additions and 40 deletions

View File

@@ -39,7 +39,7 @@ namespace Semmle.Autobuild
{
projFile = builder.Actions.LoadXml(FullPath);
}
catch (Exception e) when (e is XmlException || e is FileNotFoundException)
catch (Exception ex) when (ex is XmlException || ex is FileNotFoundException)
{
builder.Log(Severity.Info, $"Unable to read project file {path}.");
return;

View File

@@ -63,7 +63,7 @@ namespace Semmle.Autobuild
{
solution = SolutionFile.Parse(FullPath);
}
catch (Exception e) when (e is InvalidProjectFileException || e is FileNotFoundException)
catch (Exception ex) when (ex is InvalidProjectFileException || ex is FileNotFoundException)
{
// We allow specifying projects as solutions in lgtm.yml, so model
// that scenario as a solution with just that one project

View File

@@ -143,7 +143,7 @@ namespace Semmle.Extraction.CIL.Entities
}
}
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
logger.Log(Severity.Error, string.Format("Exception extracting {0}: {1}", assemblyPath, ex));
}

View File

@@ -402,6 +402,9 @@ namespace Semmle.Extraction.CIL.Entities
declType = parentMethod == null ? parent as Type : parentMethod.DeclaringType;
if (declType is null)
throw new InternalError("Parent context of method is not a type");
ShortId = MakeMethodId(declType, nameLabel);
var typeSourceDeclaration = declType.SourceDeclaration;

View File

@@ -1121,7 +1121,7 @@ namespace Semmle.Extraction.CIL.Entities
static readonly Id excl = Id.Create("M!");
public Id MakeId(GenericContext outerGc)
{
if (innerGc != outerGc && innerGc is Method method)
if (!ReferenceEquals(innerGc, outerGc) && innerGc is Method method)
return open + method.Label.Value + close + excl + index;
return excl + index;
}

View File

@@ -283,7 +283,7 @@ namespace Semmle.BuildAnalyser
}
++succeededProjects;
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
++failedProjects;
progressMonitor.FailedProjectFile(proj.FullName, ex.Message);

View File

@@ -183,10 +183,10 @@ namespace Semmle.BuildAnalyser
}
}
}
catch (Exception e)
when (e is System.ComponentModel.Win32Exception || e is FileNotFoundException)
catch (Exception ex)
when (ex is System.ComponentModel.Win32Exception || ex is FileNotFoundException)
{
pm.FailedNugetCommand(pi.FileName, pi.Arguments, e.Message);
pm.FailedNugetCommand(pi.FileName, pi.Arguments, ex.Message);
}
}

View File

@@ -97,7 +97,7 @@ namespace Semmle.Extraction.CSharp
extractor.SetAssemblyFile(assemblyIdentity, refPath);
}
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
extractor.Message(new Message
{
@@ -272,7 +272,7 @@ namespace Semmle.Extraction.CSharp
ReportProgress(assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, skipExtraction ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
}
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
Logger.Log(Severity.Error, " Unhandled exception analyzing {0}: {1}", r.FilePath, ex);
}
@@ -354,7 +354,7 @@ namespace Semmle.Extraction.CSharp
ReportProgress(sourcePath, trapPath, stopwatch.Elapsed, excluded ? AnalysisAction.Excluded : upToDate ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
extractor.Message(new Message { exception = ex, message = string.Format("Unhandled exception processing {0}: {1}", tree.FilePath, ex), severity = Severity.Error });
}

View File

@@ -134,6 +134,9 @@ namespace Semmle.Extraction.CSharp.Entities
if (symbol.Kind == SymbolKind.NamedType)
{
INamedTypeSymbol nt = symbol as INamedTypeSymbol;
if (nt is null)
throw new InternalError(symbol, "Symbol kind is inconsistent with its type");
if (nt.TypeKind == TypeKind.Struct)
{
// Sadly, these properties are internal so cannot be accessed directly.

View File

@@ -1,4 +1,4 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax; // lgtm[cs/similar-file]
using Semmle.Extraction.Kinds;
namespace Semmle.Extraction.CSharp.Entities.Statements

View File

@@ -1,4 +1,4 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax; // lgtm[cs/similar-file]
using Semmle.Extraction.Kinds;
namespace Semmle.Extraction.CSharp.Entities.Statements

View File

@@ -1,4 +1,4 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax; // lgtm[cs/similar-file]
using Semmle.Extraction.Kinds;
namespace Semmle.Extraction.CSharp.Entities.Statements

View File

@@ -1,4 +1,4 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax; // lgtm[cs/similar-file]
using Semmle.Extraction.Kinds;
namespace Semmle.Extraction.CSharp.Entities.Statements

View File

@@ -165,9 +165,9 @@ namespace Semmle.Extraction.CSharp
return analyser.TotalErrors == 0 ? ExitCode.Ok : ExitCode.Errors;
}
catch (Exception e)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
logger.Log(Severity.Error, " Unhandled exception: {0}", e);
logger.Log(Severity.Error, " Unhandled exception: {0}", ex);
return ExitCode.Errors;
}
}
@@ -354,9 +354,9 @@ namespace Semmle.Extraction.CSharp
pm.MissingSummary(analyser.MissingTypes.Count(), analyser.MissingNamespaces.Count());
}
catch (Exception e)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
analyser.Logger.Log(Severity.Error, " Unhandled exception: {0}", e);
analyser.Logger.Log(Severity.Error, " Unhandled exception: {0}", ex);
}
}
}

View File

@@ -1,4 +1,4 @@
namespace Semmle.Extraction.Kinds
namespace Semmle.Extraction.Kinds // lgtm[cs/similar-file]
{
/// <summary>
/// This enum has been auto-generated from the C# DB scheme - do not edit.

View File

@@ -1,4 +1,4 @@
namespace Semmle.Extraction.Kinds
namespace Semmle.Extraction.Kinds // lgtm[cs/similar-file]
{
/// <summary>
/// This enum has been auto-generated from the C# DB scheme - do not edit.

View File

@@ -54,9 +54,9 @@ namespace Semmle.Extraction.CSharp.Populators
{
node.Accept(new Ast(cx, parent, child));
}
catch (System.Exception e)
catch (System.Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
cx.ModelError(node, "Exception processing syntax node of type {0}: {1}", node.Kind(), e);
cx.ModelError(node, "Exception processing syntax node of type {0}: {1}", node.Kind(), ex);
}
}
}

View File

@@ -55,9 +55,9 @@ namespace Semmle.Extraction.CSharp.Populators
{
return symbol.Accept(new Symbols(cx));
}
catch (Exception e)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
cx.ModelError(symbol, "Exception processing symbol '{2}' of type '{0}': {1}", symbol.Kind, e, symbol);
cx.ModelError(symbol, "Exception processing symbol '{2}' of type '{0}': {1}", symbol.Kind, ex, symbol);
return null;
}
}

View File

@@ -93,7 +93,7 @@ namespace Semmle.Extraction
{
if (idLabelCache.TryGetValue(id, out var originalEntity))
{
Extractor.Message(new Message { message = "Label collision for " + id.ToString(), severity = Severity.Warning });
Extractor.Message(new Message { message = "Label collision for " + id, severity = Severity.Warning });
}
else
{
@@ -216,13 +216,13 @@ namespace Semmle.Extraction
{
populateQueue.Dequeue()();
}
catch (InternalError e)
catch (InternalError ex)
{
Extractor.Message(e.ExtractionMessage);
Extractor.Message(ex.ExtractionMessage);
}
catch (Exception e)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
Extractor.Message(new Message { severity = Severity.Error, exception = e, message = "Uncaught exception" });
Extractor.Message(new Message { severity = Severity.Error, exception = ex, message = "Uncaught exception" });
}
}
}
@@ -504,7 +504,7 @@ namespace Semmle.Extraction
{
a();
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
var internalError = ex as InternalError;
var message = internalError != null

View File

@@ -57,7 +57,7 @@ namespace Semmle.Extraction
public override bool Equals(object obj)
{
var other = obj as CachedEntity<Initializer>;
return obj != null && obj.GetType() == GetType() && Equals(other.symbol, symbol);
return other?.GetType() == GetType() && Equals(other.symbol, symbol);
}
public abstract TrapStackBehaviour TrapStackBehaviour { get; }

View File

@@ -190,7 +190,7 @@ namespace Semmle.Extraction
FileUtils.TryDelete(tmpFile);
}
}
catch (Exception ex)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
Logger.Log(Severity.Error, "Failed to move the trap file from {0} to {1} because {2}", tmpFile, TrapFile, ex);
}

View File

@@ -70,7 +70,7 @@ namespace Semmle.Util
/// <returns>The canonical path.</returns>
public override string GetCanonicalPath(string path, IPathCache cache)
{
using (var hFile = Win32.CreateFile(
using (var hFile = Win32.CreateFile( // lgtm[cs/call-to-unmanaged-code]
path,
0,
Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE,
@@ -88,13 +88,13 @@ namespace Semmle.Util
else
{
StringBuilder outPath = new StringBuilder(Win32.MAX_PATH);
int length = Win32.GetFinalPathNameByHandle(hFile, outPath, outPath.Capacity, 0);
int length = Win32.GetFinalPathNameByHandle(hFile, outPath, outPath.Capacity, 0); // lgtm[cs/call-to-unmanaged-code]
if (length >= outPath.Capacity)
{
// Path length exceeded MAX_PATH.
// Possible if target has a long path.
outPath = new StringBuilder(length + 1);
length = Win32.GetFinalPathNameByHandle(hFile, outPath, outPath.Capacity, 0);
length = Win32.GetFinalPathNameByHandle(hFile, outPath, outPath.Capacity, 0); // lgtm[cs/call-to-unmanaged-code]
}
const int PREAMBLE = 4; // outPath always starts \\?\

View File

@@ -74,9 +74,9 @@ namespace Semmle.Util.Logging
FileShare.ReadWrite, 8192));
writer.AutoFlush = true;
}
catch (Exception e)
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
Console.Error.WriteLine("SEMMLE: Couldn't initialise C# extractor output: " + e.Message + "\n" + e.StackTrace);
Console.Error.WriteLine("SEMMLE: Couldn't initialise C# extractor output: " + ex.Message + "\n" + ex.StackTrace);
Console.Error.Flush();
throw;
}

View File

@@ -11,14 +11,14 @@ namespace Semmle.Util
public class Win32
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetFinalPathNameByHandle(
public static extern int GetFinalPathNameByHandle( // lgtm[cs/unmanaged-code]
SafeHandle handle,
[In, Out] StringBuilder path,
int bufLen,
int flags);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern SafeFileHandle CreateFile(
public static extern SafeFileHandle CreateFile( // lgtm[cs/unmanaged-code]
string filename,
uint desiredAccess,
uint shareMode,