Skip to content
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

NUnit1001 false positive for types with custom type converters #232

Closed
jnm2 opened this issue May 5, 2020 · 3 comments
Closed

NUnit1001 false positive for types with custom type converters #232

jnm2 opened this issue May 5, 2020 · 3 comments

Comments

@jnm2
Copy link
Contributor

jnm2 commented May 5, 2020

A warning should not be produced. The analyzer can see that CustomType has [TypeConverter(typeof(CustomTypeConverter))] even though it can't run it. It's a safe assumption that type converters can convert from string. Even without that assumption (say, with an int test parameter), the warning message is making a claim that it can't prove.

using System;
using System.ComponentModel;
using System.Globalization;
using NUnit.Framework;

class C
{
    // NUnit1001 The value of the argument at position '0' of type string cannot be assigned to
    // the argument 'customType' of type CustomType.
    //        ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    [TestCase("String representation")]
    public void M(CustomType customType)
    {
    }
}

[TypeConverter(typeof(CustomTypeConverter))]
struct CustomType
{
    public CustomType(string value)
    {
        Value = value;
    }

    public string Value { get; }

    private sealed class CustomTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return value is string str
                ? new CustomType(str)
                : base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            return destinationType == typeof(string)
                ? value.ToString()
                : base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
@mikkelbu
Copy link
Member

mikkelbu commented May 5, 2020

@jnm2 This sounds like a duplicate of #197, but I like this issue better as it provides more information.

@jnm2
Copy link
Contributor Author

jnm2 commented May 6, 2020

Thanks. Can I close this one and move my comment there?

@mikkelbu
Copy link
Member

mikkelbu commented May 6, 2020

Sure 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants