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 S4226 FP: Should not raise for generated class #5457

Closed
lg2de opened this issue Mar 4, 2022 · 0 comments · Fixed by #5486
Closed

Fix S4226 FP: Should not raise for generated class #5457

lg2de opened this issue Mar 4, 2022 · 0 comments · Fixed by #5486
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@lg2de
Copy link
Contributor

lg2de commented Mar 4, 2022

Description

The Rule S4226 (Extensions should be in separate namespaces) notes in the description: "It makes little sense to create an extension method when it is possible to just add that method to the class itself."

If the base class is a generated class (e.g. using xsd.exe or xsd2code) it is not "possible to just add that method to the class itself".

So, I ask to add exception for generated classes, e.g. annotated with System.CodeDom.Compiler.GeneratedCodeAttribute.

Repro steps

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract partial class ReplyWithErrorListBase {
    
    private ReplyErrorListError[] errorListField;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("Error", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
    public ReplyErrorListError[] ErrorList {
        get {
            return this.errorListField;
        }
        set {
            this.errorListField = value;
        }
    }
}

internal static class ReplyErrorExtensions
{
    public static void SetSyncLaterError(this ReplyWithErrorListBase reply) // SHOULD NOT RAISE!
    {
        reply.ErrorList = new[]
            { new ReplyErrorListError { Code = 1, Text = "foo" } };
    }
}

Expected behavior

S4226 should not raise.

Actual behavior

Warning: "Either move this extension to another namespace or move the method inside the class itself."

Known workarounds

N/A

Related information

  • C#/VB.NET Plugins version: 8.26
  • Visual Studio version: 2022
  • MSBuild / dotnet version: net472
  • Operating System: Windows 10
costin-zaharia-sonarsource added a commit that referenced this issue Mar 14, 2022
@costin-zaharia-sonarsource costin-zaharia-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Mar 14, 2022
@costin-zaharia-sonarsource costin-zaharia-sonarsource added this to the 8.37 milestone Mar 14, 2022
csaba-sagi-sonarsource pushed a commit that referenced this issue Mar 14, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S4226 should not raise for generated class Fix S4226 FP: Should not raise for generated class Mar 30, 2022
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
None yet
Development

Successfully merging a pull request may close this issue.

3 participants