Skip to content

Commit 02b1108

Browse files
committed
C#: Add more tests for cs/useless-cast-to-self
1 parent ccd7bb5 commit 02b1108

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

csharp/ql/test/query-tests/Language Abuse/UselessCastToSelf/UselessCastToSelf.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq.Expressions;
23

34
class Test
45
{
@@ -8,6 +9,8 @@ void f()
89
var bad1 = (int)1;
910
var bad2 = (Test)this;
1011
var bad3 = this as Test;
12+
func = (Func<int, int?>)(x => x); // MISSING
13+
exprFunc = (Expression<Func<int, int?>>)(x => x);
1114

1215
// GOOD
1316
var good1 = (object)1;
@@ -17,6 +20,8 @@ void f()
1720
var good5 = (Action<int>)(x => { });
1821
var good6 = (Action<int>)(delegate (int x) { });
1922
var good7 = (Action<int>)((int x) => { });
23+
func = x => x;
24+
exprFunc = x => x;
2025
}
2126

2227
enum Enum
@@ -27,4 +32,7 @@ enum Enum
2732
D = 9 | (32 << A),
2833
E = 9 | (32 << (int)A) // BAD
2934
}
35+
36+
private Func<int, int?> func;
37+
private Expression<Func<int, int?>> exprFunc;
3038
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
| UselessCastToSelf.cs:8:20:8:25 | (...) ... | This cast is redundant because the expression already has type Int32. |
2-
| UselessCastToSelf.cs:9:20:9:29 | (...) ... | This cast is redundant because the expression already has type Test. |
3-
| UselessCastToSelf.cs:10:20:10:31 | ... as ... | This cast is redundant because the expression already has type Test. |
4-
| UselessCastToSelf.cs:26:17:26:22 | (...) ... | This cast is redundant because the expression already has type Int32. |
5-
| UselessCastToSelf.cs:28:24:28:29 | (...) ... | This cast is redundant because the expression already has type Int32. |
1+
| UselessCastToSelf.cs:9:20:9:25 | (...) ... | This cast is redundant because the expression already has type Int32. |
2+
| UselessCastToSelf.cs:10:20:10:29 | (...) ... | This cast is redundant because the expression already has type Test. |
3+
| UselessCastToSelf.cs:11:20:11:31 | ... as ... | This cast is redundant because the expression already has type Test. |
4+
| UselessCastToSelf.cs:13:20:13:56 | (...) ... | This cast is redundant because the expression already has type Expression<Func<Int32,Nullable<Int32>>>. |
5+
| UselessCastToSelf.cs:31:17:31:22 | (...) ... | This cast is redundant because the expression already has type Int32. |
6+
| UselessCastToSelf.cs:33:24:33:29 | (...) ... | This cast is redundant because the expression already has type Int32. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj

0 commit comments

Comments
 (0)