Skip to content
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

Fix S2325 FP: Partial method implementations #8025

Closed
E4est opened this issue Sep 18, 2023 · 0 comments · Fixed by #9381
Closed

Fix S2325 FP: Partial method implementations #8025

E4est opened this issue Sep 18, 2023 · 0 comments · Fixed by #9381
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@E4est
Copy link

E4est commented Sep 18, 2023

Description

S2325 is raised on partial implementations that only refer to their own parameters. The rule correctly recognizes that no instance-specific methods or properties are accessed, but making the partial implementation static doesn't even compile, because both partial declarations should be static.
The original declaring partial class is generated, so it's not an option to edit that.

Repro steps

// In our project this class is generated.
partial class Class
{
    public void WriteEverything()
    {
        Console.WriteLine("Something");

        WriteMore();
    }

    partial void WriteMore();
}

partial class Class
{
    partial void WriteMore() // Noncompliant - FP
    {
        Console.WriteLine("More");
    }
}

Expected behavior

S2325 is not triggered on methods that are part of an external contract.
(This works fine for interfaces.)

Actual behavior

S2325 is triggered.

Known workarounds

Add a private readonly field for any constant value that is used and access that.

// In our project this class is generated.
partial class Class
{
    public void WriteEverything()
    {
        Console.WriteLine("Something");

        WriteMore();
    }

    partial void WriteMore();
}

partial class Class
{
    private readonly string _workaround = "More";

    partial void WriteMore()
    {
        Console.WriteLine(_workaround);
    }
}

Related information

  • C#/VB.NET Plugins version: SonarAnalyzer.CSharp 9.10.0.77988
  • Visual Studio version: 2022 Professional v17.7.3
  • MSBuild / dotnet version: 17.7.2+d6990bcfa / 7.0.400
  • SonarScanner for .NET version (if used): -
  • Operating System: Windows 10 22H2 19045.3448
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix S2325 FP: Rule is raised on partial implementations Fix S2325 FP: Partial method implementations Sep 18, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Sep 18, 2023
@Tim-Pohlmann Tim-Pohlmann added Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements labels Apr 9, 2024
@Tim-Pohlmann Tim-Pohlmann added this to the 9.24 milestone Apr 9, 2024
@Tim-Pohlmann Tim-Pohlmann added this to To do in Best Kanban Apr 9, 2024
@Tim-Pohlmann Tim-Pohlmann removed this from To do in Best Kanban Apr 18, 2024
@Tim-Pohlmann Tim-Pohlmann removed the Sprint: Hardening Fix FPs/FNs/improvements label Apr 18, 2024
@Tim-Pohlmann Tim-Pohlmann removed this from the 9.24 milestone Apr 18, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource removed the Area: VB.NET VB.NET rules related issues. label Jun 4, 2024
@github-actions github-actions bot added this to Review in progress in Best Kanban Jun 4, 2024
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Jun 4, 2024
Best Kanban automation moved this from Review approved to Validate Peach Jun 4, 2024
@CristianAmbrosini CristianAmbrosini moved this from Validate Peach to Done in Best Kanban Jun 5, 2024
@Tim-Pohlmann Tim-Pohlmann added this to the 9.27 milestone Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants