Skip to content

Commit

Permalink
Enable CA1821, CA1825, CA1826
Browse files Browse the repository at this point in the history
Contributes to #24055
  • Loading branch information
pranavkm committed May 26, 2021
1 parent e7b5aa6 commit 1315a9f
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 31 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,18 @@ dotnet_diagnostic.CA1802.severity = warning
# CA1805: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = warning

# CA1823: Remove empty Finalizers
dotnet_diagnostic.CA1821.severity = warning

# CA1823: Avoid unused private fields
dotnet_diagnostic.CA1823.severity = warning

# CA1823: Avoid zero-length array allocations
dotnet_diagnostic.CA1825.severity = warning

# CA1826: Do not use Enumerable methods on indexable collections. Instead use the collection directly
dotnet_diagnostic.CA1826.severity = warning

# CA2012: Use ValueTask correctly
dotnet_diagnostic.CA2012.severity = warning

Expand All @@ -110,5 +119,9 @@ dotnet_diagnostic.CA1507.severity = suggestion
dotnet_diagnostic.CA1802.severity = suggestion
# CA1805: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = suggestion
# CA1823: Avoid zero-length array allocations
dotnet_diagnostic.CA1825.severity = suggestion
# CA1826: Do not use Enumerable methods on indexable collections. Instead use the collection directly
dotnet_diagnostic.CA1826.severity = suggestion
# CA2012: Use ValueTask correctly
dotnet_diagnostic.CA2012.severity = suggestion
2 changes: 1 addition & 1 deletion src/Identity/Extensions.Core/src/Base32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static byte[] FromBase32(string input)
input = input.TrimEnd('=').ToUpperInvariant();
if (input.Length == 0)
{
return new byte[0];
return Array.Empty<byte>();
}

var output = new byte[input.Length * 5 / 8];
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/Session/src/NoOpSessionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SetValue(EncodedKey key, byte[] value)

public ICollection<EncodedKey> Keys { get; } = Array.Empty<EncodedKey>();

public ICollection<byte[]> Values { get; } = new byte[0][];
public ICollection<byte[]> Values { get; } = Array.Empty<byte[]>();

public void Clear() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <summary>
/// A <see cref="ValueProviderResult"/> that represents a lack of data.
/// </summary>
public static ValueProviderResult None = new ValueProviderResult(new string[0]);
public static ValueProviderResult None = new ValueProviderResult(Array.Empty<string>());

/// <summary>
/// Creates a new <see cref="ValueProviderResult"/> using <see cref="CultureInfo.InvariantCulture"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ private string[] GetMethodParameters(TagHelperDescriptor tagHelper)

