Skip to content

Commit e0f8d7a

Browse files
committed
Fix IDE0039 (Use local function)
1 parent 9307c79 commit e0f8d7a

5 files changed

Lines changed: 97 additions & 96 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
6363
if (memberSyntax is NamespaceDeclarationSyntax)
6464
{
6565
// namespaces are not symbols. So we are just renaming the namespace
66-
Func<CancellationToken, Task<Document>> renameNamespace = cancellationToken =>
66+
Task<Document> RenameNamespace(CancellationToken cancellationToken)
6767
{
6868
IdentifierNameSyntax identifierSyntax = (IdentifierNameSyntax)token.Parent;
6969

7070
var newIdentifierSyntax = identifierSyntax.WithIdentifier(SyntaxFactory.Identifier(newName));
7171

7272
var newRoot = root.ReplaceNode(identifierSyntax, newIdentifierSyntax);
7373
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
74-
};
74+
}
7575

7676
context.RegisterCodeFix(
7777
CodeAction.Create(
7878
string.Format(NamingResources.RenameToCodeFix, newName),
79-
renameNamespace,
79+
(Func<CancellationToken, Task<Document>>)RenameNamespace,
8080
nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id),
8181
diagnostic);
8282
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/ObjectPools/ObjectPoolTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public class ObjectPoolTests
1313
[Fact]
1414
public void TestDefaultConstructor()
1515
{
16-
Func<object> factory = () => new object();
17-
var pool = new ObjectPool<object>(factory);
16+
object Factory() => new object();
17+
var pool = new ObjectPool<object>(Factory);
1818
Assert.IsType<object>(pool.Allocate());
1919
}
2020

