Skip to content

Commit 4e7a8a6

Browse files
committed
WPF0073 don't warn when generic
Fix #353
1 parent 0096cc0 commit 4e7a8a6

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace ValidCode.Converters;
2+
3+
using System;
4+
using System.Windows.Data;
5+
6+
public sealed class RelayConverter<TSource, TResult> : IValueConverter
7+
{
8+
private readonly Func<TSource, TResult> convert;
9+
10+
public RelayConverter(Func<TSource, TResult> convert)
11+
{
12+
this.convert = convert;
13+
}
14+
15+
public object? Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
16+
{
17+
return value switch
18+
{
19+
TSource source => this.convert(source),
20+
_ => throw new ArgumentException($"Expected value of type {typeof(TSource).Name}"),
21+
};
22+
}
23+
24+
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
25+
{
26+
throw new NotSupportedException($"{nameof(RelayConverter<TSource, TResult>)} can only be used in OneWay bindings");
27+
}
28+
}

WpfAnalyzers.Test/WPF0073ConverterDoesNotHaveAttributeUnknownTypes/Valid.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,40 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
284284
}";
285285
RoslynAssert.Valid(Analyzer, code);
286286
}
287+
288+
[Test]
289+
public static void DonNotWarnWhenGeneric()
290+
{
291+
var code = @"
292+
namespace ValidCode.Converters;
293+
294+
using System;
295+
using System.Windows.Data;
296+
297+
public sealed class RelayConverter<TSource, TResult> : IValueConverter
298+
{
299+
private readonly Func<TSource, TResult> convert;
300+
301+
public RelayConverter(Func<TSource, TResult> convert)
302+
{
303+
this.convert = convert;
304+
}
305+
306+
public object? Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
307+
{
308+
return value switch
309+
{
310+
TSource source => this.convert(source),
311+
_ => throw new ArgumentException($""Expected value of type {typeof(TSource).Name}""),
312+
};
313+
}
314+
315+
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
316+
{
317+
throw new NotSupportedException($""{nameof(RelayConverter<TSource, TResult>)} can only be used in OneWay bindings"");
318+
}
319+
}
320+
";
321+
RoslynAssert.Valid(Analyzer, code);
322+
}
287323
}

WpfAnalyzers/Analyzers/ValueConverterAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ context.Node is ClassDeclarationSyntax classDeclaration &&
6262
{
6363
if (ValueConverter.TryGetConversionTypes(classDeclaration, context.SemanticModel, context.CancellationToken, out var sourceType, out var targetType))
6464
{
65-
if (sourceType is { } &&
66-
sourceType != QualifiedType.System.Object &&
65+
if (sourceType != QualifiedType.System.Object &&
6766
attribute.TryFindArgument(0, "sourceType", out var arg) &&
6867
arg.Expression is TypeOfExpressionSyntax sourceTypeOf &&
6968
TypeSymbol.TryGet(sourceTypeOf, type, context.SemanticModel, context.CancellationToken, out var argType) &&
@@ -81,7 +80,8 @@ arg.Expression is TypeOfExpressionSyntax targetTypeOf &&
8180
}
8281
}
8382
}
84-
else if (!classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
83+
else if (!classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword) &&
84+
!type.IsGenericType)
8585
{
8686
context.ReportDiagnostic(
8787
Diagnostic.Create(

0 commit comments

Comments
 (0)