Skip to content

Commit e4f7bfb

Browse files
authored
Enable CA1821, CA1825-CA1834 (dotnet#33041)
* Enable CA1821, CA1825-CA1834 Contributes to dotnet#24055
1 parent b62acdf commit e4f7bfb

File tree

61 files changed

+196
-157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+196
-157
lines changed

.editorconfig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,40 @@ dotnet_diagnostic.CA1802.severity = warning
9191
# CA1805: Do not initialize unnecessarily
9292
dotnet_diagnostic.CA1805.severity = warning
9393

94+
# CA1823: Remove empty Finalizers
95+
dotnet_diagnostic.CA1821.severity = warning
96+
9497
# CA1823: Avoid unused private fields
9598
dotnet_diagnostic.CA1823.severity = warning
9699

100+
# CA1823: Avoid zero-length array allocations
101+
dotnet_diagnostic.CA1825.severity = warning
102+
103+
# CA1826: Do not use Enumerable methods on indexable collections. Instead use the collection directly
104+
dotnet_diagnostic.CA1826.severity = warning
105+
106+
# CA1827: Do not use Count() or LongCount() when Any() can be used
107+
dotnet_diagnostic.CA1827.severity = warning
108+
109+
# CA1828: Do not use CountAsync() or LongCountAsync() when AnyAsync() can be used
110+
dotnet_diagnostic.CA1828.severity = warning
111+
112+
# CA1829: Use Length/Count property instead of Count() when available
113+
dotnet_diagnostic.CA1829.severity = warning
114+
115+
# CA1830: Prefer strongly-typed Append and Insert method overloads on StringBuilder
116+
dotnet_diagnostic.CA1830.severity = warning
117+
118+
# CA1831: Use AsSpan or AsMemory instead of Range-based indexers when appropriate
119+
# CA1832: Use AsSpan or AsMemory instead of Range-based indexers when appropriate
120+
# CA1833: Use AsSpan or AsMemory instead of Range-based indexers when appropriate
121+
dotnet_diagnostic.CA1831.severity = warning
122+
dotnet_diagnostic.CA1832.severity = warning
123+
dotnet_diagnostic.CA1833.severity = warning
124+
125+
# CA1834: Consider using 'StringBuilder.Append(char)' when applicable
126+
dotnet_diagnostic.CA1834.severity = warning
127+
97128
# CA2012: Use ValueTask correctly
98129
dotnet_diagnostic.CA2012.severity = warning
99130

@@ -110,5 +141,15 @@ dotnet_diagnostic.CA1507.severity = suggestion
110141
dotnet_diagnostic.CA1802.severity = suggestion
111142
# CA1805: Do not initialize unnecessarily
112143
dotnet_diagnostic.CA1805.severity = suggestion
144+
# CA1823: Avoid zero-length array allocations
145+
dotnet_diagnostic.CA1825.severity = suggestion
146+
# CA1826: Do not use Enumerable methods on indexable collections. Instead use the collection directly
147+
dotnet_diagnostic.CA1826.severity = suggestion
148+
# CA1827: Do not use Count() or LongCount() when Any() can be used
149+
dotnet_diagnostic.CA1827.severity = suggestion
150+
# CA1829: Use Length/Count property instead of Count() when available
151+
dotnet_diagnostic.CA1829.severity = suggestion
152+
# CA1834: Consider using 'StringBuilder.Append(char)' when applicable
153+
dotnet_diagnostic.CA1834.severity = suggestion
113154
# CA2012: Use ValueTask correctly
114155
dotnet_diagnostic.CA2012.severity = suggestion

eng/tools/BaselineGenerator/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private async Task<int> Run()
168168
foreach (var group in reader.NuspecReader.GetDependencyGroups())
169169
{
170170
// Don't bother generating empty ItemGroup elements.
171-
if (group.Packages.Count() == 0)
171+
if (!group.Packages.Any())
172172
{
173173
continue;
174174
}

src/Components/Analyzers/test/Verifiers/DiagnosticVerifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void VerifyDiagnostics(string[] sources, string language, DiagnosticAnal
109109
/// <param name="expectedResults">Diagnostic Results that should have appeared in the code</param>
110110
private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResults, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expectedResults)
111111
{
112-
int expectedCount = expectedResults.Count();
112+
int expectedCount = expectedResults.Length;
113113
int actualCount = actualResults.Count();
114114

115115
if (expectedCount != actualCount)

src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ private void CreateDefaultLogMessage(StringBuilder logBuilder, LogLevel logLevel
119119
logBuilder.Append(GetLogLevelString(logLevel));
120120
logBuilder.Append(_loglevelPadding);
121121
logBuilder.Append(logName);
122-
logBuilder.Append("[");
122+
logBuilder.Append('[');
123123
logBuilder.Append(eventId);
124-
logBuilder.Append("]");
124+
logBuilder.Append(']');
125125

126126
if (!string.IsNullOrEmpty(message))
127127
{

src/Components/WebView/WebView/src/QueryString.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ public override int GetHashCode()
292292

293293
private static void AppendKeyValuePair(StringBuilder builder, string key, string? value, bool first)
294294
{
295-
builder.Append(first ? "?" : "&");
295+
builder.Append(first ? '?' : '&');
296296
builder.Append(UrlEncoder.Default.Encode(key));
297-
builder.Append("=");
297+
builder.Append('=');
298298
if (!string.IsNullOrEmpty(value))
299299
{
300300
builder.Append(UrlEncoder.Default.Encode(value));

src/FileProviders/Embedded/src/Manifest/ManifestParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private static string EnsureName(XElement element)
143143

144144
private static string EnsureText(XElement element)
145145
{
146-
if (element.Elements().Count() == 0 &&
146+
if (!element.Elements().Any() &&
147147
!element.IsEmpty &&
148148
element.Nodes().Count() == 1 &&
149149
element.FirstNode?.NodeType == XmlNodeType.Text)

src/Http/Headers/src/CookieHeaderValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public override string ToString()
9494
var header = new StringBuilder();
9595

9696
header.Append(_name.AsSpan());
97-
header.Append("=");
97+
header.Append('=');
9898
header.Append(_value.AsSpan());
9999

100100
return header.ToString();

src/Http/Headers/src/SetCookieHeaderValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ private static void Append(ref Span<char> span, ReadOnlySpan<char> other)
329329
public void AppendToStringBuilder(StringBuilder builder)
330330
{
331331
builder.Append(_name.AsSpan());
332-
builder.Append("=");
332+
builder.Append('=');
333333
builder.Append(_value.AsSpan());
334334

335335
if (Expires.HasValue)
@@ -388,7 +388,7 @@ private static void AppendSegment(StringBuilder builder, StringSegment name, Str
388388
builder.Append(name.AsSpan());
389389
if (value != null)
390390
{
391-
builder.Append("=");
391+
builder.Append('=');
392392
builder.Append(value.AsSpan());
393393
}
394394
}

src/Http/Http.Abstractions/src/QueryString.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ public override int GetHashCode()
286286

287287
private static void AppendKeyValuePair(StringBuilder builder, string key, string? value, bool first)
288288
{
289-
builder.Append(first ? "?" : "&");
289+
builder.Append(first ? '?' : '&');
290290
builder.Append(UrlEncoder.Default.Encode(key));
291-
builder.Append("=");
291+
builder.Append('=');
292292
if (!string.IsNullOrEmpty(value))
293293
{
294294
builder.Append(UrlEncoder.Default.Encode(value));

src/Http/Routing/src/DefaultLinkGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,14 @@ private static string FormatRouteValues(IReadOnlyDictionary<string, object> valu
480480

481481
foreach (var kvp in values.OrderBy(kvp => kvp.Key))
482482
{
483-
builder.Append("\"");
483+
builder.Append('"');
484484
builder.Append(kvp.Key);
485-
builder.Append("\"");
486-
builder.Append(":");
487-
builder.Append(" ");
488-
builder.Append("\"");
485+
builder.Append('"');
486+
builder.Append(':');
487+
builder.Append(' ');
488+
builder.Append('"');
489489
builder.Append(kvp.Value);
490-
builder.Append("\"");
490+
builder.Append('"');
491491
builder.Append(", ");
492492
}
493493

0 commit comments

Comments
 (0)