Skip to content

Commit fb3ce46

Browse files
committed
C#: Address review comments.
1 parent 5c931fa commit fb3ce46

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public static bool ConstructedOrParentIsConstructed(INamedTypeSymbol symbol)
2525
symbol.ContainingType is not null && ConstructedOrParentIsConstructed(symbol.ContainingType);
2626
}
2727

28+
29+
/// <summary>
30+
/// A hashset containing the C# contextual keywords that could be confused with types (and typing).
31+
///
32+
/// For the list of all contextual keywords, see
33+
/// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/#contextual-keywords
34+
/// </summary>
35+
private readonly HashSet<string> ContextualKeywordTypes = [
36+
"dynamic",
37+
"nint",
38+
"nuint",
39+
"var"
40+
];
41+
2842
/// <summary>
2943
/// Returns true in case we suspect this is a broken type.
3044
/// </summary>
@@ -40,8 +54,9 @@ private bool IsBrokenType(ITypeSymbol symbol)
4054

4155
// (1) public class { ... } is a broken type as it doesn't have a name.
4256
// (2) public class var { ... } is an allowed type, but it overrides the `var` keyword for all uses.
43-
// It is probably a better heuristic to treat it as a broken type.
44-
return string.IsNullOrEmpty(symbol.Name) || symbol.Name == "var";
57+
// The same goes for other contextual keywords that could be used as type names.
58+
// It is probably a better heuristic to treat these as broken types.
59+
return string.IsNullOrEmpty(symbol.Name) || ContextualKeywordTypes.Contains(symbol.Name);
4560
}
4661

4762
public Kinds.TypeKind GetTypeKind(Context cx, bool constructUnderlyingTupleType)

csharp/extractor/Semmle.Extraction.CSharp/Extractor/BinaryLogExtractionContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public BinaryLogExtractionContext(string cwd, string[] args, string outputPath,
4747

4848
public static string? GetAdjustedPath(ExtractionContext extractionContext, string sourcePath)
4949
{
50-
if (extractionContext.Mode.HasFlag(ExtractorMode.BinaryLog)
50+
if (extractionContext.IsBinaryLog
5151
&& extractionContext is BinaryLogExtractionContext binaryLogExtractionContext
5252
&& binaryLogExtractionContext.GetAdjustedPath(sourcePath) is string adjustedPath)
5353
{

csharp/extractor/Semmle.Extraction.CSharp/Extractor/ExtractionContext.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ExtractionContext
1616
public string OutputPath { get; }
1717
public IEnumerable<CompilationInfo> CompilationInfos { get; }
1818
public bool IsStandalone => Mode.HasFlag(ExtractorMode.Standalone);
19+
public bool IsBinaryLog => Mode.HasFlag(ExtractorMode.BinaryLog);
1920

2021
/// <summary>
2122
/// Creates a new extractor instance for one compilation unit.

0 commit comments

Comments
 (0)