Skip to content

Commit

Permalink
Update CodeAnalysis config with recent rules
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Mar 25, 2024
1 parent 0880c28 commit e9f454c
Show file tree
Hide file tree
Showing 25 changed files with 115 additions and 28 deletions.
49 changes: 44 additions & 5 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ dotnet_diagnostic.CA1863.severity = suggestion
# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
dotnet_diagnostic.CA1864.severity = warning

# CA1865: Use char overload
dotnet_diagnostic.CA1865.severity = warning

# CA1866: Use char overload
dotnet_diagnostic.CA1866.severity = warning

# CA1867: Use char overload
dotnet_diagnostic.CA1867.severity = warning

# CA1868: Unnecessary call to 'Contains' for sets
dotnet_diagnostic.CA1868.severity = warning

Expand All @@ -501,6 +510,9 @@ dotnet_diagnostic.CA1870.severity = warning
# CA1871: Do not pass a nullable struct to 'ArgumentNullException.ThrowIfNull'
dotnet_diagnostic.CA1871.severity = warning

# CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'
dotnet_diagnostic.CA1872.severity = warning

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = none

Expand Down Expand Up @@ -610,9 +622,6 @@ dotnet_diagnostic.CA2226.severity = none
# CA2227: Collection properties should be read only
dotnet_diagnostic.CA2227.severity = none

# CA2229: Implement serialization constructors
dotnet_diagnostic.CA2229.severity = warning

# CA2231: Overload operator equals on overriding value type Equals
dotnet_diagnostic.CA2231.severity = none

Expand Down Expand Up @@ -692,7 +701,7 @@ dotnet_diagnostic.CA2261.severity = warning
dotnet_diagnostic.CA2262.severity = warning

# CA2263: Prefer generic overload when type is known
dotnet_diagnostic.CA2263.severity = info
dotnet_diagnostic.CA2263.severity = suggestion

# CA2264: Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'
dotnet_diagnostic.CA2264.severity = warning
Expand Down Expand Up @@ -1827,7 +1836,7 @@ dotnet_diagnostic.IDE0200.severity = warning
# IDE0210: Use top-level statements
dotnet_diagnostic.IDE0210.severity = none

# IDE0211: Use program main
# IDE0211: Convert to 'Program.Main' style program
dotnet_diagnostic.IDE0211.severity = none

# IDE0220: foreach cast
Expand All @@ -1845,6 +1854,9 @@ dotnet_diagnostic.IDE0241.severity = suggestion
# IDE0250: Make struct readonly
dotnet_diagnostic.IDE0250.severity = suggestion

# IDE0251: Make member readonly
dotnet_diagnostic.IDE0251.severity = suggestion

# IDE0260: Use pattern matching
dotnet_diagnostic.IDE0260.severity = suggestion

Expand All @@ -1854,6 +1866,27 @@ dotnet_diagnostic.IDE0270.severity = suggestion
# IDE0280: Use 'nameof'
dotnet_diagnostic.IDE0280.severity = warning

# IDE0290: Use primary constructor
dotnet_diagnostic.IDE0290.severity = suggestion

# IDE0300: Use collection expression for array
dotnet_diagnostic.IDE0300.severity = suggestion

# IDE0301: Use collection expression for empty
dotnet_diagnostic.IDE0301.severity = suggestion

# IDE0302: Use collection expression for stackalloc
dotnet_diagnostic.IDE0302.severity = suggestion

# IDE0303: Use collection expression for Create()
dotnet_diagnostic.IDE0303.severity = suggestion

# IDE0304: Use collection expression for builder
dotnet_diagnostic.IDE0304.severity = suggestion

# IDE0305: Use collection expression for fluent
dotnet_diagnostic.IDE0305.severity = suggestion

# IDE1005: Delegate invocation can be simplified.
dotnet_diagnostic.IDE1005.severity = warning

Expand All @@ -1874,3 +1907,9 @@ dotnet_diagnostic.IDE2003.severity = silent

# IDE2004: Blank line not allowed after constructor initializer colon
dotnet_diagnostic.IDE2004.severity = silent

# IDE2005: Blank line not allowed after conditional expression token
dotnet_diagnostic.IDE2005.severity = silent

# IDE2006: Blank line not allowed after arrow expression clause token
dotnet_diagnostic.IDE2006.severity = silent
44 changes: 43 additions & 1 deletion eng/CodeAnalysis.test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,15 @@ dotnet_diagnostic.CA1863.severity = none
# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
dotnet_diagnostic.CA1864.severity = none

# CA1865: Use char overload
dotnet_diagnostic.CA1865.severity = none

# CA1866: Use char overload
dotnet_diagnostic.CA1866.severity = none

# CA1867: Use char overload
dotnet_diagnostic.CA1867.severity = none

# CA1868: Unnecessary call to 'Contains' for sets
dotnet_diagnostic.CA1868.severity = none

Expand All @@ -498,6 +507,9 @@ dotnet_diagnostic.CA1870.severity = none
# CA1871: Do not pass a nullable struct to 'ArgumentNullException.ThrowIfNull'
dotnet_diagnostic.CA1871.severity = none

# CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'
dotnet_diagnostic.CA1872.severity = none

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = none

Expand Down Expand Up @@ -1821,7 +1833,7 @@ dotnet_diagnostic.IDE0200.severity = silent
# IDE0210: Use top-level statements
dotnet_diagnostic.IDE0210.severity = silent

# IDE0211: Use program main
# IDE0211: Convert to 'Program.Main' style program
dotnet_diagnostic.IDE0211.severity = silent

# IDE0220: foreach cast
Expand All @@ -1839,6 +1851,9 @@ dotnet_diagnostic.IDE0241.severity = silent
# IDE0250: Make struct readonly
dotnet_diagnostic.IDE0250.severity = silent

# IDE0251: Make member readonly
dotnet_diagnostic.IDE0251.severity = silent

# IDE0260: Use pattern matching
dotnet_diagnostic.IDE0260.severity = silent

Expand All @@ -1848,6 +1863,27 @@ dotnet_diagnostic.IDE0270.severity = silent
# IDE0280: Use 'nameof'
dotnet_diagnostic.IDE0280.severity = silent

# IDE0290: Use primary constructor
dotnet_diagnostic.IDE0290.severity = silent

# IDE0300: Use collection expression for array
dotnet_diagnostic.IDE0300.severity = silent

# IDE0301: Use collection expression for empty
dotnet_diagnostic.IDE0301.severity = silent

# IDE0302: Use collection expression for stackalloc
dotnet_diagnostic.IDE0302.severity = silent

# IDE0303: Use collection expression for Create()
dotnet_diagnostic.IDE0303.severity = silent

# IDE0304: Use collection expression for builder
dotnet_diagnostic.IDE0304.severity = silent

# IDE0305: Use collection expression for fluent
dotnet_diagnostic.IDE0305.severity = silent

# IDE1005: Delegate invocation can be simplified.
dotnet_diagnostic.IDE1005.severity = silent

Expand All @@ -1869,6 +1905,12 @@ dotnet_diagnostic.IDE2003.severity = silent
# IDE2004: Blank line not allowed after constructor initializer colon
dotnet_diagnostic.IDE2004.severity = silent

# IDE2005: Blank line not allowed after conditional expression token
dotnet_diagnostic.IDE2005.severity = silent

# IDE2006: Blank line not allowed after arrow expression clause token
dotnet_diagnostic.IDE2006.severity = silent

# xUnit1000: Test classes must be public
dotnet_diagnostic.xUnit1000.severity = warning

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private string SanitizeNameWithHash(string literal)
hash = SHA256.HashData(GetBytesFromString(literal));
}

mangledName += "_" + BitConverter.ToString(hash).Replace("-", "");
mangledName += "_" + Convert.ToHexString(hash);
}

