DotNetAnalyzers / WpfAnalyzers

MIT License
168 stars 19 forks source link

WPF0071 should figure out types here #192

Open JohanLarsson opened 6 years ago

JohanLarsson commented 6 years ago
namespace RoslynSandbox
{
    using System;
    using System.Globalization;
    using System.Windows.Data;

    public class StringComparisonToBoolConverter : IValueConverter
    {
        public static readonly StringComparisonToBoolConverter TrueWhenOrdinal = new StringComparisonToBoolConverter(StringComparison.Ordinal);
        public static readonly StringComparisonToBoolConverter TrueWhenOrdinalIgnoreCase = new StringComparisonToBoolConverter(StringComparison.OrdinalIgnoreCase);

        private readonly StringComparison trueWhen;

        private StringComparisonToBoolConverter(StringComparison trueWhen)
        {
            this.trueWhen = trueWhen;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Equals(value, this.trueWhen);
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException($"{nameof(StringComparisonToBoolConverter)} can only be used in OneWay bindings");
        }
    }
}
JohanLarsson commented 6 years ago
[Test]
public void Issue192()
{
    var testCode = @"
namespace RoslynSandbox
{
using System;
using System.Globalization;
using System.Windows.Data;

public class StringComparisonToBoolConverter : IValueConverter
{
public static readonly StringComparisonToBoolConverter TrueWhenOrdinal = new StringComparisonToBoolConverter(StringComparison.Ordinal);
public static readonly StringComparisonToBoolConverter TrueWhenOrdinalIgnoreCase = new StringComparisonToBoolConverter(StringComparison.OrdinalIgnoreCase);

private readonly StringComparison trueWhen;

private StringComparisonToBoolConverter(StringComparison trueWhen)
{
    this.trueWhen = trueWhen;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return Equals(value, this.trueWhen);
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotSupportedException($""{nameof(StringComparisonToBoolConverter)} can only be used in OneWay bindings"");
}
}
}";

    var fixedCode = @"
namespace RoslynSandbox
{
using System;
using System.Globalization;
using System.Windows.Data;

[ValueConversion(typeof(StringComparison), typeof(bool))]
public class StringComparisonToBoolConverter : IValueConverter
{
public static readonly StringComparisonToBoolConverter TrueWhenOrdinal = new StringComparisonToBoolConverter(StringComparison.Ordinal);
public static readonly StringComparisonToBoolConverter TrueWhenOrdinalIgnoreCase = new StringComparisonToBoolConverter(StringComparison.OrdinalIgnoreCase);

private readonly StringComparison trueWhen;

private StringComparisonToBoolConverter(StringComparison trueWhen)
{
    this.trueWhen = trueWhen;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return Equals(value, this.trueWhen);
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotSupportedException($""{nameof(StringComparisonToBoolConverter)} can only be used in OneWay bindings"");
}
}
}";
    AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, testCode, fixedCode, allowCompilationErrors: AllowCompilationErrors.Yes);
}