Skip to content

Commit

Permalink
Enable more analyzers (dotnet/linker#1862)
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/linker@59eef1f
  • Loading branch information
marek-safar committed Mar 1, 2021
1 parent b486dc6 commit d4c06d0
Show file tree
Hide file tree
Showing 48 changed files with 82 additions and 172 deletions.
8 changes: 8 additions & 0 deletions src/tools/illink/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,21 @@ dotnet_diagnostic.IDE0082.severity = warning
dotnet_analyzer_diagnostic.category-performance.severity = warning
dotnet_analyzer_diagnostic.category-maintainability.severity = warning
dotnet_analyzer_diagnostic.category-reliability.severity = warning
dotnet_analyzer_diagnostic.category-usage.severity = warning
#dotnet_analyzer_diagnostic.category-style.severity = warning

# call GC.SuppressFinalize(object)
dotnet_diagnostic.CA1816.severity = none

# CA1834: Use 'StringBuilder.Append(char)'
dotnet_diagnostic.CA1834.severity = none

# RS2008 Ignore analyzer release tracking
dotnet_diagnostic.RS2008.severity = none

# Exception type is not sufficiently specific
dotnet_diagnostic.CA2201.severity = none

[external**]
dotnet_analyzer_diagnostic.severity = none
generated_code = true
2 changes: 1 addition & 1 deletion src/tools/illink/lint.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
powershell -ExecutionPolicy ByPass -NoProfile -command "Set-Location %~dp0; & """%~dp0eng\dotnet.ps1""" ""tool restore"""
powershell -ExecutionPolicy ByPass -NoProfile -command "Set-Location %~dp0; & """%~dp0eng\dotnet.ps1""" ""tool run dotnet-format . -f --verbosity diagnostic --exclude src/analyzer src/tuner external %*"""
powershell -ExecutionPolicy ByPass -NoProfile -command "Set-Location %~dp0; & """%~dp0eng\dotnet.ps1""" ""tool run dotnet-format illink.sln --verbosity diagnostic --fix-whitespace --exclude src/analyzer src/tuner external %*"""
2 changes: 1 addition & 1 deletion src/tools/illink/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ done

scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
"$scriptroot/eng/dotnet.sh" tool restore
"$scriptroot/eng/dotnet.sh" tool run dotnet-format . -f --verbosity diagnostic --exclude src/analyzer src/tuner external $@
"$scriptroot/eng/dotnet.sh" tool run dotnet-format illink.sln --verbosity diagnostic --fix-whitespace --exclude src/analyzer src/tuner external $@
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FlowAnalysis;
using Microsoft.CodeAnalysis.Operations;

namespace ILLink.RoslynAnalyzer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3138,7 +3138,7 @@ protected virtual void MarkInstruction (Instruction instruction, MethodDefinitio
Code.Newobj => DependencyKind.Newobj,
Code.Ldvirtftn => DependencyKind.Ldvirtftn,
Code.Ldftn => DependencyKind.Ldftn,
_ => throw new Exception ($"unexpected opcode {instruction.OpCode}")
_ => throw new InvalidOperationException ($"unexpected opcode {instruction.OpCode}")
};
requiresReflectionMethodBodyScanner |=
ReflectionMethodBodyScanner.RequiresReflectionMethodBodyScannerForCallSite (_context, (MethodReference) instruction.Operand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Diagnostics;
using System.Text;
using System.Xml.XPath;

using Mono.Cecil;

namespace Mono.Linker.Steps
{
public class ResolveFromXmlStep : ProcessLinkerXmlStepBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using Mono.Cecil;

namespace Mono.Linker.Steps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public virtual AssemblyDefinition Resolve (AssemblyNameReference name, ReaderPar
throw new AssemblyResolutionException (name, new FileNotFoundException ($"Unable to find '{name.Name}.dll' or '{name.Name}.exe' file"));
}

public static string[] Extensions = new[] { ".dll", ".exe" };
protected static readonly string[] Extensions = new[] { ".dll", ".exe" };

AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Text;
using Mono.Cecil;

Expand Down
1 change: 0 additions & 1 deletion src/tools/illink/src/linker/Linker/DynamicDependency.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Mono.Cecil;

Expand Down
1 change: 0 additions & 1 deletion src/tools/illink/src/linker/Linker/LinkContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;
Expand Down
1 change: 0 additions & 1 deletion src/tools/illink/src/linker/Linker/MarkingHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Mono.Cecil;

namespace Mono.Linker
Expand Down
4 changes: 1 addition & 3 deletions src/tools/illink/src/linker/Linker/SuppressMessageInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Mono.Cecil;

namespace Mono.Linker
namespace Mono.Linker
{
public struct SuppressMessageInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
using System.IO.Compression;
using System.Xml;
using Mono.Cecil;
using Mono.Linker.Steps;

namespace Mono.Linker
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static async Task VerifyAnalyzerAsync (string src, (string, string)[]? an
VerifyDiagnosticResults (diags, analyzers, expected, DefaultVerifier);
}

private static IVerifier DefaultVerifier = new DefaultVerifier ();
private static readonly IVerifier DefaultVerifier = new DefaultVerifier ();

/// <summary>
/// Gets the default full name of the first source file added for a test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void TestAssemblyPaths (ITaskItem[] assemblyPaths)
continue;

AssemblyAction expectedAction = (AssemblyAction) Enum.Parse (typeof (AssemblyAction), trimMode, ignoreCase: true);
AssemblyAction actualAction = (AssemblyAction) context.Actions[Path.GetFileNameWithoutExtension (assemblyPath)];
AssemblyAction actualAction = context.Actions[Path.GetFileNameWithoutExtension (assemblyPath)];

Assert.Equal (expectedAction, actualAction);
}
Expand Down
9 changes: 4 additions & 5 deletions src/tools/illink/test/ILLink.Tasks.Tests/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Build.Framework;
using Mono.Linker;
using Mono.Linker.Steps;
using Xunit;

namespace ILLink.Tasks.Tests
{
Expand Down Expand Up @@ -107,7 +106,7 @@ public IEnumerable<string> GetRootAssemblies ()
if (!(step is RootAssemblyInput))
continue;

var assemblyName = (string) (typeof (RootAssemblyInput).GetField ("fileName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (step));
var assemblyName = (string) typeof (RootAssemblyInput).GetField ("fileName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (step);
if (assemblyName == null)
continue;

Expand All @@ -121,15 +120,15 @@ public IEnumerable<string> GetRootDescriptors ()
if (!(step is ResolveFromXmlStep))
continue;

var descriptor = (string) (typeof (ResolveFromXmlStep).GetField ("_xmlDocumentLocation", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (step));
var descriptor = (string) typeof (ResolveFromXmlStep).GetField ("_xmlDocumentLocation", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (step);

yield return descriptor;
}
}

public IEnumerable<string> GetReferenceAssemblies ()
{
return (IEnumerable<string>) (typeof (AssemblyResolver).GetField ("_references", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (context.Resolver));
return (IEnumerable<string>) typeof (AssemblyResolver).GetField ("_references", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (context.Resolver);
}

protected override void AddResolveFromXmlStep (Pipeline pipeline, string file)
Expand All @@ -146,7 +145,7 @@ protected override void AddXmlDependencyRecorder (LinkContext context, string fi

public IEnumerable<IDependencyRecorder> GetDependencyRecorders ()
{
return (IEnumerable<IDependencyRecorder>) (typeof (Tracer).GetField ("recorders", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (context.Tracer));
return (IEnumerable<IDependencyRecorder>) typeof (Tracer).GetField ("recorders", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (context.Tracer);
}

public new bool GetOptimizationName (string optimization, out CodeOptimizations codeOptimizations)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
/// Asserts that the given documentation signature string resolves to the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
/// Asserts that the member to which this attribute is applied has the given
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
/// Asserts that the given documentation signature string resolves to the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
/// Asserts that the given documentation signature string does not resolve
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Metadata
namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class SkipUnresolvedAttribute : BaseMetadataAttribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Metadata
namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class StripDescriptorsAttribute : BaseMetadataAttribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Metadata
namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class StripLinkAttributesAttribute : BaseMetadataAttribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Metadata
namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class StripSubstitutionsAttribute : BaseMetadataAttribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace System.Runtime.CompilerServices
{
// This attribute is normally implemented in CoreLib as internal, but in order to test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Mono.Linker.Tests.Cases.CoreLink
{
#if NETCOREAPP
[IgnoreTestCase("Not important for .NET Core build")]
[IgnoreTestCase ("Not important for .NET Core build")]
#endif
[SetupLinkerTrimMode ("link")]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Mono.Linker.Tests.Cases.CoreLink
{
#if NETCOREAPP
[IgnoreTestCase("Not important for .NET Core build")]
[IgnoreTestCase ("Not important for .NET Core build")]
#endif
[SetupLinkerTrimMode ("link")]
[Reference ("System.dll")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Mono.Linker.Tests.Cases.CoreLink
{
#if NETCOREAPP
[IgnoreTestCase("Not important for .NET Core build")]
[IgnoreTestCase ("Not important for .NET Core build")]
#endif
[SetupLinkerTrimMode ("link")]
[SetupLinkerArgument ("--strip-security", "true")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Mono.Linker.Tests.Cases.References
// We library should be gone. The `using` statement leaves no traces in the IL so nothing in `library` will be marked
[RemovedAssembly ("library.dll")]
#if NETCOREAPP
[KeptReferencesInAssembly ("copied.dll", new [] {"System.Private.CoreLib"})]
[KeptReferencesInAssembly ("copied.dll", new[] { "System.Private.CoreLib" })]
#else
[KeptReferencesInAssembly ("copied.dll", new[] { "mscorlib" })]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Mono.Linker.Tests.Cases.References
// We library should be gone. The `using` statement leaves no traces in the IL so nothing in `library` will be marked
[RemovedAssembly ("library.dll")]
#if NETCOREAPP
[KeptReferencesInAssembly ("copied.dll", new [] {"System.Private.CoreLib"})]
[KeptReferencesInAssembly ("copied.dll", new[] { "System.Private.CoreLib" })]
#else
[KeptReferencesInAssembly ("copied.dll", new[] { "mscorlib" })]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mono.Linker.Tests.Cases.References
{
#if NETCOREAPP
[IgnoreTestCase("Asserts are specific to .NET Framework")]
[IgnoreTestCase ("Asserts are specific to .NET Framework")]
#endif
[SetupLinkerTrimMode ("link")]
// Il8n & the blacklist step pollute the results with extra stuff that didn't need to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,29 @@ static void TestNameBindingFlagsCallingConventionParameterModifier ()
#if NETCOREAPP
[Kept]
[RecognizedReflectionAccessPattern (
typeof (Type), nameof (Type.GetMethod), new Type [] { typeof (string), typeof (int), typeof (Type []) },
typeof (TestNameWithIntAndTypeClass), nameof (TestNameWithIntAndTypeClass.OnlyCalledViaReflection), new Type[] { typeof (int), typeof (int) } )]
typeof (Type), nameof (Type.GetMethod), new Type[] { typeof (string), typeof (int), typeof (Type[]) },
typeof (TestNameWithIntAndTypeClass), nameof (TestNameWithIntAndTypeClass.OnlyCalledViaReflection), new Type[] { typeof (int), typeof (int) })]
static void TestNameWithIntAndType ()
{
var method = typeof (TestNameWithIntAndTypeClass).GetMethod ("OnlyCalledViaReflection", 1, new Type [] { typeof (int) });
method.Invoke (null, new object [] { });
var method = typeof (TestNameWithIntAndTypeClass).GetMethod ("OnlyCalledViaReflection", 1, new Type[] { typeof (int) });
method.Invoke (null, new object[] { });
}

[Kept]
static void TestNameWithIntAndBindingFlags ()
{
var method = typeof (TestNameWithIntAndBindingClass).GetMethod ("OnlyCalledViaReflection", 1, BindingFlags.Public, null, new Type [] { }, null);
method.Invoke (null, new object [] { });
var method = typeof (TestNameWithIntAndBindingClass).GetMethod ("OnlyCalledViaReflection", 1, BindingFlags.Public, null, new Type[] { }, null);
method.Invoke (null, new object[] { });
}

[Kept]
[RecognizedReflectionAccessPattern (
typeof (Type), nameof (Type.GetMethod), new Type [] { typeof (string), typeof (int), typeof (BindingFlags), typeof (Binder), typeof(CallingConventions), typeof (Type []), typeof (ParameterModifier []) },
typeof (TestNameWithIntBindingFlagsCallingConventionParameterClass), nameof (TestNameWithIntBindingFlagsCallingConventionParameterClass.OnlyCalledViaReflection), new Type [0])]
static void TestNameWithIntBindingFlagsCallingConventionParameter()
typeof (Type), nameof (Type.GetMethod), new Type[] { typeof (string), typeof (int), typeof (BindingFlags), typeof (Binder), typeof (CallingConventions), typeof (Type[]), typeof (ParameterModifier[]) },
typeof (TestNameWithIntBindingFlagsCallingConventionParameterClass), nameof (TestNameWithIntBindingFlagsCallingConventionParameterClass.OnlyCalledViaReflection), new Type[0])]
static void TestNameWithIntBindingFlagsCallingConventionParameter ()
{
var method = typeof (TestNameWithIntBindingFlagsCallingConventionParameterClass).GetMethod ("OnlyCalledViaReflection", 1, BindingFlags.Static | BindingFlags.NonPublic, null, CallingConventions.Any, new Type [] { }, null);
method.Invoke (null, new object [] { });
var method = typeof (TestNameWithIntBindingFlagsCallingConventionParameterClass).GetMethod ("OnlyCalledViaReflection", 1, BindingFlags.Static | BindingFlags.NonPublic, null, CallingConventions.Any, new Type[] { }, null);
method.Invoke (null, new object[] { });
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void Main ()
#if !NETCOREAPP
[ExpectBodyModified]
#else
[ExpectedInstructionSequence (new [] {
[ExpectedInstructionSequence (new[] {
"nop",
"ldarg.0",
"isinst",
Expand Down Expand Up @@ -52,7 +52,7 @@ static void Test_1 (object type)
#if !NETCOREAPP
[ExpectBodyModified]
#else
[ExpectedInstructionSequence (new [] {
[ExpectedInstructionSequence (new[] {
"nop",
"call",
"stloc.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Mono.Linker.Tests.Extensions;
using Mono.Linker.Tests.TestCasesRunner;
using NUnit.Framework;
Expand Down
Loading

0 comments on commit d4c06d0

Please sign in to comment.