Skip to content

Commit

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

* More gardening
  • Loading branch information
sailro authored Feb 22, 2024
1 parent dfb120e commit f54a146
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
15 changes: 4 additions & 11 deletions src/Microsoft.Unity.Analyzers.Tests/ConsistencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Microsoft.Unity.Analyzers.Tests;

public class ConsistencyTests
public class ConsistencyTests(ITestOutputHelper output)
{
private static Dictionary<string, List<int>> CollectIds<T>(Func<T, string> reader) where T : class
{
Expand Down Expand Up @@ -42,7 +42,7 @@ private static Dictionary<string, List<int>> CollectIds<T>(Func<T, string> reade
var num = int.Parse(id[3..]);

if (!lookup.TryGetValue(prefix, out _))
lookup.Add(prefix, new List<int>());
lookup.Add(prefix, []);

lookup[prefix].Add(num);
}
Expand Down Expand Up @@ -74,19 +74,12 @@ private static void CheckLookup(Dictionary<string, List<int>> lookup)
}
}

private readonly ITestOutputHelper _output;

public ConsistencyTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void CheckAnalyzerIds()
{
CheckLookup(CollectIds<DiagnosticDescriptor>(d =>
{
_output.WriteLine($"Scanning diagnostic {d.Id}: {d.Description}");
output.WriteLine($"Scanning diagnostic {d.Id}: {d.Description}");
return d.Id;
}));
}
Expand All @@ -96,7 +89,7 @@ public void CheckSuppressorIds()
{
CheckLookup(CollectIds<SuppressionDescriptor>(d =>
{
_output.WriteLine($"Scanning suppressor {d.Id} for {d.SuppressedDiagnosticId}: {d.Justification}");
output.WriteLine($"Scanning suppressor {d.Id} for {d.SuppressedDiagnosticId}: {d.Justification}");
return d.Id;
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace Microsoft.Unity.Analyzers.Tests;
/// </summary>
public readonly struct DiagnosticResult
{
private static readonly object[] EmptyArguments = [];

private readonly ImmutableArray<DiagnosticLocation> _spans;
private readonly bool _suppressMessage;
private readonly string? _message;
Expand Down Expand Up @@ -74,7 +72,7 @@ public string? Message

if (_message != null) return _message;

if (MessageFormat != null) return string.Format(MessageFormat.ToString(), MessageArguments ?? EmptyArguments);
if (MessageFormat != null) return string.Format(MessageFormat.ToString(), MessageArguments ?? []);

return null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.Unity.Analyzers.Tests/UnityPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace Microsoft.Unity.Analyzers.Tests;

internal static class UnityPath
{
private static readonly List<string> UnityInstallations = new();
private static readonly List<string> _unityInstallations = [];

public static string FirstInstallation()
{
return UnityInstallations.First();
return _unityInstallations.First();
}

static UnityPath()
Expand All @@ -33,7 +33,7 @@ static UnityPath()
if (OperatingSystem.IsLinux())
RegisterLinuxInstallations();

if (UnityInstallations.Count == 0)
if (_unityInstallations.Count == 0)
throw new Exception("Could not locate a Unity installation");
}

Expand Down Expand Up @@ -107,6 +107,6 @@ private static void RegisterRegistryInstallations()
private static void RegisterUnityInstallation(string path)
{
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
UnityInstallations.Add(path);
_unityInstallations.Add(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static IEnumerable<SyntaxNode> MethodBodies(SyntaxNode root)
methodBodies = methodBodies.Concat(methods
.Where(syntax => methodSyntax.DescendantNodes().OfType<InvocationExpressionSyntax>()
.Any(invocationSyntax => invocationSyntax.Expression.ToString() == syntax.Identifier.Text))
.Concat(new[] { methodSyntax })
.Append(methodSyntax)
.Select(method => method.Body ?? method.ExpressionBody as SyntaxNode))!;
}

Expand Down

0 comments on commit f54a146

Please sign in to comment.