private void WriteTargetElementString(CodeWriter writer, TagHelperDescriptor tagHelper)
{
Debug.Assert(tagHelper.TagMatchingRules.Count() == 1);
Debug.Assert(tagHelper.TagMatchingRules.Count == 1);

var rule = tagHelper.TagMatchingRules.First();
var rule = tagHelper.TagMatchingRules[0];

writer.Write("[")
.WriteStartMethodInvocation(HtmlTargetElementAttributeTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ private string[] GetMethodParameters(TagHelperDescriptor tagHelper)

private void WriteTargetElementString(CodeWriter writer, TagHelperDescriptor tagHelper)
{
Debug.Assert(tagHelper.TagMatchingRules.Count() == 1);
Debug.Assert(tagHelper.TagMatchingRules.Count == 1);

var rule = tagHelper.TagMatchingRules.First();
var rule = tagHelper.TagMatchingRules[0];

writer.Write("[")
.WriteStartMethodInvocation(HtmlTargetElementAttributeTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ private string[] GetMethodParameters(TagHelperDescriptor tagHelper)

private void WriteTargetElementString(CodeWriter writer, TagHelperDescriptor tagHelper)
{
Debug.Assert(tagHelper.TagMatchingRules.Count() == 1);
Debug.Assert(tagHelper.TagMatchingRules.Count == 1);

var rule = tagHelper.TagMatchingRules.First();
var rule = tagHelper.TagMatchingRules[0];

writer.Write("[")
.WriteStartMethodInvocation(HtmlTargetElementAttributeTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,8 @@ private void RewriteNodesForElementEventCallbackBind(

private static IntermediateToken GetAttributeContent(IntermediateNode node)
{
var template = node.FindDescendantNodes<TemplateIntermediateNode>().FirstOrDefault();
var nodes = node.FindDescendantNodes<TemplateIntermediateNode>();
var template = nodes.Count > 0 ? nodes[0] : default;
if (template != null)
{
// See comments in TemplateDiagnosticPass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ private IntermediateNode RewriteUsage(IntermediateNode parent, TagHelperDirectiv

private static IReadOnlyList<IntermediateToken> GetAttributeContent(IntermediateNode node)
{
var template = node.FindDescendantNodes<TemplateIntermediateNode>().FirstOrDefault();
var nodes = node.FindDescendantNodes<TemplateIntermediateNode>();
var template = nodes.Count == 0 ? nodes[0] : default;
if (template != null)
{
// See comments in TemplateDiagnosticPass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public override void VisitCSharpStatementLiteral(CSharpStatementLiteralSyntax no
_builder.Add(new DirectiveTokenIntermediateNode()
{
Content = addTagHelperChunkGenerator.LookupText,
DirectiveToken = CSharpCodeParser.AddTagHelperDirectiveDescriptor.Tokens.First(),
DirectiveToken = CSharpCodeParser.AddTagHelperDirectiveDescriptor.Tokens[0],
Source = BuildSourceSpanFromNode(node),
});

Expand Down Expand Up @@ -385,7 +385,7 @@ public override void VisitCSharpStatementLiteral(CSharpStatementLiteralSyntax no
_builder.Add(new DirectiveTokenIntermediateNode()
{
Content = removeTagHelperChunkGenerator.LookupText,
DirectiveToken = CSharpCodeParser.RemoveTagHelperDirectiveDescriptor.Tokens.First(),
DirectiveToken = CSharpCodeParser.RemoveTagHelperDirectiveDescriptor.Tokens[0],
Source = BuildSourceSpanFromNode(node),
});

Expand Down Expand Up @@ -423,7 +423,7 @@ public override void VisitCSharpStatementLiteral(CSharpStatementLiteralSyntax no
_builder.Add(new DirectiveTokenIntermediateNode()
{
Content = tagHelperPrefixChunkGenerator.Prefix,
DirectiveToken = CSharpCodeParser.TagHelperPrefixDirectiveDescriptor.Tokens.First(),
DirectiveToken = CSharpCodeParser.TagHelperPrefixDirectiveDescriptor.Tokens[0],
Source = BuildSourceSpanFromNode(node),
});

Expand Down Expand Up @@ -1019,7 +1019,8 @@ private void VisitAttributeValue(SyntaxNode node)

IReadOnlyList<SyntaxNode> children = node.ChildNodes();
var position = node.Position;
if (children.FirstOrDefault() is MarkupBlockSyntax markupBlock &&
if (children.Count > 0 &&
children[0] is MarkupBlockSyntax markupBlock &&
markupBlock.Children.Count == 2 &&
markupBlock.Children[0] is MarkupTextLiteralSyntax &&
markupBlock.Children[1] is MarkupEphemeralTextLiteralSyntax)
Expand Down Expand Up @@ -1970,7 +1971,8 @@ private void VisitAttributeValue(SyntaxNode node)

IReadOnlyList<SyntaxNode> children = node.ChildNodes();
var position = node.Position;
if (children.FirstOrDefault() is MarkupBlockSyntax markupBlock &&
if (children.Count > 0 &&
children[0] is MarkupBlockSyntax markupBlock &&
markupBlock.Children.Count == 2 &&
markupBlock.Children[0] is MarkupTextLiteralSyntax &&
markupBlock.Children[1] is MarkupEphemeralTextLiteralSyntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ internal class CSharpCodeParser : TokenizerBackedParser<CSharpTokenizer>
builder.Description = Resources.TagHelperPrefixDirective_Description;
});

internal static readonly IEnumerable<DirectiveDescriptor> DefaultDirectiveDescriptors = new DirectiveDescriptor[]
{
};

internal static ISet<string> DefaultKeywords = new HashSet<string>()
{
SyntaxConstants.CSharp.TagHelperPrefixKeyword,
Expand Down Expand Up @@ -912,11 +908,8 @@ protected bool TryParseDirective(in SyntaxListBuilder<RazorSyntaxNode> builder,

private void SetupDirectiveParsers(IEnumerable<DirectiveDescriptor> directiveDescriptors)
{
var allDirectives = directiveDescriptors.Concat(DefaultDirectiveDescriptors).ToList();

for (var i = 0; i < allDirectives.Count; i++)
foreach (var directiveDescriptor in directiveDescriptors)
{
var directiveDescriptor = allDirectives[i];
CurrentKeywords.Add(directiveDescriptor.Directive);
MapDirectives((builder, transition) => ParseExtensibleDirective(builder, transition, directiveDescriptor), directiveDescriptor.Directive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class RazorDiagnostic : IEquatable<RazorDiagnostic>, IFormattable
{
internal static readonly RazorDiagnostic[] EmptyArray = new RazorDiagnostic[0];
internal static readonly object[] EmptyArgs = new object[0];
internal static readonly RazorDiagnostic[] EmptyArray = Array.Empty<RazorDiagnostic>();
internal static readonly object[] EmptyArgs = Array.Empty<object>();

public abstract string Id { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class RazorSourceDocument
{
internal const int LargeObjectHeapLimitInChars = 40 * 1024; // 40K Unicode chars is 80KB which is less than the large object heap limit.

internal static readonly RazorSourceDocument[] EmptyArray = new RazorSourceDocument[0];
internal static readonly RazorSourceDocument[] EmptyArray = Array.Empty<RazorSourceDocument>();

/// <summary>
/// Gets the encoding of the text in the original source document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private TagHelperDescriptor CreateChildContentDescriptor(ComponentSymbols symbol
builder.TagMatchingRule(r =>
{
r.TagName = attribute.Name;
r.ParentTag = component.TagMatchingRules.First().TagName;
r.ParentTag = component.TagMatchingRules[0].TagName;
});

if (attribute.IsParameterizedChildContentProperty())
Expand Down
2 changes: 1 addition & 1 deletion src/Servers/HttpSys/src/RequestProcessing/ResponseBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private List<GCHandle> PinDataBuffers(bool endOfRequest, ArraySegment<byte> data
else if (!hasData && !addTrailers)
{
// No data
dataChunks = new HttpApiTypes.HTTP_DATA_CHUNK[0];
dataChunks = Array.Empty<HttpApiTypes.HTTP_DATA_CHUNK>();
return pins;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Http2cat/Http2Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static IEnumerable<KeyValuePair<string, string>> ReadRateRequestHeaders(i
public static readonly byte[] _helloBytes = Encoding.ASCII.GetBytes("hello");
public static readonly byte[] _worldBytes = Encoding.ASCII.GetBytes("world");
public static readonly byte[] _helloWorldBytes = Encoding.ASCII.GetBytes("hello, world");
public static readonly byte[] _noData = new byte[0];
public static readonly byte[] _noData = Array.Empty<byte>();
public static readonly byte[] _maxData = Encoding.ASCII.GetBytes(new string('a', Http2PeerSettings.MinAllowedMaxFrameSize));

internal readonly Http2PeerSettings _clientSettings = new Http2PeerSettings();
Expand Down
3 changes: 2 additions & 1 deletion src/Shared/HttpSys/RequestProcessing/HeaderParser.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.HttpSys.Internal
{
internal static class HeaderParser
{
internal static IEnumerable<string> Empty = new string[0];
internal static IEnumerable<string> Empty = Array.Empty<string>();

// Split on commas, except in quotes
internal static IEnumerable<string> SplitValues(StringValues values)
Expand Down

0 comments on commit 1315a9f

Please sign in to comment.