mirror of
https://github.com/github/codeql.git
synced 2026-04-25 16:55:19 +02:00
Merge pull request #4952 from tamasvajk/feature/type-mention-nullable
C#: Fix type mention extraction of named types with nullability enabled
This commit is contained in:
@@ -497,13 +497,13 @@ namespace Semmle.Extraction.CSharp
|
||||
/// <summary>
|
||||
/// Holds if this symbol is a source declaration.
|
||||
/// </summary>
|
||||
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition);
|
||||
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.IncludeNullability.Equals(symbol, symbol.OriginalDefinition);
|
||||
|
||||
/// <summary>
|
||||
/// Holds if this method is a source declaration.
|
||||
/// </summary>
|
||||
public static bool IsSourceDeclaration(this IMethodSymbol method) =>
|
||||
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.Default.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
|
||||
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.IncludeNullability.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
|
||||
|
||||
/// <summary>
|
||||
/// Holds if this parameter is a source declaration.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
#nullable disable
|
||||
|
||||
public class Base { }
|
||||
public class Derived : Base { }
|
||||
public interface I0 { }
|
||||
public interface I1 : I0 { }
|
||||
public struct Str : I1 { }
|
||||
|
||||
public class C1
|
||||
{
|
||||
public void M1()
|
||||
{
|
||||
Derived? d0 = new Derived();
|
||||
Derived d1 = new Derived();
|
||||
Str? s0 = new Str();
|
||||
Str s1 = new Str();
|
||||
I1? i0 = new Str();
|
||||
I1 i1 = new Str();
|
||||
}
|
||||
}
|
||||
24
csharp/ql/test/library-tests/typeMentions/NullableEnabled.cs
Normal file
24
csharp/ql/test/library-tests/typeMentions/NullableEnabled.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
#nullable enable
|
||||
|
||||
public class Base { }
|
||||
public class Derived : Base { }
|
||||
public interface I0 { }
|
||||
public interface I1 : I0 { }
|
||||
public struct Str : I1 { }
|
||||
|
||||
public class C1
|
||||
{
|
||||
public void M1()
|
||||
{
|
||||
Derived? d0 = new Derived();
|
||||
Derived d1 = new Derived();
|
||||
Str? s0 = new Str();
|
||||
Str s1 = new Str();
|
||||
I1? i0 = new Str();
|
||||
I1 i1 = new Str();
|
||||
}
|
||||
}
|
||||
|
||||
// semmle-extractor-options: --separate-compilation
|
||||
@@ -0,0 +1,37 @@
|
||||
typeMentions
|
||||
| NullableDisabled.cs:6:24:6:27 | Base |
|
||||
| NullableDisabled.cs:8:23:8:24 | I0 |
|
||||
| NullableDisabled.cs:9:21:9:22 | I1 |
|
||||
| NullableDisabled.cs:13:12:13:15 | Void |
|
||||
| NullableDisabled.cs:15:9:15:15 | Derived |
|
||||
| NullableDisabled.cs:15:27:15:33 | Derived |
|
||||
| NullableDisabled.cs:16:9:16:15 | Derived |
|
||||
| NullableDisabled.cs:16:26:16:32 | Derived |
|
||||
| NullableDisabled.cs:17:9:17:11 | Str |
|
||||
| NullableDisabled.cs:17:9:17:12 | Nullable<Str> |
|
||||
| NullableDisabled.cs:17:23:17:25 | Str |
|
||||
| NullableDisabled.cs:18:9:18:11 | Str |
|
||||
| NullableDisabled.cs:18:22:18:24 | Str |
|
||||
| NullableDisabled.cs:19:9:19:10 | I1 |
|
||||
| NullableDisabled.cs:19:22:19:24 | Str |
|
||||
| NullableDisabled.cs:20:9:20:10 | I1 |
|
||||
| NullableDisabled.cs:20:21:20:23 | Str |
|
||||
| NullableEnabled.cs:6:24:6:27 | Base |
|
||||
| NullableEnabled.cs:8:23:8:24 | I0 |
|
||||
| NullableEnabled.cs:9:21:9:22 | I1 |
|
||||
| NullableEnabled.cs:13:12:13:15 | Void |
|
||||
| NullableEnabled.cs:15:9:15:15 | Derived |
|
||||
| NullableEnabled.cs:15:27:15:33 | Derived |
|
||||
| NullableEnabled.cs:16:9:16:15 | Derived |
|
||||
| NullableEnabled.cs:16:26:16:32 | Derived |
|
||||
| NullableEnabled.cs:17:9:17:11 | Str |
|
||||
| NullableEnabled.cs:17:9:17:12 | Nullable<Str> |
|
||||
| NullableEnabled.cs:17:23:17:25 | Str |
|
||||
| NullableEnabled.cs:18:9:18:11 | Str |
|
||||
| NullableEnabled.cs:18:22:18:24 | Str |
|
||||
| NullableEnabled.cs:19:9:19:10 | I1 |
|
||||
| NullableEnabled.cs:19:22:19:24 | Str |
|
||||
| NullableEnabled.cs:20:9:20:10 | I1 |
|
||||
| NullableEnabled.cs:20:21:20:23 | Str |
|
||||
duplicates
|
||||
diff
|
||||
29
csharp/ql/test/library-tests/typeMentions/TypeMentions.ql
Normal file
29
csharp/ql/test/library-tests/typeMentions/TypeMentions.ql
Normal file
@@ -0,0 +1,29 @@
|
||||
import csharp
|
||||
|
||||
private predicate matchingLineLoc(TypeMention tm0, TypeMention tm1) {
|
||||
tm0.getLocation().getStartLine() = tm1.getLocation().getStartLine() and
|
||||
tm0.getLocation().getEndLine() = tm1.getLocation().getEndLine() and
|
||||
tm0.getLocation().getStartColumn() = tm1.getLocation().getStartColumn() and
|
||||
tm0.getLocation().getEndColumn() = tm1.getLocation().getEndColumn()
|
||||
}
|
||||
|
||||
private predicate matchingFile(TypeMention tm0, TypeMention tm1) {
|
||||
tm0.getLocation().getFile().getStem() = tm1.getLocation().getFile().getStem()
|
||||
}
|
||||
|
||||
query predicate typeMentions(TypeMention tm) { any() }
|
||||
|
||||
query predicate duplicates(TypeMention tm) {
|
||||
exists(TypeMention other |
|
||||
matchingLineLoc(other, tm) and
|
||||
matchingFile(other, tm) and
|
||||
other != tm
|
||||
)
|
||||
}
|
||||
|
||||
query predicate diff(TypeMention tm) {
|
||||
not exists(TypeMention other |
|
||||
matchingLineLoc(other, tm) and
|
||||
not matchingFile(other, tm)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user