dotnet / dotnet/roslyn

IDE0079 incorrectly flags necessary BL0005 warning suppressions in Blazor component tests

Open
#79,854 0 comments 0 reactions 1 assignee Claimed by @CyrusNajmabadi View on GitHub
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

When testing Blazor components with bUnit, we often need to directly set component parameters (e.g., component.Instance.Value = x) which triggers BL0005 warnings. When suppressing BL0005 with #pragma warning disable BL0005, the IDE0079 analyzer incorrectly reports this as an "unnecessary suppression" even though the suppression is necessary. This creates a catch-22 situation where:
1. If you keep the BL0005 suppression, you get an IDE0079 warning
2. If you remove the suppression as suggested, you get the BL0005 warning back

```cs
[Fact]
public void ComponentTest()
{
var cut = RenderComponent();
var inputNumber = cut.FindComponent>();

// If this line is not suppressed, BL0005 is triggered
#pragma warning disable BL0005 // Component parameter should not be set outside of its component
inputNumber.Instance.Value = 0;
#pragma warning restore BL0005

// IDE0079 incorrectly says the above suppression is unnecessary
// But removing it causes BL0005 warning
}
```

**Version Used**: 9.0.300

**Steps To Reproduce**:

1. Create a Blazor test project with bUnit
2. Create a test that directly sets a component parameter value
3. Add #pragma warning disable BL0005 to suppress the component parameter warning
4. Observe that IDE0079 reports the suppression as "unnecessary"
5. Remove the suppression, and BL0005 warning reappears

**Diagnostic Id**:

[IDE0079](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0079): `Remove unnecessary suppression`

**Expected Behavior**:

IDE0079 should recognize that BL0005 suppressions are necessary when component parameters are directly set in test code and not flag them as "unnecessary suppressions".

**Actual Behavior**:

DE0079 incorrectly flags necessary BL0005 suppressions, forcing developers to either:
1. Add another suppression for IDE0079 (#pragma warning disable IDE0079)
2. Ignore one of the warnings
3. Restructure tests to avoid direct parameter setting (not always feasible)

**Additional Context**:

• This occurs specifically in Blazor component testing scenarios
• The issue appears to be in how IDE0079 evaluates suppression necessity in Blazor contexts
• Current workaround is to add both suppressions:

```cs
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable BL0005 // Component parameter should not be set outside of its component
// Code that would trigger BL0005
#pragma warning restore BL0005
#pragma warning restore IDE0079
```

Manually migrated from https://github.com/dotnet/roslyn-analyzers/issues/7718

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.