Skip to content

Commit

Permalink
Accept generic type parameters as key types without complaint (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBoike authored Apr 10, 2024
1 parent 7ae6748 commit fb96b95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Particular.Analyzers.Tests/DictionaryKeyAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,23 @@ public record class GoodRecord(string A, int B);
return Assert(code, DiagnosticIds.DictionaryHasUnsupportedKeyType);
}
#endif

[Test]
public Task IgnoreGenericParameters()
{
var code = """
using System.Collections.Generic;

public static class Extensions
{
public static void GoodExtensionMethod<T>(this HashSet<T> set)
{
// User will be warned on the type they are trying to use the method on
}
}
""";

return Assert(code, DiagnosticIds.DictionaryHasUnsupportedKeyType);
}
}
}
6 changes: 6 additions & 0 deletions src/Particular.Analyzers/DictionaryKeysAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ static bool IsAppropriateDictionaryKey(KnownTypes knownTypes, ITypeSymbol type)
return true;
}

if (type is ITypeParameterSymbol)
{
// Especially on extension methods and whatnot, the user will be warned on whatever concrete type they're using
return true;
}

var implementsIEquatable = type.Interfaces
.Any(iface => iface.IsGenericType && iface.ConstructedFrom.Equals(knownTypes.IEquatableT, SymbolEqualityComparer.Default));

Expand Down

0 comments on commit fb96b95

Please sign in to comment.