Skip to content

Fix S1144 FP: Unused fields in class with StructLayout #6912

@sm-g

Description

@sm-g

Description

When class has StructLayout, we need all its props for memory allocation.

Repro steps

For example

public class Foo {

public void Bar(){

  var netResource = new NetResource
  {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = "foo"
  };

  _ = WNetAddConnection2(netResource, "pass", "user", 0);

}

        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);

        [StructLayout(LayoutKind.Sequential)]
        private struct NetResource
        {
            public ResourceScope Scope;
            public ResourceType ResourceType;
            public ResourceDisplaytype DisplayType;
            public int Usage;
            public string LocalName;
            public string RemoteName;
            public string Comment;
            public string Provider;
        }
}

Expected behavior

no issue

Actual behavior

S1144 on not used props of NetResource struct

Related information

SonarAnalyzer.CSharp 8.54.0.64047

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