From 706b0a0afdc1cdd965a90cb1f2474d62d4f400b5 Mon Sep 17 00:00:00 2001 From: Sebastien Lebreton Date: Sat, 13 Feb 2021 11:08:47 -0800 Subject: [PATCH] Code gardening (#150) * Code Gardening * Remaining codefixes --- .../BaseTest.cs | 2 +- .../SerializeFieldSuppressorTests.cs | 1 - .../CreateInstance.cs | 4 ++-- .../IndirectionMessage.cs | 1 - src/Microsoft.Unity.Analyzers/KnownMethods.cs | 7 ++---- .../MethodInvocation.cs | 12 +++++----- src/Microsoft.Unity.Analyzers/ScriptInfo.cs | 24 +++++++++++++------ .../SetPositionAndRotation.cs | 1 - src/Microsoft.Unity.Analyzers/UnityStubs.cs | 4 ++++ .../UpdateDeltaTime.cs | 4 ++-- src/new-analyzer/AbstractDiagnosticBuilder.cs | 20 +++++++--------- 11 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.Unity.Analyzers.Tests/BaseTest.cs b/src/Microsoft.Unity.Analyzers.Tests/BaseTest.cs index 9e7258d5..97321762 100644 --- a/src/Microsoft.Unity.Analyzers.Tests/BaseTest.cs +++ b/src/Microsoft.Unity.Analyzers.Tests/BaseTest.cs @@ -11,7 +11,7 @@ [module: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Tests", Scope = "namespaceanddescendants", - Target = "Microsoft.Unity.Analyzers.Tests")] + Target = "~N:Microsoft.Unity.Analyzers.Tests")] namespace Microsoft.Unity.Analyzers.Tests { diff --git a/src/Microsoft.Unity.Analyzers.Tests/SerializeFieldSuppressorTests.cs b/src/Microsoft.Unity.Analyzers.Tests/SerializeFieldSuppressorTests.cs index e46d174c..6dcc6a2a 100644 --- a/src/Microsoft.Unity.Analyzers.Tests/SerializeFieldSuppressorTests.cs +++ b/src/Microsoft.Unity.Analyzers.Tests/SerializeFieldSuppressorTests.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Xunit; -using Xunit.Sdk; namespace Microsoft.Unity.Analyzers.Tests { diff --git a/src/Microsoft.Unity.Analyzers/CreateInstance.cs b/src/Microsoft.Unity.Analyzers/CreateInstance.cs index d6287abb..9eb826b5 100644 --- a/src/Microsoft.Unity.Analyzers/CreateInstance.cs +++ b/src/Microsoft.Unity.Analyzers/CreateInstance.cs @@ -24,7 +24,7 @@ public class CreateInstanceAnalyzer : DiagnosticAnalyzer { public const string ComponentId = "UNT0010"; - private static readonly DiagnosticDescriptor ComponentIdRule = new DiagnosticDescriptor( + internal static readonly DiagnosticDescriptor ComponentIdRule = new DiagnosticDescriptor( ComponentId, title: Strings.CreateComponentInstanceDiagnosticTitle, messageFormat: Strings.CreateComponentInstanceDiagnosticMessageFormat, @@ -35,7 +35,7 @@ public class CreateInstanceAnalyzer : DiagnosticAnalyzer public const string ScriptableObjectId = "UNT0011"; - private static readonly DiagnosticDescriptor ScriptableObjectRule = new DiagnosticDescriptor( + internal static readonly DiagnosticDescriptor ScriptableObjectRule = new DiagnosticDescriptor( ScriptableObjectId, title: Strings.CreateScriptableObjectInstanceDiagnosticTitle, messageFormat: Strings.CreateScriptableObjectInstanceDiagnosticMessageFormat, diff --git a/src/Microsoft.Unity.Analyzers/IndirectionMessage.cs b/src/Microsoft.Unity.Analyzers/IndirectionMessage.cs index 439c184d..9583157b 100644 --- a/src/Microsoft.Unity.Analyzers/IndirectionMessage.cs +++ b/src/Microsoft.Unity.Analyzers/IndirectionMessage.cs @@ -4,7 +4,6 @@ *-------------------------------------------------------------------------------------------*/ using System.Collections.Immutable; -using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; diff --git a/src/Microsoft.Unity.Analyzers/KnownMethods.cs b/src/Microsoft.Unity.Analyzers/KnownMethods.cs index 6a916bcf..37f92089 100644 --- a/src/Microsoft.Unity.Analyzers/KnownMethods.cs +++ b/src/Microsoft.Unity.Analyzers/KnownMethods.cs @@ -10,7 +10,7 @@ namespace Microsoft.Unity.Analyzers { internal static class KnownMethods { - private static readonly HashSet GetComponentNames = new HashSet(new[] + internal static readonly HashSet GetComponentNames = new HashSet(new[] { nameof(UnityEngine.Component.GetComponent), nameof(UnityEngine.Component.GetComponentInChildren), @@ -27,10 +27,7 @@ public static bool IsGetComponent(IMethodSymbol method) return false; var containingType = method.ContainingType; - if (!containingType.Matches(typeof(UnityEngine.Component)) && !containingType.Matches(typeof(UnityEngine.GameObject))) - return false; - - return true; + return containingType.Matches(typeof(UnityEngine.Component)) || containingType.Matches(typeof(UnityEngine.GameObject)); } } } diff --git a/src/Microsoft.Unity.Analyzers/MethodInvocation.cs b/src/Microsoft.Unity.Analyzers/MethodInvocation.cs index 20739dec..9f518a9f 100644 --- a/src/Microsoft.Unity.Analyzers/MethodInvocation.cs +++ b/src/Microsoft.Unity.Analyzers/MethodInvocation.cs @@ -194,16 +194,16 @@ public MethodInvocationNameOfCodeFix() : base(Strings.MethodInvocationNameOfCode protected override ArgumentSyntax GetArgument(string name) { - var nameof = IdentifierName( - Identifier( - TriviaList(), + const string nameof = "nameof"; + + var identifierName = IdentifierName(Identifier(TriviaList(), SyntaxKind.NameOfKeyword, - "nameof", - "nameof", + nameof, + nameof, TriviaList())); return Argument( - InvocationExpression(nameof) + InvocationExpression(identifierName) .WithArgumentList( ArgumentList( SingletonSeparatedList( diff --git a/src/Microsoft.Unity.Analyzers/ScriptInfo.cs b/src/Microsoft.Unity.Analyzers/ScriptInfo.cs index 88754fa7..1a00fbb3 100644 --- a/src/Microsoft.Unity.Analyzers/ScriptInfo.cs +++ b/src/Microsoft.Unity.Analyzers/ScriptInfo.cs @@ -13,28 +13,38 @@ namespace Microsoft.Unity.Analyzers { internal class ScriptInfo { - private static readonly Type[] Types = { typeof(UnityEngine.Networking.NetworkBehaviour), typeof(UnityEngine.StateMachineBehaviour), typeof(UnityEngine.EventSystems.UIBehaviour), typeof(UnityEditor.ScriptableWizard), typeof(UnityEditor.EditorWindow), typeof(UnityEditor.Editor), typeof(UnityEngine.ScriptableObject), typeof(UnityEngine.MonoBehaviour), typeof(UnityEditor.AssetPostprocessor) }; + internal static readonly Type[] Types = + { + typeof(UnityEngine.Networking.NetworkBehaviour), + typeof(UnityEngine.StateMachineBehaviour), + typeof(UnityEngine.EventSystems.UIBehaviour), + typeof(UnityEditor.ScriptableWizard), + typeof(UnityEditor.EditorWindow), + typeof(UnityEditor.Editor), + typeof(UnityEngine.ScriptableObject), + typeof(UnityEngine.MonoBehaviour), + typeof(UnityEditor.AssetPostprocessor) + }; private readonly ITypeSymbol _symbol; - private readonly Type _metadata; - public bool HasMessages => _metadata != null; - public Type Metadata => _metadata; + public bool HasMessages => Metadata != null; + public Type Metadata { get; } public ScriptInfo(ITypeSymbol symbol) { _symbol = symbol; - _metadata = GetMatchingMetadata(symbol); + Metadata = GetMatchingMetadata(symbol); } public static MethodInfo[] Messages { get; } = Types.SelectMany(GetMessages).ToArray(); public IEnumerable GetMessages() { - if (_metadata == null) + if (Metadata == null) yield break; - for (var type = _metadata; type != null && type != typeof(object); type = type.GetTypeInfo().BaseType) + for (var type = Metadata; type != null && type != typeof(object); type = type.GetTypeInfo().BaseType) { foreach (var message in GetMessages(type)) yield return message; diff --git a/src/Microsoft.Unity.Analyzers/SetPositionAndRotation.cs b/src/Microsoft.Unity.Analyzers/SetPositionAndRotation.cs index 4ac1fe53..75d363de 100644 --- a/src/Microsoft.Unity.Analyzers/SetPositionAndRotation.cs +++ b/src/Microsoft.Unity.Analyzers/SetPositionAndRotation.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Immutable; -using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; diff --git a/src/Microsoft.Unity.Analyzers/UnityStubs.cs b/src/Microsoft.Unity.Analyzers/UnityStubs.cs index 8ce9b514..626bda45 100644 --- a/src/Microsoft.Unity.Analyzers/UnityStubs.cs +++ b/src/Microsoft.Unity.Analyzers/UnityStubs.cs @@ -9,6 +9,8 @@ using System.Collections.Generic; using Microsoft.Unity.Analyzers; +#pragma warning disable + namespace UnityEngine { class Object @@ -451,3 +453,5 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse } } + +#pragma warning enable diff --git a/src/Microsoft.Unity.Analyzers/UpdateDeltaTime.cs b/src/Microsoft.Unity.Analyzers/UpdateDeltaTime.cs index c621eb51..3cc160cf 100644 --- a/src/Microsoft.Unity.Analyzers/UpdateDeltaTime.cs +++ b/src/Microsoft.Unity.Analyzers/UpdateDeltaTime.cs @@ -23,7 +23,7 @@ public class UpdateDeltaTimeAnalyzer : DiagnosticAnalyzer { internal const string UpdateId = "UNT0004"; - private static readonly DiagnosticDescriptor UpdateRule = new DiagnosticDescriptor( + internal static readonly DiagnosticDescriptor UpdateRule = new DiagnosticDescriptor( UpdateId, title: Strings.UpdateWithoutFixedDeltaTimeDiagnosticTitle, messageFormat: Strings.UpdateWithoutFixedDeltaTimeDiagnosticMessageFormat, @@ -34,7 +34,7 @@ public class UpdateDeltaTimeAnalyzer : DiagnosticAnalyzer internal const string FixedUpdateId = "UNT0005"; - private static readonly DiagnosticDescriptor FixedUpdateRule = new DiagnosticDescriptor( + internal static readonly DiagnosticDescriptor FixedUpdateRule = new DiagnosticDescriptor( FixedUpdateId, title: Strings.FixedUpdateWithoutDeltaTimeDiagnosticTitle, messageFormat: Strings.FixedUpdateWithoutDeltaTimeDiagnosticMessageFormat, diff --git a/src/new-analyzer/AbstractDiagnosticBuilder.cs b/src/new-analyzer/AbstractDiagnosticBuilder.cs index c9ddad26..c5db54ba 100644 --- a/src/new-analyzer/AbstractDiagnosticBuilder.cs +++ b/src/new-analyzer/AbstractDiagnosticBuilder.cs @@ -108,18 +108,15 @@ private bool TryGetAnalyzerIdentifier(string file, out int identifier) { identifier = -1; - using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var reader = new StreamReader(fs); + + string line; + while ((line = reader.ReadLine()) != null) { - using (var reader = new StreamReader(fs)) + if (TryExtractIdentifier(line, out int id)) { - string line; - while ((line = reader.ReadLine()) != null) - { - if (TryExtractIdentifier(line, out int id)) - { - identifier = Math.Max(identifier, id); - } - } + identifier = Math.Max(identifier, id); } } @@ -141,8 +138,7 @@ private bool TryExtractIdentifier(string line, out int identifier) index += 3; - var id = line.Substring(index, end - index); - + var id = line[index..end]; return int.TryParse(id, out identifier); }