Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 21, 2025

The TUnit analyzer was incorrectly enforcing the required keyword for properties with data source attributes in all classes, including custom Attribute classes. This caused unnecessary compiler errors when creating custom attributes that use property injection.

Problem

When defining a custom attribute that inherits from System.Attribute with injected properties, the analyzer would flag TUnit0043 errors:

public class MyCustomAttribute : System.Attribute
{
    [ClassDataSource<string>]
    public string TestProperty { get; init; } // ❌ TUnit0043: Property must use `required` keyword
}

This was problematic because:

  • Attribute classes have different initialization semantics than test classes
  • The required keyword enforcement should only apply to test classes where property injection occurs during test execution
  • Custom attributes need flexibility in their property definitions

Solution

Modified the TestDataAnalyzer.CheckPropertyAccessor() method to:

  1. Check inheritance hierarchy: Added IsInAttributeClass() helper method that uses the existing IsOrInherits() extension to detect if a property's containing type inherits from System.Attribute

  2. Skip enforcement for Attribute classes: When a property with data source attributes is in an Attribute class, the analyzer no longer reports TUnit0043

  3. Maintain enforcement for test classes: Regular test classes continue to require the required keyword for proper property injection

After the fix

// ✅ No longer triggers TUnit0043
public class MyCustomAttribute : System.Attribute
{
    [ClassDataSource<string>]
    public string TestProperty { get; init; }
}

// ✅ Still correctly enforces required keyword
public class MyTestClass
{
    [ClassDataSource<string>]
    public required string TestProperty { get; init; } // Required for test classes
}

The fix correctly handles both direct inheritance (: System.Attribute) and indirect inheritance through base classes, ensuring robust detection of attribute classes.

Fixes #2980.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

… properties

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Don't enforce the required keyword for injected properties in Attribute classes Fix analyzer to skip required keyword enforcement for Attribute class properties Aug 21, 2025
@Copilot Copilot AI requested a review from thomhurst August 21, 2025 13:05
Copilot finished work on behalf of thomhurst August 21, 2025 13:05
@thomhurst thomhurst marked this pull request as ready for review August 21, 2025 13:09
@Copilot Copilot AI temporarily deployed to Pull Requests August 21, 2025 13:09 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 21, 2025 13:09 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 21, 2025 13:09 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 21, 2025 13:09 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 21, 2025 13:09 Inactive
@thomhurst thomhurst enabled auto-merge (squash) August 21, 2025 13:09
@thomhurst thomhurst disabled auto-merge August 21, 2025 13:37
@thomhurst thomhurst merged commit 8956b1e into main Aug 21, 2025
12 of 15 checks passed
@thomhurst thomhurst deleted the copilot/fix-2980 branch August 21, 2025 13:37
This was referenced Aug 25, 2025
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

Successfully merging this pull request may close these issues.

Don't enforce the required keyword for injected properties in Attribute classes
2 participants