SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
797 stars 229 forks source link

Fix S2325 FP: XAML (WPF) event handlers cannot not be static #9695

Closed PetrJasek89 closed 3 days ago

PetrJasek89 commented 5 days ago

Description

XAML event handlers cannot be static. Currently S2325 is raised on XAML event handlers.

The issue is the same as #5536, only instead of WinForms it applies to XAML.

Repro steps

private void SomeControl_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Handling of the event
}

Expected behavior

No issue raised as XAML cannot handle static event handlers.

Actual behavior

S2325 is raised.

Known workarounds

Suppression attribute added to all affected methods.

PetrJasek89 commented 5 days ago

I cannot build and test on my system so I didn't want to create a PR with a fix. I have at least created a path (9695.patch) that should fix the issue. It basically copies the fix that was done for #5536.

I think there could be done a small improvement that would also apply to the WinForms case. In both cases, the containing type will be (has to be) partial. Therefore check for this could be added as well.

sebastien-marichal commented 4 days ago

Hello @PetrJasek89,

Thank you for reporting this issue.

I confirm this indeed a false positive. I will add it to our backlog to tackle it later.

Thank you also for you suggested patch, it seems close to what we would need to do to add this exclusion.

Note for the implementation: Exception list on RSPEC need to updated for this rule.

sebastien-marichal commented 3 days ago

I am closing the issue as it has been moved to our internal backlog for future fix: NET-710