return mangledName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private protected override void CreateSection(ObjectNodeSection section, string
Name =
section == ObjectNodeSection.TLSSection ? ".tls$" :
section == ObjectNodeSection.HydrationTargetSection ? "hydrated" :
(section.Name.StartsWith(".") ? section.Name : "." + section.Name),
(section.Name.StartsWith('.') ? section.Name : "." + section.Name),
SectionCharacteristics = section.Type switch
{
SectionType.ReadOnly =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
int valueIndex = 0;
foreach (string line in _runtimeOptions)
{
int indexOfEquals = line.IndexOf("=");
int indexOfEquals = line.IndexOf('=');
if (indexOfEquals > 0)
{
string key = line.Substring(0, indexOfEquals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override void DumpObjectNode(NodeFactory nodeFactory, ObjectNode node,

private string HashData(byte[] data)
{
return BitConverter.ToString(_sha256.ComputeHash(data)).Replace("-", "").ToLowerInvariant();
return Convert.ToHexStringLower(_sha256.ComputeHash(data));
}

internal override void End()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<AssemblyName>ILCompiler.Compiler</AssemblyName>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CA1866;CA1867</NoWarn>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Platforms>x64;x86</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void Write(Stream stream)

// 3rd, the name, Quote the string if not already quoted
string asString = rec.ToString(false);
bool alreadyQuoted = asString.StartsWith("\"") && asString.EndsWith("\"");
bool alreadyQuoted = asString.StartsWith('\"') && asString.EndsWith('\"');
if (!alreadyQuoted)
{
LogWriter.Write("\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AssemblyName>ILCompiler.TypeSystem</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<NoWarn>$(NoWarn);CA1866</NoWarn>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Platforms>x64;x86</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<NoWarn>$(NoWarn);CA1866</NoWarn>
<EnableDefaultItems>true</EnableDefaultItems>
<IsPackable>true</IsPackable>
<PackageDescription>Command line configuration provider implementation for Microsoft.Extensions.Configuration. This package enables you to read configuration parameters from the command line arguments of your application. You can use CommandLineConfigurationExtensions.AddCommandLine extension method on IConfigurationBuilder to add the command line configuration provider to the configuration builder.</PackageDescription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<RootNamespace>Microsoft.Extensions.FileProviders</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CA1865;CA1866</NoWarn>
<EnableDefaultItems>true</EnableDefaultItems>
<IsPackable>true</IsPackable>
<PackageDescription>File provider for physical files for Microsoft.Extensions.FileProviders.</PackageDescription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks Condition="'$(NetCoreAppPrevious)' != ''">$(TargetFrameworks);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
<NoWarn>$(NoWarn);IDE0059;IDE0060;CA1822</NoWarn>
<NoWarn>$(NoWarn);IDE0059;IDE0060;CA1822;CA1865</NoWarn>
<EnableAOTAnalyzer>false</EnableAOTAnalyzer>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks Condition="'$(NetCoreAppPrevious)' != ''">$(TargetFrameworks);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);0618</NoWarn>
<NoWarn>$(NoWarn);IDE0059;IDE0060;CA1822</NoWarn>
<NoWarn>$(NoWarn);IDE0059;IDE0060;CA1822;CA1865</NoWarn>
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<EnableAOTAnalyzer>false</EnableAOTAnalyzer>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
<NoWarn>$(NoWarn);CA1865</NoWarn>
<PackageDescription>Provides read-only reflection on assemblies in an isolated context with support for assemblies that target different processor architectures and runtimes. Using MetadataLoadContext enables you to inspect assemblies without loading them into the main execution context. Assemblies in MetadataLoadContext are treated only as metadata, that is, you can read information about their members, but cannot execute any code contained in them.</PackageDescription>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks Condition="'$(NetCoreAppPrevious)' != ''">$(TargetFrameworks);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CA2249</NoWarn>
<NoWarn>$(NoWarn);CA2249;CA1865</NoWarn>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
<PackageDescription>Provides the System.ServiceProcess.ServiceController class, which allows you to connect to a Windows service, manipulate it, or get information about it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static bool IsRangeDefinition(string rawName, out string rangeName, out
// Ranges are represented within angle brackets, such as the following:
// DC00;<Low Surrogate, First>;Cs;0;L;;;;;N;;;;;
// DFFF;<Low Surrogate, Last>;Cs;0;L;;;;;N;;;;;
if (rawName.StartsWith("<", StringComparison.Ordinal))
if (rawName.StartsWith('<'))
{
if (rawName.EndsWith(", First>", StringComparison.Ordinal))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public void MinMaxLengthIsCorrect(string pattern, int options, int expectedMin,

Assert.Equal(expectedMin, tree.FindOptimizations.MinRequiredLength);

if (!pattern.EndsWith("$", StringComparison.Ordinal) &&
if (!pattern.EndsWith('$') &&
!pattern.EndsWith(@"\Z", StringComparison.OrdinalIgnoreCase))
{
// MaxPossibleLength is currently only computed/stored if there's a trailing End{Z} anchor as the max length is otherwise unused
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/debugger/BrowserDebugProxy/DebugStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ private void GetSourceLinkUrl(string document, Dictionary<string, string> source
{
string key = sourceLinkDocument.Key;

if (!key.EndsWith("*", StringComparison.OrdinalIgnoreCase))
if (!key.EndsWith('*'))
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mono/browser/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ public async Task<string> GetAssemblyFileNameFromId(int assemblyId, Cancellation

using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdAssembly.GetName, commandParamsWriter, token);
var name = retDebuggerCmdReader.ReadString();
return name.Remove(name.IndexOf(",")) + ".dll";
return name.Remove(name.IndexOf(',')) + ".dll";
}

public async Task<string> GetMethodName(int methodId, CancellationToken token)
Expand Down Expand Up @@ -2185,7 +2185,7 @@ public async Task<JArray> GetHoistedLocalVariables(MethodInfoWithDebugInformatio
asyncLocal["name"] = match.Groups["varName"].Value;
}
}
else if (fieldName.StartsWith("$"))
else if (fieldName.StartsWith('$'))
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/host/WasmAppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>$(AspNetCoreAppCurrent)</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<NoWarn>$(NoWarn),CA2007</NoWarn>
<NoWarn>$(NoWarn),CA1866;CA2007</NoWarn>
<Nullable>enable</Nullable>
<UseAppHost>false</UseAppHost>
<RollForward>LatestMajor</RollForward>
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public ApkBuilder(TaskLoggingHelper logger)
// also, aapt is not happy about zip files
return false;
}
if (fileName.StartsWith("."))
if (fileName.StartsWith('.'))
{
// aapt complains on such files
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppToolCurrent);$(NetFrameworkToolCurrent)</TargetFrameworks>
<NoWarn>$(NoWarn),CA1050,CA1850,CA1845,CA1859,NU5128</NoWarn>
<NoWarn>$(NoWarn),CA1050,CA1850,CA1845,CA1859;CA1866,NU5128</NoWarn>
<RootNamespace>Microsoft.NET.Sdk.WebAssembly</RootNamespace>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
Loading

0 comments on commit e9f454c

Please sign in to comment.