Skip to content

Commit

Permalink
FP NUnit1001: CustomTypeConverters could convert from anything (#559)
Browse files Browse the repository at this point in the history
* CustomTypeConverters could convert from anything.

---------

Co-authored-by: Corniel Nobel <mail@corniel.nl>
Co-authored-by: Manfred Brands <manfred-brands@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 19, 2023
1 parent 36e649e commit 156c9c0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,15 @@ public void AnalyzeArgumentIsIntConvertedToTypeWithCustomConverter()
class C
{
[TestCase(2)]
[TestCase(2)]
public void Test(CustomType p) { }
}
[TypeConverter(typeof(CustomTypeConverter))]
struct CustomType { }
class CustomTypeConverter : TypeConverter { }");

RoslynAssert.Diagnostics(this.analyzer,
ExpectedDiagnostic.Create(AnalyzerIdentifiers.TestCaseParameterTypeMismatchUsage),
testCode);
RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private static bool CanBeTranslatedByTypeConverter(
{
if (targetType.typeConverter is null)
{
return argumentValue.GetType() == typeof(string);
return argumentValue is string;
}

var typeConverter = targetType.typeConverter.Value;
Expand All @@ -210,11 +210,12 @@ private static bool CanBeTranslatedByTypeConverter(
}
}
}
else if (argumentValue is null || argumentValue is string)
else
{
if (compilation.GetTypeByMetadataName(typeof(TypeConverterAttribute).FullName!) is { } typeConverterAttribute
&& targetTypeSymbol.GetAllAttributes().Any(data => SymbolEqualityComparer.Default.Equals(typeConverterAttribute, data.AttributeClass)))
{
// If there is an explicit listed TypeConverter, we assume it can convert anything.
return true;
}
}
Expand Down

0 comments on commit 156c9c0

Please sign in to comment.