2121
[Fact]
2222
public void TestAllocateFree()
2323
{
24-
Func<object> factory = () => new object();
25-
var pool = new ObjectPool<object>(factory);
24+
object Factory() => new object();
25+
var pool = new ObjectPool<object>(Factory);
2626

2727
// Covers the case where no item is in the pool
2828
Assert.IsType<object>(pool.Allocate());
@@ -52,8 +52,8 @@ public void TestAllocateFree()
5252
[Fact]
5353
public void TestObjectCanBeDropped()
5454
{
55-
Func<object> factory = () => new object();
56-
var pool = new ObjectPool<object>(factory, 1);
55+
object Factory() => new object();
56+
var pool = new ObjectPool<object>(Factory, 1);
5757

5858
var obj = new object();
5959
pool.Free(obj);

StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/ObjectPools/SharedPoolsTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,14 @@ public void TestClearAndFreeLarge()
211211
[Fact]
212212
public void TestPooledObjectHandlesNullAllocation()
213213
{
214-
Func<ObjectPool<object>, object> allocator = pool => null;
215-
Action<ObjectPool<object>, object> releaser = (pool, obj) => { };
216-
using (var obj = new PooledObject<object>(SharedPools.Default<object>(), allocator, releaser))
214+
object Allocator(ObjectPool<object> pool)
215+
=> null;
216+
217+
void Releaser(ObjectPool<object> pool, object obj)
218+
{
219+
}
220+
221+
using (var obj = new PooledObject<object>(SharedPools.Default<object>(), Allocator, Releaser))
217222
{
218223
Assert.Null(obj.Object);
219224
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
5858

5959
internal static Func<TSyntax, TProperty> CreateSyntaxPropertyAccessor<TSyntax, TProperty>(Type type, string propertyName)
6060
{
61-
Func<TSyntax, TProperty> fallbackAccessor =
62-
syntax =>
61+
TProperty FallbackAccessor(TSyntax syntax)
62+
{
63+
if (syntax == null)
6364
{
64-
if (syntax == null)
65-
{
66-
// Unlike an extension method which would throw ArgumentNullException here, the light-up
67-
// behavior needs to match behavior of the underlying property.
68-
throw new NullReferenceException();
69-
}
65+
// Unlike an extension method which would throw ArgumentNullException here, the light-up
66+
// behavior needs to match behavior of the underlying property.
67+
throw new NullReferenceException();
68+
}
7069

71-
return default;
72-
};
70+
return default;
71+
}
7372

7473
if (type == null)
7574
{
76-
return fallbackAccessor;
75+
return FallbackAccessor;
7776
}
7877

7978
if (!typeof(TSyntax).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
@@ -84,7 +83,7 @@ internal static Func<TSyntax, TProperty> CreateSyntaxPropertyAccessor<TSyntax, T
8483
var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
8584
if (property == null)
8685
{
87-
return fallbackAccessor;
86+
return FallbackAccessor;
8887
}
8988

9089
if (!typeof(TProperty).GetTypeInfo().IsAssignableFrom(property.PropertyType.GetTypeInfo()))
@@ -107,22 +106,21 @@ internal static Func<TSyntax, TProperty> CreateSyntaxPropertyAccessor<TSyntax, T
107106

108107
internal static Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>> CreateSeparatedSyntaxListPropertyAccessor<TSyntax, TProperty>(Type type, string propertyName)
109108
{
110-
Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>> fallbackAccessor =
111-
syntax =>
109+
SeparatedSyntaxListWrapper<TProperty> FallbackAccessor(TSyntax syntax)
110+
{
111+
if (syntax == null)
112112
{
113-
if (syntax == null)
114-
{
115-
// Unlike an extension method which would throw ArgumentNullException here, the light-up
116-
// behavior needs to match behavior of the underlying property.
117-
throw new NullReferenceException();
118-
}
113+
// Unlike an extension method which would throw ArgumentNullException here, the light-up
114+
// behavior needs to match behavior of the underlying property.
115+
throw new NullReferenceException();
116+
}
119117

120-
return SeparatedSyntaxListWrapper<TProperty>.UnsupportedEmpty;
121-
};
118+
return SeparatedSyntaxListWrapper<TProperty>.UnsupportedEmpty;
119+
}
122120

123121
if (type == null)
124122
{
125-
return fallbackAccessor;
123+
return FallbackAccessor;
126124
}
127125

128126
if (!typeof(TSyntax).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
@@ -133,7 +131,7 @@ internal static Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>> CreateSepar
133131
var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
134132
if (property == null)
135133
{
136-
return fallbackAccessor;
134+
return FallbackAccessor;
137135
}
138136

139137
if (property.PropertyType.GetGenericTypeDefinition() != typeof(SeparatedSyntaxList<>))
@@ -168,27 +166,26 @@ internal static Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>> CreateSepar
168166

169167
internal static Func<TSyntax, TProperty, TSyntax> CreateSyntaxWithPropertyAccessor<TSyntax, TProperty>(Type type, string propertyName)
170168
{
171-
Func<TSyntax, TProperty, TSyntax> fallbackAccessor =
172-
(syntax, newValue) =>
169+
TSyntax FallbackAccessor(TSyntax syntax, TProperty newValue)
170+
{
171+
if (syntax == null)
173172
{
174-
if (syntax == null)
175-
{
176-
// Unlike an extension method which would throw ArgumentNullException here, the light-up
177-
// behavior needs to match behavior of the underlying property.
178-
throw new NullReferenceException();
179-
}
173+
// Unlike an extension method which would throw ArgumentNullException here, the light-up
174+
// behavior needs to match behavior of the underlying property.
175+
throw new NullReferenceException();
176+
}
180177

181-
if (Equals(newValue, default(TProperty)))
182-
{
183-
return syntax;
184-
}
178+
if (Equals(newValue, default(TProperty)))
179+
{
180+
return syntax;
181+
}
185182

186-
throw new NotSupportedException();
187-
};
183+
throw new NotSupportedException();
184+
}
188185

189186
if (type == null)
190187
{
191-
return fallbackAccessor;
188+
return FallbackAccessor;
192189
}
193190

194191
if (!typeof(TSyntax).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
@@ -199,7 +196,7 @@ internal static Func<TSyntax, TProperty, TSyntax> CreateSyntaxWithPropertyAccess
199196
var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
200197
if (property == null)
201198
{
202-
return fallbackAccessor;
199+
return FallbackAccessor;
203200
}
204201

205202
if (!typeof(TProperty).GetTypeInfo().IsAssignableFrom(property.PropertyType.GetTypeInfo()))
@@ -231,27 +228,26 @@ internal static Func<TSyntax, TProperty, TSyntax> CreateSyntaxWithPropertyAccess
231228

232229
internal static Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>, TSyntax> CreateSeparatedSyntaxListWithPropertyAccessor<TSyntax, TProperty>(Type type, string propertyName)
233230
{
234-
Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>, TSyntax> fallbackAccessor =
235-
(syntax, newValue) =>
231+
TSyntax FallbackAccessor(TSyntax syntax, SeparatedSyntaxListWrapper<TProperty> newValue)
232+
{
233+
if (syntax == null)
236234
{
237-
if (syntax == null)
238-
{
239-
// Unlike an extension method which would throw ArgumentNullException here, the light-up
240-
// behavior needs to match behavior of the underlying property.
241-
throw new NullReferenceException();
242-
}
235+
// Unlike an extension method which would throw ArgumentNullException here, the light-up
236+
// behavior needs to match behavior of the underlying property.
237+
throw new NullReferenceException();
238+
}
243239

244-
if (newValue is null)
245-
{
246-
return syntax;
247-
}
240+
if (newValue is null)
241+
{
242+
return syntax;
243+
}
248244

249-
throw new NotSupportedException();
250-
};
245+
throw new NotSupportedException();
246+
}
251247

252248
if (type == null)
253249
{
254-
return fallbackAccessor;
250+
return FallbackAccessor;
255251
}
256252

257253
if (!typeof(TSyntax).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
@@ -262,7 +258,7 @@ internal static Func<TSyntax, SeparatedSyntaxListWrapper<TProperty>, TSyntax> Cr
262258
var property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
263259
if (property == null)
264260
{
265-
return fallbackAccessor;
261+
return FallbackAccessor;
266262
}
267263

268264
if (property.PropertyType.GetGenericTypeDefinition() != typeof(SeparatedSyntaxList<>))

StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -293,41 +293,41 @@ public string GetCopyrightText(string fileName)
293293
private KeyValuePair<string, bool> BuildCopyrightText(string fileName)
294294
{
295295
bool canCache = true;
296-
string pattern = Regex.Escape("{") + "(?<Property>[a-zA-Z0-9]+)" + Regex.Escape("}");
297-
MatchEvaluator evaluator =
298-
match =>
299-
{
300-
string key = match.Groups["Property"].Value;
301-
switch (key)
302-
{
303-
case "companyName":
304-
return this.CompanyName;
305296

306-
case "copyrightText":
307-
return "[CircularReference]";
297+
string Evaluator(Match match)
298+
{
299+
string key = match.Groups["Property"].Value;
300+
switch (key)
301+
{
302+
case "companyName":
303+
return this.CompanyName;
308304

309-
default:
310-
string value;
311-
if (this.Variables.TryGetValue(key, out value))
312-
{
313-
return value;
314-
}
305+
case "copyrightText":
306+
return "[CircularReference]";
315307

316-
if (key == "fileName")
317-
{
318-
// The 'fileName' built-in variable is only applied when the user did not include an
319-
// explicit value for a custom 'fileName' variable.
320-
canCache = false;
321-
return fileName;
322-
}
308+
default:
309+
string value;
310+
if (this.Variables.TryGetValue(key, out value))
311+
{
312+
return value;
313+
}
323314

324-
break;
315+
if (key == "fileName")
316+
{
317+
// The 'fileName' built-in variable is only applied when the user did not include an
318+
// explicit value for a custom 'fileName' variable.
319+
canCache = false;
320+
return fileName;
325321
}
326322

327-
return "[InvalidReference]";
328-
};
323+
break;
324+
}
329325

330-
string expanded = Regex.Replace(this.copyrightText, pattern, evaluator);
326+
return "[InvalidReference]";
327+
}
328+
329+
string pattern = Regex.Escape("{") + "(?<Property>[a-zA-Z0-9]+)" + Regex.Escape("}");
330+
string expanded = Regex.Replace(this.copyrightText, pattern, Evaluator);
331331
return new KeyValuePair<string, bool>(expanded, canCache);
332332
}
333333
}

0 commit comments

Comments
 (0)