Skip to content

Fix S2325 FP: Partial method implementations #8025

@E4est

Description

@E4est

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

Metadata

Metadata

Labels

False PositiveRule IS triggered when it shouldn't be.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions