Skip to content

Commit 36a1293

Browse files
authored
Merge pull request github#4952 from tamasvajk/feature/type-mention-nullable
C#: Fix type mention extraction of named types with nullability enabled
2 parents 3468593 + 61b207f commit 36a1293

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

csharp/extractor/Semmle.Extraction.CSharp/SymbolExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,13 @@ public static IEnumerable<IParameterSymbol> GetParameters(this ISymbol parameter
497497
/// <summary>
498498
/// Holds if this symbol is a source declaration.
499499
/// </summary>
500-
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition);
500+
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.IncludeNullability.Equals(symbol, symbol.OriginalDefinition);
501501

502502
/// <summary>
503503
/// Holds if this method is a source declaration.
504504
/// </summary>
505505
public static bool IsSourceDeclaration(this IMethodSymbol method) =>
506-
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.Default.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
506+
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.IncludeNullability.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
507507

508508
/// <summary>
509509
/// Holds if this parameter is a source declaration.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
#nullable disable
4+
5+
public class Base { }
6+
public class Derived : Base { }
7+
public interface I0 { }
8+
public interface I1 : I0 { }
9+
public struct Str : I1 { }
10+
11+
public class C1
12+
{
13+
public void M1()
14+
{
15+
Derived? d0 = new Derived();
16+
Derived d1 = new Derived();
17+
Str? s0 = new Str();
18+
Str s1 = new Str();
19+
I1? i0 = new Str();
20+
I1 i1 = new Str();
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
#nullable enable
4+
5+
public class Base { }
6+
public class Derived : Base { }
7+
public interface I0 { }
8+
public interface I1 : I0 { }
9+
public struct Str : I1 { }
10+
11+
public class C1
12+
{
13+
public void M1()
14+
{
15+
Derived? d0 = new Derived();
16+
Derived d1 = new Derived();
17+
Str? s0 = new Str();
18+
Str s1 = new Str();
19+
I1? i0 = new Str();
20+
I1 i1 = new Str();
21+
}
22+
}
23+
24+
// semmle-extractor-options: --separate-compilation
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
typeMentions
2+
| NullableDisabled.cs:6:24:6:27 | Base |
3+
| NullableDisabled.cs:8:23:8:24 | I0 |
4+
| NullableDisabled.cs:9:21:9:22 | I1 |
5+
| NullableDisabled.cs:13:12:13:15 | Void |
6+
| NullableDisabled.cs:15:9:15:15 | Derived |
7+
| NullableDisabled.cs:15:27:15:33 | Derived |
8+
| NullableDisabled.cs:16:9:16:15 | Derived |
9+
| NullableDisabled.cs:16:26:16:32 | Derived |
10+
| NullableDisabled.cs:17:9:17:11 | Str |
11+
| NullableDisabled.cs:17:9:17:12 | Nullable<Str> |
12+
| NullableDisabled.cs:17:23:17:25 | Str |
13+
| NullableDisabled.cs:18:9:18:11 | Str |
14+
| NullableDisabled.cs:18:22:18:24 | Str |
15+
| NullableDisabled.cs:19:9:19:10 | I1 |
16+
| NullableDisabled.cs:19:22:19:24 | Str |
17+
| NullableDisabled.cs:20:9:20:10 | I1 |
18+
| NullableDisabled.cs:20:21:20:23 | Str |
19+
| NullableEnabled.cs:6:24:6:27 | Base |
20+
| NullableEnabled.cs:8:23:8:24 | I0 |
21+
| NullableEnabled.cs:9:21:9:22 | I1 |
22+
| NullableEnabled.cs:13:12:13:15 | Void |
23+
| NullableEnabled.cs:15:9:15:15 | Derived |
24+
| NullableEnabled.cs:15:27:15:33 | Derived |
25+
| NullableEnabled.cs:16:9:16:15 | Derived |
26+
| NullableEnabled.cs:16:26:16:32 | Derived |
27+
| NullableEnabled.cs:17:9:17:11 | Str |
28+
| NullableEnabled.cs:17:9:17:12 | Nullable<Str> |
29+
| NullableEnabled.cs:17:23:17:25 | Str |
30+
| NullableEnabled.cs:18:9:18:11 | Str |
31+
| NullableEnabled.cs:18:22:18:24 | Str |
32+
| NullableEnabled.cs:19:9:19:10 | I1 |
33+
| NullableEnabled.cs:19:22:19:24 | Str |
34+
| NullableEnabled.cs:20:9:20:10 | I1 |
35+
| NullableEnabled.cs:20:21:20:23 | Str |
36+
duplicates
37+
diff
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import csharp
2+
3+
private predicate matchingLineLoc(TypeMention tm0, TypeMention tm1) {
4+
tm0.getLocation().getStartLine() = tm1.getLocation().getStartLine() and
5+
tm0.getLocation().getEndLine() = tm1.getLocation().getEndLine() and
6+
tm0.getLocation().getStartColumn() = tm1.getLocation().getStartColumn() and
7+
tm0.getLocation().getEndColumn() = tm1.getLocation().getEndColumn()
8+
}
9+
10+
private predicate matchingFile(TypeMention tm0, TypeMention tm1) {
11+
tm0.getLocation().getFile().getStem() = tm1.getLocation().getFile().getStem()
12+
}
13+
14+
query predicate typeMentions(TypeMention tm) { any() }
15+
16+
query predicate duplicates(TypeMention tm) {
17+
exists(TypeMention other |
18+
matchingLineLoc(other, tm) and
19+
matchingFile(other, tm) and
20+
other != tm
21+
)
22+
}
23+
24+
query predicate diff(TypeMention tm) {
25+
not exists(TypeMention other |
26+
matchingLineLoc(other, tm) and
27+
not matchingFile(other, tm)
28+
)
29+
}

0 commit comments

Comments
 (0)