diff --git a/csharp/autobuilder/Semmle.Autobuild/Project.cs b/csharp/autobuilder/Semmle.Autobuild/Project.cs index 47752f691a49..5e49815a49be 100644 --- a/csharp/autobuilder/Semmle.Autobuild/Project.cs +++ b/csharp/autobuilder/Semmle.Autobuild/Project.cs @@ -39,7 +39,7 @@ public Project(Autobuilder builder, string path) : base(builder, path) { 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; diff --git a/csharp/autobuilder/Semmle.Autobuild/Solution.cs b/csharp/autobuilder/Semmle.Autobuild/Solution.cs index a5512a84ea66..661f46199f87 100644 --- a/csharp/autobuilder/Semmle.Autobuild/Solution.cs +++ b/csharp/autobuilder/Semmle.Autobuild/Solution.cs @@ -63,7 +63,7 @@ public Solution(Autobuilder builder, string path, bool allowProject) : base(buil { 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 diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs index 31745a434f67..577f9ec37a76 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs @@ -143,7 +143,7 @@ public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger } } } - catch (Exception ex) + catch (Exception ex) // lgtm[cs/catch-of-all-exceptions] { logger.Log(Severity.Error, string.Format("Exception extracting {0}: {1}", assemblyPath, ex)); } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs index 314ecb7ca927..6b6df0cfb118 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs @@ -402,6 +402,9 @@ public MemberReferenceMethod(GenericContext gc, MemberReferenceHandle handle) : 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; diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs index 37e6b84436ae..0644d668fa1f 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs @@ -1121,7 +1121,7 @@ class GenericMethodParameter : ITypeSignature 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; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs index 864316ac60b1..b0ef328bcd00 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs @@ -283,7 +283,7 @@ void AnalyseProjectFiles(FileInfo[] projectFiles) } ++succeededProjects; } - catch (Exception ex) + catch (Exception ex) // lgtm[cs/catch-of-all-exceptions] { ++failedProjects; progressMonitor.FailedProjectFile(proj.FullName, ex.Message); diff --git a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/NugetPackages.cs b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/NugetPackages.cs index 067f0a84cfe1..1f0755f307f4 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/NugetPackages.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/NugetPackages.cs @@ -183,10 +183,10 @@ void RestoreNugetPackage(string package, IProgressMonitor pm) } } } - 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); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs index 331bcbf72611..96496b8132f6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs @@ -97,7 +97,7 @@ void SetReferencePaths() extractor.SetAssemblyFile(assemblyIdentity, refPath); } } - catch (Exception ex) + catch (Exception ex) // lgtm[cs/catch-of-all-exceptions] { extractor.Message(new Message { @@ -272,7 +272,7 @@ void DoAnalyseAssembly(PortableExecutableReference r) 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 @@ void DoExtractTree(SyntaxTree tree) 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 }); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs index bca30cea96d1..148bd190eb15 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs @@ -134,6 +134,9 @@ public static void ExtractModifiers(Context cx, IEntity key, ISymbol symbol) 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. diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs index 55ce9d800580..e11710c5a29d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs @@ -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 diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs index 2616b8749971..ca2ec47b36f0 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs @@ -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 diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs index 98e3c37c1dcf..f7788bddcd11 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs @@ -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 diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs index 797d4021708e..0de0f4146688 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs @@ -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 diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs index a15f85617e0c..ae5525899267 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs @@ -165,9 +165,9 @@ public static ExitCode Run(string[] args) 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 @@ public static void ExtractStandalone( 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); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Kinds/StmtKind.cs b/csharp/extractor/Semmle.Extraction.CSharp/Kinds/StmtKind.cs index 1ecf005a3606..f55ab664794f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Kinds/StmtKind.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Kinds/StmtKind.cs @@ -1,4 +1,4 @@ -namespace Semmle.Extraction.Kinds +namespace Semmle.Extraction.Kinds // lgtm[cs/similar-file] { /// /// This enum has been auto-generated from the C# DB scheme - do not edit. diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Kinds/TypeKind.cs b/csharp/extractor/Semmle.Extraction.CSharp/Kinds/TypeKind.cs index 6387193db3eb..fb205a6646e5 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Kinds/TypeKind.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Kinds/TypeKind.cs @@ -1,4 +1,4 @@ -namespace Semmle.Extraction.Kinds +namespace Semmle.Extraction.Kinds // lgtm[cs/similar-file] { /// /// This enum has been auto-generated from the C# DB scheme - do not edit. diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Ast.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Ast.cs index 0ee8117ff8c9..3188a13bd72f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Ast.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Ast.cs @@ -54,9 +54,9 @@ public static void Extract(this Context cx, CSharpSyntaxNode node, IExpressionPa { 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); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Symbols.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Symbols.cs index 0a62e4beaf05..48e28621dff0 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Symbols.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Symbols.cs @@ -55,9 +55,9 @@ public static IEntity CreateEntity(this Context cx, ISymbol symbol) { 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; } } diff --git a/csharp/extractor/Semmle.Extraction/Context.cs b/csharp/extractor/Semmle.Extraction/Context.cs index 76ed4ddea63a..68e7030521af 100644 --- a/csharp/extractor/Semmle.Extraction/Context.cs +++ b/csharp/extractor/Semmle.Extraction/Context.cs @@ -93,7 +93,7 @@ private void CheckEntityHasUniqueLabel(IId id, ICachedEntity entity) { 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 @@ public void PopulateAll() { 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 @@ static public void Try(this Context context, SyntaxNode node, ISymbol symbol, Ac { a(); } - catch (Exception ex) + catch (Exception ex) // lgtm[cs/catch-of-all-exceptions] { var internalError = ex as InternalError; var message = internalError != null diff --git a/csharp/extractor/Semmle.Extraction/Symbol.cs b/csharp/extractor/Semmle.Extraction/Symbol.cs index 0687c07a29b6..76e07e0a1211 100644 --- a/csharp/extractor/Semmle.Extraction/Symbol.cs +++ b/csharp/extractor/Semmle.Extraction/Symbol.cs @@ -57,7 +57,7 @@ protected void WithDuplicationGuard(System.Action a, IEntity location) public override bool Equals(object obj) { var other = obj as CachedEntity; - return obj != null && obj.GetType() == GetType() && Equals(other.symbol, symbol); + return other?.GetType() == GetType() && Equals(other.symbol, symbol); } public abstract TrapStackBehaviour TrapStackBehaviour { get; } diff --git a/csharp/extractor/Semmle.Extraction/TrapWriter.cs b/csharp/extractor/Semmle.Extraction/TrapWriter.cs index 25003e4e53d6..4176a26c84d9 100644 --- a/csharp/extractor/Semmle.Extraction/TrapWriter.cs +++ b/csharp/extractor/Semmle.Extraction/TrapWriter.cs @@ -190,7 +190,7 @@ public void Dispose() 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); } diff --git a/csharp/extractor/Semmle.Util/CanonicalPathCache.cs b/csharp/extractor/Semmle.Util/CanonicalPathCache.cs index 8872c53ed401..5b5a04a0c48b 100644 --- a/csharp/extractor/Semmle.Util/CanonicalPathCache.cs +++ b/csharp/extractor/Semmle.Util/CanonicalPathCache.cs @@ -70,7 +70,7 @@ class GetFinalPathNameByHandleStrategy : PathStrategy /// The canonical path. 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 @@ public override string GetCanonicalPath(string path, IPathCache cache) 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 \\?\ diff --git a/csharp/extractor/Semmle.Util/Logger.cs b/csharp/extractor/Semmle.Util/Logger.cs index 524de6dae232..69171dfe37e1 100644 --- a/csharp/extractor/Semmle.Util/Logger.cs +++ b/csharp/extractor/Semmle.Util/Logger.cs @@ -74,9 +74,9 @@ public FileLogger(Verbosity verbosity, string outputFile) 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; } diff --git a/csharp/extractor/Semmle.Util/Win32.cs b/csharp/extractor/Semmle.Util/Win32.cs index d6deee3ed9df..fe198b9612ad 100644 --- a/csharp/extractor/Semmle.Util/Win32.cs +++ b/csharp/extractor/Semmle.Util/Win32.cs @@ -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,