Skip to content

Commit

Permalink
Emit diagnostics even if default value is not constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jan 5, 2025
1 parent 515a42c commit d6743ba
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,19 +736,19 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
// pretending this were the empty string literal instead. This way we can still support the property and convert to an attribute.
fieldFlags.DefaultValue = TypedConstantInfo.Primitive.String.Empty;
}
else
else if (fieldFlags.DefaultValueOperation is IDefaultValueOperation { Type: { } defaultValueExpressionType })
{
// If we don't have a constant, check if it's some constant value we can forward. In this case, we
// did not retrieve it. As a last resort, check if this is explicitly a 'default(T)' expression.
if (fieldFlags.DefaultValueOperation is not IDefaultValueOperation { Type: { } defaultValueExpressionType })
{
continue;
}

// Store the expression type for later, so we can validate it. We cannot validate it from here, as we
// only see the declared property type for metadata. This isn't guaranteed to match the property type.
// We don't have a constant. As a last resort, check if this is explicitly a 'default(T)' expression.
// If so, store the expression type for later, so we can validate it. We cannot validate it here, as
// we still want to execute the rest of the checks below to potentially emit more diagnostics first.
fieldFlags.DefaultValueExpressionType = defaultValueExpressionType;
}
else
{
// At this point we know the property cannot possibly be converted, so mark it as invalid. We do this
// rather than returning immediately, to still allow more diagnostics to be produced in the steps below.
fieldFlags.HasAnyDiagnostics = true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,63 @@ public partial class MyControl : Control
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
}

// Regression test for a case found in the Microsoft Store
[TestMethod]
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_InvalidRegisterArguments_WCTDP0030_WithObjectInitialization_DoesNotWarn()
{
const string source = """
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace MyApp;
public partial class MyControl : Control
{
public static readonly DependencyProperty MarginProperty = DependencyProperty.Register(
nameof(Margin),
{|WCTDP0030:typeof(double)|},
typeof(MyControl),
new PropertyMetadata({|WCTDP0032:new Thickness(0)|}));
private Thickness Margin
{
get => (Thickness)GetValue(MarginProperty);
set => SetValue(MarginProperty, value);
}
}
""";

await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
}

[TestMethod]
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_InvalidRegisterArguments_WCTDP0030_WithExplicitDefaultValueNull_DoesNotWarn()
{
const string source = """
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace MyApp;
public partial class MyControl : Control
{
public static readonly DependencyProperty MarginProperty = DependencyProperty.Register(
nameof(Margin),
{|WCTDP0030:typeof(double)|},
typeof(MyControl),
new PropertyMetadata({|WCTDP0031:null|}));
private Thickness Margin
{
get => (Thickness)GetValue(MarginProperty);
set => SetValue(MarginProperty, value);
}
}
""";

await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
}

// Regression test for a case found in https://github.com/jenius-apps/ambie
[TestMethod]
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_InvalidRegisterArguments_WCTDP0030_WithInvalidPropertyName_DoesNotWarn()
Expand Down

0 comments on commit d6743ba

Please sign in to comment.