Skip to content

Commit

Permalink
Code gardening (#150)
Browse files Browse the repository at this point in the history
* Code Gardening

* Remaining codefixes
  • Loading branch information
sailro authored Feb 13, 2021
1 parent d120ad2 commit 706b0a0
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.Unity.Analyzers.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Xunit;
using Xunit.Sdk;

namespace Microsoft.Unity.Analyzers.Tests
{
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Unity.Analyzers/CreateInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.Unity.Analyzers/IndirectionMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*-------------------------------------------------------------------------------------------*/

using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down
7 changes: 2 additions & 5 deletions src/Microsoft.Unity.Analyzers/KnownMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Unity.Analyzers
{
internal static class KnownMethods
{
private static readonly HashSet<string> GetComponentNames = new HashSet<string>(new[]
internal static readonly HashSet<string> GetComponentNames = new HashSet<string>(new[]
{
nameof(UnityEngine.Component.GetComponent),
nameof(UnityEngine.Component.GetComponentInChildren),
Expand All @@ -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));
}
}
}
12 changes: 6 additions & 6 deletions src/Microsoft.Unity.Analyzers/MethodInvocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 17 additions & 7 deletions src/Microsoft.Unity.Analyzers/ScriptInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodInfo> 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;
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.Unity.Analyzers/SetPositionAndRotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.Unity.Analyzers/UnityStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Collections.Generic;
using Microsoft.Unity.Analyzers;

#pragma warning disable

namespace UnityEngine
{
class Object
Expand Down Expand Up @@ -451,3 +453,5 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
}

}

#pragma warning enable
4 changes: 2 additions & 2 deletions src/Microsoft.Unity.Analyzers/UpdateDeltaTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
20 changes: 8 additions & 12 deletions src/new-analyzer/AbstractDiagnosticBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 706b0a0

Please sign in to comment.