1
0
Fork 0
mirror of https://github.com/VSadov/Satori.git synced 2025-06-09 17:44:48 +09:00

Set severity of rule CA1870 to warning (#92135)

* Set severity of rule CA1870 to warning

* Replace one more usage in nativeaot corelib

* Set severity for tests as well

* pragma disable the rule in nativeaot's reflection impl
This commit is contained in:
Miha Zupan 2023-09-20 17:45:31 +02:00 committed by GitHub
parent 901f780b2e
commit e235aeff6c
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 9 deletions

View file

@ -489,6 +489,9 @@ dotnet_diagnostic.CA1868.severity = warning
# CA1869: Cache and reuse 'JsonSerializerOptions' instances # CA1869: Cache and reuse 'JsonSerializerOptions' instances
dotnet_diagnostic.CA1869.severity = warning dotnet_diagnostic.CA1869.severity = warning
# CA1870: Use a cached 'SearchValues' instance
dotnet_diagnostic.CA1870.severity = warning
# CA2000: Dispose objects before losing scope # CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = none dotnet_diagnostic.CA2000.severity = none

View file

@ -486,6 +486,9 @@ dotnet_diagnostic.CA1868.severity = none
# CA1869: Cache and reuse 'JsonSerializerOptions' instances # CA1869: Cache and reuse 'JsonSerializerOptions' instances
dotnet_diagnostic.CA1869.severity = none dotnet_diagnostic.CA1869.severity = none
# CA1870: Use a cached 'SearchValues' instance
dotnet_diagnostic.CA1870.severity = none
# CA2000: Dispose objects before losing scope # CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = none dotnet_diagnostic.CA2000.severity = none

View file

@ -109,7 +109,12 @@ namespace System.Reflection.Runtime.General
public static string EscapeTypeNameIdentifier(this string identifier) public static string EscapeTypeNameIdentifier(this string identifier)
{ {
// Some characters in a type name need to be escaped // Some characters in a type name need to be escaped
// We're avoiding calling into MemoryExtensions here as it has paths that lead to reflection,
// and that would lead to an infinite loop given that this is the implementation of reflection.
#pragma warning disable CA1870 // Use a cached 'SearchValues' instance
if (identifier != null && identifier.IndexOfAny(s_charsToEscape) != -1) if (identifier != null && identifier.IndexOfAny(s_charsToEscape) != -1)
#pragma warning restore CA1870
{ {
StringBuilder sbEscapedName = new StringBuilder(identifier.Length); StringBuilder sbEscapedName = new StringBuilder(identifier.Length);
foreach (char c in identifier) foreach (char c in identifier)

View file

@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
@ -18,6 +19,12 @@ namespace System.Configuration
[DebuggerDisplay("ConfigPath = {ConfigPath}")] [DebuggerDisplay("ConfigPath = {ConfigPath}")]
internal abstract class BaseConfigurationRecord : IInternalConfigRecord internal abstract class BaseConfigurationRecord : IInternalConfigRecord
{ {
#if NET8_0_OR_GREATER
private static readonly SearchValues<char> s_invalidSubPathChars = SearchValues.Create(InvalidSubPathCharactersString);
#else
private static ReadOnlySpan<char> s_invalidSubPathChars => InvalidSubPathCharactersString.AsSpan();
#endif
protected const string NewLine = "\r\n"; protected const string NewLine = "\r\n";
internal const string KeywordTrue = "true"; internal const string KeywordTrue = "true";
@ -128,7 +135,6 @@ namespace System.Configuration
// Comparer used in sorting IndirectInputs. // Comparer used in sorting IndirectInputs.
private static readonly IComparer<SectionInput> s_indirectInputsComparer = new IndirectLocationInputComparer(); private static readonly IComparer<SectionInput> s_indirectInputsComparer = new IndirectLocationInputComparer();
private static readonly char[] s_invalidSubPathCharactersArray = InvalidSubPathCharactersString.ToCharArray();
protected Hashtable _children; // configName -> record protected Hashtable _children; // configName -> record
private object _configContext; // Context for config level private object _configContext; // Context for config level
@ -3090,7 +3096,7 @@ namespace System.Configuration
throw new ConfigurationErrorsException(SR.Config_location_path_invalid_last_character, errorInfo); throw new ConfigurationErrorsException(SR.Config_location_path_invalid_last_character, errorInfo);
// combination of URI reserved characters and OS invalid filename characters, minus / (allowed reserved character) // combination of URI reserved characters and OS invalid filename characters, minus / (allowed reserved character)
if (subPath.IndexOfAny(s_invalidSubPathCharactersArray) != -1) if (subPath.AsSpan().IndexOfAny(s_invalidSubPathChars) >= 0)
throw new ConfigurationErrorsException(SR.Config_location_path_invalid_character, errorInfo); throw new ConfigurationErrorsException(SR.Config_location_path_invalid_character, errorInfo);
return subPath; return subPath;

View file

@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
@ -11,6 +12,12 @@ namespace System.Reflection.TypeLoading
{ {
internal static class Helpers internal static class Helpers
{ {
#if NET8_0_OR_GREATER
private static readonly SearchValues<char> s_charsToEscape = SearchValues.Create("\\[]+*&,");
#else
private static ReadOnlySpan<char> s_charsToEscape => "\\[]+*&,".AsSpan();
#endif
[return: NotNullIfNotNull(nameof(original))] [return: NotNullIfNotNull(nameof(original))]
public static T[]? CloneArray<T>(this T[]? original) public static T[]? CloneArray<T>(this T[]? original)
{ {
@ -96,7 +103,7 @@ namespace System.Reflection.TypeLoading
public static string EscapeTypeNameIdentifier(this string identifier) public static string EscapeTypeNameIdentifier(this string identifier)
{ {
// Some characters in a type name need to be escaped // Some characters in a type name need to be escaped
if (identifier.IndexOfAny(s_charsToEscape) != -1) if (TypeNameContainsTypeParserMetacharacters(identifier))
{ {
StringBuilder sbEscapedName = new StringBuilder(identifier.Length); StringBuilder sbEscapedName = new StringBuilder(identifier.Length);
foreach (char c in identifier) foreach (char c in identifier)
@ -113,12 +120,16 @@ namespace System.Reflection.TypeLoading
public static bool TypeNameContainsTypeParserMetacharacters(this string identifier) public static bool TypeNameContainsTypeParserMetacharacters(this string identifier)
{ {
return identifier.IndexOfAny(s_charsToEscape) != -1; return identifier.AsSpan().IndexOfAny(s_charsToEscape) >= 0;
} }
public static bool NeedsEscapingInTypeName(this char c) public static bool NeedsEscapingInTypeName(this char c)
{ {
return Array.IndexOf(s_charsToEscape, c) >= 0; #if NET8_0_OR_GREATER
return s_charsToEscape.Contains(c);
#else
return s_charsToEscape.IndexOf(c) >= 0;
#endif
} }
public static string UnescapeTypeNameIdentifier(this string identifier) public static string UnescapeTypeNameIdentifier(this string identifier)
@ -145,8 +156,6 @@ namespace System.Reflection.TypeLoading
return identifier; return identifier;
} }
private static readonly char[] s_charsToEscape = new char[] { '\\', '[', ']', '+', '*', '&', ',' };
/// <summary> /// <summary>
/// For AssemblyReferences, convert "unspecified" components from the ECMA format (0xffff) to the in-memory System.Version format (0xffffffff). /// For AssemblyReferences, convert "unspecified" components from the ECMA format (0xffff) to the in-memory System.Version format (0xffffffff).
/// </summary> /// </summary>

View file

@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Speech.Internal; using System.Speech.Internal;
@ -14,6 +15,8 @@ namespace System.Speech.Recognition.SrgsGrammar
[DebuggerTypeProxy(typeof(SrgsRuleDebugDisplay))] [DebuggerTypeProxy(typeof(SrgsRuleDebugDisplay))]
public class SrgsRule : IRule public class SrgsRule : IRule
{ {
private static readonly SearchValues<char> s_invalidChars = SearchValues.Create("?*+|()^$/;.=<>[]{}\\ \t\r\n");
#region Constructors #region Constructors
private SrgsRule() private SrgsRule()
{ {
@ -383,7 +386,7 @@ namespace System.Speech.Recognition.SrgsGrammar
XmlParser.ThrowSrgsException(SRID.ConstructorNotAllowed, _id); XmlParser.ThrowSrgsException(SRID.ConstructorNotAllowed, _id);
} }
if (s != null && (s.IndexOfAny(s_invalidChars) >= 0 || s.Length == 0)) if (s != null && (s.Length == 0 || s.AsSpan().ContainsAny(s_invalidChars)))
{ {
XmlParser.ThrowSrgsException(SRID.InvalidMethodName); XmlParser.ThrowSrgsException(SRID.InvalidMethodName);
} }
@ -416,7 +419,6 @@ namespace System.Speech.Recognition.SrgsGrammar
private string _onError; private string _onError;
private string _onRecognition; private string _onRecognition;
private static readonly char[] s_invalidChars = new char[] { '?', '*', '+', '|', '(', ')', '^', '$', '/', ';', '.', '=', '<', '>', '[', ']', '{', '}', '\\', ' ', '\t', '\r', '\n' };
#endregion #endregion