-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FP NUnit1001: CustomTypeConverters could convert from anything #559
FP NUnit1001: CustomTypeConverters could convert from anything #559
Conversation
@@ -210,6 +210,10 @@ private static bool HasBuiltInImplicitConversion(ITypeSymbol argumentType, IType | |||
} | |||
} | |||
} | |||
else if (targetTypeSymbol.GetAttributes().Any(att => att.AttributeClass?.GetFullMetadataName() == typeof(TypeConverterAttribute).FullName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not behind a computer, but isn't TypeConverter not already dealt with?
You added a vase when targetType is null?
The CustomTypeConverter in your test doesn't override the Can convert from tested in the existing code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeConverters are only dealt with for strings. And the code tries to check for types it can access the code from. In most cases that is not possible. So basically, you can not know if it is supported or not, but because of that, you should report on it. Specially because the default severity is an error, and it runs perfectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I am on leave without access to a development PC until middle of July. I will look further then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I forgot to look at.
Your added case can be removed. Instead we can remove the condition on the line below.
1bd4371
to
b59c277
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a commit to your branch simplifying the solution.
Please check if it works for your use case.
[TypeConverter(typeof(CustomTypeConverter))] | ||
struct CustomType { } | ||
class CustomTypeConverter : TypeConverter { }"); | ||
|
||
RoslynAssert.Valid(this.analyzer, testCode); | ||
} | ||
|
||
[Test] | ||
public void AnalyzeArgumentIsSomeConvertedToTypeWithCustomConverter() | ||
{ | ||
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@" | ||
using System.ComponentModel; | ||
|
||
class C | ||
{ | ||
[TestCase(17)] | ||
public void Test(CustomType p) { } | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a test for this case, but one that assumes failure.
@@ -210,6 +210,10 @@ private static bool HasBuiltInImplicitConversion(ITypeSymbol argumentType, IType | |||
} | |||
} | |||
} | |||
else if (targetTypeSymbol.GetAttributes().Any(att => att.AttributeClass?.GetFullMetadataName() == typeof(TypeConverterAttribute).FullName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I forgot to look at.
Your added case can be removed. Instead we can remove the condition on the line below.
@manfred-brands I saw you made the changes yourself. Would you like me to make any changes in this PR? |
No. Are you happy with the changes? |
I stumbled into this, as I use this feature quite often:
Where
Qowaiv.Financial.Amount
is a value type that has its own type converter that not only can convert from string, but also fromint
,long
,double
,float
, anddecimal
. This works like a charm, so it too bad that this falsely flags NUnit1001.I've provided a fix, But if the implementation should be changed, just let me no.
Note that none existing test started to fail with this changed behavior.