Skip to content

Commit 4b323cd

Browse files
authored
Merge pull request github#15050 from michaelnebel/csharp/defaultparamobject
C#: Default parameters for object using attributes.
2 parents 97fc20c + fd12c3a commit 4b323cd

File tree

6 files changed

+9
-14
lines changed

6 files changed

+9
-14
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,6 @@ private static bool ContainsPattern(SyntaxNode node) =>
202202
return Default.CreateGenerated(cx, parent, childIndex, location, parameter.Type.IsReferenceType ? ValueAsString(null) : null);
203203
}
204204

205-
if (parameter.Type.SpecialType is SpecialType.System_Object)
206-
{
207-
// this can happen in VB.NET
208-
cx.ExtractionError($"Extracting default argument value 'object {parameter.Name} = default' instead of 'object {parameter.Name} = {defaultValue}'. The latter is not supported in C#.",
209-
null, null, severity: Semmle.Util.Logging.Severity.Warning);
210-
211-
// we're generating a default expression:
212-
return Default.CreateGenerated(cx, parent, childIndex, location, ValueAsString(null));
213-
}
214-
215205
if (type.SpecialType is SpecialType.None)
216206
{
217207
return ImplicitCast.CreateGeneratedConversion(cx, parent, childIndex, type, defaultValue, location);
@@ -222,7 +212,9 @@ private static bool ContainsPattern(SyntaxNode node) =>
222212
return DateTimeObjectCreation.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
223213
}
224214

225-
if (type.SpecialType is SpecialType.System_IntPtr || type.SpecialType is SpecialType.System_UIntPtr)
215+
if (type.SpecialType is SpecialType.System_Object ||
216+
type.SpecialType is SpecialType.System_IntPtr ||
217+
type.SpecialType is SpecialType.System_UIntPtr)
226218
{
227219
return ImplicitCast.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
228220
}

csharp/ql/test/library-tests/parameters/Parameters.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Parameters
2929
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
3030
public void M22(nuint arg11 = 0) => throw null;
3131
public void M23(nint arg12 = 0) => throw null;
32+
public void M24([Optional, DefaultParameterValue(7)] object arg13) => throw null;
3233

3334
public struct MyStruct
3435
{

csharp/ql/test/library-tests/parameters/Parameters.cs_

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ParametersDll
2929
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
3030
public void M22(nuint arg11 = 0) => throw null;
3131
public void M23(nint arg12 = 0) => throw null;
32+
public void M24([Optional, DefaultParameterValue(7)] object arg13) => throw null;
3233

3334
public struct MyStruct
3435
{
Binary file not shown.

csharp/ql/test/library-tests/parameters/Parameters.expected

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ noDefaultValue
55
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:24:8:24 | a | 0 |
66
| Parameters.cs:12:17:12:18 | M6 | Parameters.cs:12:29:12:30 | s1 | 0 |
77
| Parameters.cs:13:17:13:18 | M7 | Parameters.cs:13:27:13:28 | e1 | 0 |
8-
| Parameters.cs:35:32:35:39 | implicit conversion | Parameters.cs:35:54:35:54 | i | 0 |
9-
| Parameters.cs:36:32:36:39 | implicit conversion | Parameters.cs:36:57:36:57 | s | 0 |
8+
| Parameters.cs:36:32:36:39 | implicit conversion | Parameters.cs:36:54:36:54 | i | 0 |
9+
| Parameters.cs:37:32:37:39 | implicit conversion | Parameters.cs:37:57:37:57 | s | 0 |
1010
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | a | 0 |
1111
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | b | 1 |
1212
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | c | 2 |
@@ -47,6 +47,7 @@ withDefaultValue
4747
| Parameters.cs:29:17:29:19 | M21 | Parameters.cs:29:76:29:80 | arg10 | 0 | Parameters.cs:29:21:29:80 | call to operator implicit conversion | - |
4848
| Parameters.cs:30:17:30:19 | M22 | Parameters.cs:30:27:30:31 | arg11 | 0 | Parameters.cs:30:35:30:35 | (...) ... | 0 |
4949
| Parameters.cs:31:17:31:19 | M23 | Parameters.cs:31:26:31:30 | arg12 | 0 | Parameters.cs:31:34:31:34 | (...) ... | 0 |
50+
| Parameters.cs:32:17:32:19 | M24 | Parameters.cs:32:65:32:69 | arg13 | 0 | Parameters.cs:32:21:32:69 | (...) ... | 7 |
5051
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | b | 1 | Parameters.dll:0:0:0:0 | default | null |
5152
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | c | 2 | Parameters.dll:0:0:0:0 | "default string" | default string |
5253
| Parameters.dll:0:0:0:0 | M3 | Parameters.dll:0:0:0:0 | a | 0 | Parameters.dll:0:0:0:0 | 1 | 1 |
@@ -78,6 +79,7 @@ withDefaultValue
7879
| Parameters.dll:0:0:0:0 | M21 | Parameters.dll:0:0:0:0 | arg10 | 0 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | - |
7980
| Parameters.dll:0:0:0:0 | M22 | Parameters.dll:0:0:0:0 | arg11 | 0 | Parameters.dll:0:0:0:0 | (...) ... | 0 |
8081
| Parameters.dll:0:0:0:0 | M23 | Parameters.dll:0:0:0:0 | arg12 | 0 | Parameters.dll:0:0:0:0 | (...) ... | 0 |
82+
| Parameters.dll:0:0:0:0 | M24 | Parameters.dll:0:0:0:0 | arg13 | 0 | Parameters.dll:0:0:0:0 | (...) ... | 7 |
8183
dateTimeDefaults
8284
| Parameters.cs:22:17:22:19 | M14 | Parameters.cs:22:64:22:67 | arg4 | Parameters.cs:22:21:22:67 | object creation of type DateTime | DateTime(long) | 14 |
8385
| Parameters.cs:23:17:23:19 | M15 | Parameters.cs:23:68:23:71 | arg5 | Parameters.cs:23:21:23:71 | object creation of type DateTime | DateTime(long) | 10001 |

csharp/ql/test/library-tests/standalone/errorrecovery/DiagnosticsAndErrors.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@ extractorMessages
4141
| errors.cs:91:45:91:45 | Failed to resolve name |
4242
| errors.cs:92:45:92:45 | Failed to determine type |
4343
| errors.cs:92:45:92:45 | Failed to resolve name |
44-
| file://:0:0:0:0 | Extracting default argument value 'object RecordNumber = default' instead of 'object RecordNumber = -1'. The latter is not supported in C#. |

0 commit comments

Comments
 (0)