Skip to content

Add ECS0008 use the null conditional operator for event invocations #48

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

Conversation

rjmurillo
Copy link
Owner

@rjmurillo rjmurillo commented Aug 9, 2024

This pull request introduces a new analyzer rule, ECS0008, which is designed to enforce the use of the null-conditional operator when invoking event handlers in C#. This practice helps prevent potential NullReferenceExceptions, improving the safety and robustness of the code.

Key Features:

  • Detection of Potential Violations: The rule identifies patterns where an event handler is invoked without using the null-conditional operator (?.Invoke), such as direct invocations or invocations within an if statement checking for null.
  • Automatic Code Fixes: The code fix provider automatically replaces the identified pattern with the null-conditional operator, ensuring that the event handler is only invoked when it has subscribers.
  • Comprehensive Coverage: The rule handles cases where the event handler is checked directly and cases where it is first assigned to a local variable.

Example Violation:

public class EventSource
{
    private EventHandler<int> Updated;
    private int counter;

    public void RaiseUpdates()
    {
        counter++;
        if (Updated != null)
            Updated(this, counter);
    }
}

Fixed code:

public class EventSource
{
    private EventHandler<int> Updated;
    private int counter;

    public void RaiseUpdates()
    {
        counter++;
        Updated?.Invoke(this, counter);
    }
}

@rjmurillo rjmurillo added documentation Improvements or additions to documentation enhancement New feature or request analyzers labels Aug 9, 2024
@rjmurillo rjmurillo marked this pull request as ready for review August 9, 2024 00:27
@rjmurillo rjmurillo enabled auto-merge (squash) August 9, 2024 00:28
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+2.62% (target: -1.00%) 95.62%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (a98a338) 531 440 82.86%
Head commit (0bec560) 668 (+137) 571 (+131) 85.48% (+2.62%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#48) 137 131 95.62%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

@rjmurillo rjmurillo added this to the v0.1.0 milestone Aug 9, 2024
@rjmurillo rjmurillo merged commit d17017b into release/v0.1.0 Aug 9, 2024
9 of 10 checks passed
@rjmurillo rjmurillo deleted the feature/issue-33-ECS0008-use-the-null-conditional-operator-for-event-invocations branch August 9, 2024 00:50
@rjmurillo rjmurillo linked an issue Aug 9, 2024 that may be closed by this pull request
rjmurillo added a commit that referenced this pull request Aug 19, 2024
)

This pull request introduces a new analyzer rule, ECS0008, which is
designed to enforce the use of the null-conditional operator when
invoking event handlers in C#. This practice helps prevent potential
`NullReferenceExceptions`, improving the safety and robustness of the
code.

Key Features:
- **Detection of Potential Violations**: The rule identifies patterns
where an event handler is invoked without using the null-conditional
operator (`?.Invoke`), such as direct invocations or invocations within
an `if` statement checking for `null`.
- **Automatic Code Fixes**: The code fix provider automatically replaces
the identified pattern with the null-conditional operator, ensuring that
the event handler is only invoked when it has subscribers.
- **Comprehensive Coverage**: The rule handles cases where the event
handler is checked directly and cases where it is first assigned to a
local variable.

Example Violation:

```csharp
public class EventSource
{
    private EventHandler<int> Updated;
    private int counter;

    public void RaiseUpdates()
    {
        counter++;
        if (Updated != null)
            Updated(this, counter);
    }
}
```

Fixed code:

```csharp
public class EventSource
{
    private EventHandler<int> Updated;
    private int counter;

    public void RaiseUpdates()
    {
        counter++;
        Updated?.Invoke(this, counter);
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzers documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Item 8: Use the Null Conditional Operator for Event Invocations
1 participant