Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ListDotNetTypes/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO.Compression;
using System.IO.Compression;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -38,7 +38,7 @@
if (!string.Equals(Path.GetExtension(entry.Name), ".dll", StringComparison.OrdinalIgnoreCase))
continue;

using var entryStream = CopyToMemoryStream(zipArchive.GetEntry(entry.FullName)!);
await using var entryStream = CopyToMemoryStream(zipArchive.GetEntry(entry.FullName)!);
using var portableExecutableReader = new PEReader(entryStream);
var metadataReader = portableExecutableReader.GetMetadataReader();

Expand All @@ -58,7 +58,7 @@
}
}

await File.WriteAllLinesAsync(Path.Combine(args.Length > 0 ? args[0] : "../../../../Meziantou.Analyzer/Resources/", includePreview ? "bcl-preview.txt" : "bcl.txt"), types.OrderBy(t => t, StringComparer.Ordinal));
await File.WriteAllLinesAsync(Path.Combine(args.Length > 0 ? args[0] : "../../../../Meziantou.Analyzer/Resources/", includePreview ? "bcl-preview.txt" : "bcl.txt"), types.Order(StringComparer.Ordinal));
}

static MemoryStream CopyToMemoryStream(ZipArchiveEntry entry)
Expand Down
20 changes: 17 additions & 3 deletions src/Meziantou.Analyzer/Internals/OverloadFinder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Immutable;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;

Expand Down Expand Up @@ -94,19 +94,33 @@ public bool HasOverloadWithAdditionalParameterOfType(
return null;
}

/// <summary>
/// Methods are similar if:
/// <list type="bullet">
/// <item><paramref name="method"/> and <paramref name="otherMethod"/> are not the same method</item>
/// <item><paramref name="method"/> and <paramref name="otherMethod"/> have parameters of the same types, order is not important</item>
/// <item><paramref name="otherMethod"/> can have additional parameters of type specified by <paramref name="additionalParameterTypes"/></item>
/// <item>If <paramref name="allowOptionalParameters"/>, <paramref name="otherMethod"/> can have more parameters if they are optional</item>
/// </list>
/// </summary>
public static bool HasSimilarParameters(IMethodSymbol method, IMethodSymbol otherMethod, bool allowOptionalParameters, params ITypeSymbol[] additionalParameterTypes)
{
if (method.IsEqualTo(otherMethod))
return false;

// The new method must have at least the same number of parameters as the old method, plus the number of additional parameters
if (otherMethod.Parameters.Length - method.Parameters.Length < additionalParameterTypes.Length)
return false;

// If allowOptionalParameters is false, the new method must have exactly the same number of parameters as the old method
if (!allowOptionalParameters && otherMethod.Parameters.Length - method.Parameters.Length != additionalParameterTypes.Length)
return false;

// Most of the time, an overload has the same order for the parameters
// Most of the time, an overload has the same order for the parameters. Try to match them in order first (faster)
{
int i = 0, j = 0;
var additionalParameterIndex = 0;
while (i < method.Parameters.Length && j < method.Parameters.Length)
while (i < method.Parameters.Length && j < otherMethod.Parameters.Length)
{
var methodParameter = method.Parameters[i];
var otherMethodParameter = otherMethod.Parameters[j];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Meziantou.Analyzer.Rules;
using Meziantou.Analyzer.Test.Helpers;
using Microsoft.CodeAnalysis;
using TestHelper;
using Xunit;

Expand All @@ -16,6 +17,19 @@ private static ProjectBuilder CreateProjectBuilder()
.WithCodeFixProvider<UseAnOverloadThatHasTimeProviderFixer>();
}

[Fact]
public async Task NoReport_ConsoleWriteLine()
{
const string SourceCode = """
System.Console.WriteLine("test");
""";
await CreateProjectBuilder()
.WithOutputKind(OutputKind.ConsoleApplication)
.WithSourceCode(SourceCode)
.ValidateAsync();
}


[Fact]
public async Task NotAvailable()
{
Expand Down