Skip to content

Commit edd256b

Browse files
authored
Lambdas cleanup (#1995)
* don't create unnecessary lambdas * the Argument<T> ctor will throw for null anyway * avoid closure allocations
1 parent c33ead5 commit edd256b

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/System.CommandLine/Argument{T}.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Argument(
3939
Func<T> defaultValueFactory,
4040
string? description = null) : this(name, description)
4141
{
42-
SetDefaultValueFactory(() => defaultValueFactory());
42+
SetDefaultValueFactory(defaultValueFactory);
4343
}
4444

4545
/// <summary>
@@ -63,7 +63,7 @@ public Argument(
6363
/// <exception cref="ArgumentNullException">Thrown when <paramref name="defaultValueFactory"/> is null.</exception>
6464
public Argument(Func<T> defaultValueFactory) : this()
6565
{
66-
SetDefaultValueFactory(() => defaultValueFactory());
66+
SetDefaultValueFactory(defaultValueFactory);
6767
}
6868

6969
/// <summary>
@@ -132,7 +132,7 @@ public Argument(Func<ArgumentResult, T> parse, bool isDefault = false) : this(nu
132132
/// <param name="value">The default value for the argument.</param>
133133
public void SetDefaultValue(T value)
134134
{
135-
SetDefaultValueFactory(() => value);
135+
SetDefaultValueFactory(_ => value);
136136
}
137137

138138
/// <summary>
@@ -206,10 +206,10 @@ void UnrecognizedArgumentError(ArgumentResult argumentResult)
206206
/// </summary>
207207
public void AcceptLegalFilePathsOnly()
208208
{
209-
var invalidPathChars = Path.GetInvalidPathChars();
210-
211-
Validators.Add(result =>
209+
Validators.Add(static result =>
212210
{
211+
var invalidPathChars = Path.GetInvalidPathChars();
212+
213213
for (var i = 0; i < result.Tokens.Count; i++)
214214
{
215215
var token = result.Tokens[i];
@@ -232,10 +232,10 @@ public void AcceptLegalFilePathsOnly()
232232
/// <remarks>A parse error will result, for example, if file path separators are found in the parsed value.</remarks>
233233
public void AcceptLegalFileNamesOnly()
234234
{
235-
var invalidFileNameChars = Path.GetInvalidFileNameChars();
236-
237-
Validators.Add(result =>
235+
Validators.Add(static result =>
238236
{
237+
var invalidFileNameChars = Path.GetInvalidFileNameChars();
238+
239239
for (var i = 0; i < result.Tokens.Count; i++)
240240
{
241241
var token = result.Tokens[i];

src/System.CommandLine/Option{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public Option(
5151
Func<T> defaultValueFactory,
5252
string? description = null)
5353
: this(name, description,
54-
new Argument<T>(defaultValueFactory ?? throw new ArgumentNullException(nameof(defaultValueFactory))))
54+
new Argument<T>(defaultValueFactory))
5555
{ }
5656

5757
/// <inheritdoc/>
5858
public Option(
5959
string[] aliases,
6060
Func<T> defaultValueFactory,
6161
string? description = null)
62-
: this(aliases, description, new Argument<T>(defaultValueFactory ?? throw new ArgumentNullException(nameof(defaultValueFactory))))
62+
: this(aliases, description, new Argument<T>(defaultValueFactory))
6363
{
6464
}
6565

0 commit comments

Comments
 (0)