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 S1144 FP: Unused fields in class with StructLayout when struct is in deep hierarchy class #9432

Closed
tkouba opened this issue Jun 14, 2024 · 1 comment · Fixed by #9441
Closed
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@tkouba
Copy link

tkouba commented Jun 14, 2024

Description

When class has StructLayout, we need all its props for memory allocation. Even if its embedded in deep class hierarchy. Similar to issue #6912, but fix is not working in edge cases.

Repro steps

For example:

    partial class WindowsCredentialService
    {
        class NativeMethods
        {
            [StructLayout(LayoutKind.Sequential)]
            internal struct Credential
            {
                public int Flags;
                public int Type;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string TargetName;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string Comment;
                public long LastWritten;
                public int CredentialBlobSize;
                public IntPtr CredentialBlob;
                public int Persist;
                public int AttributeCount;
                public IntPtr Attributes;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string TargetAlias;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string UserName;
            }

Expected behavior

No issue

Actual behavior

S1144 on not used props of Credential struct

Known workarounds

Move struct in class hierarchy:

    partial class WindowsCredentialService
    {
	[StructLayout(LayoutKind.Sequential)]
	internal struct Credential
	{
		public int Flags;
		public int Type;
		[MarshalAs(UnmanagedType.LPWStr)]
		public string TargetName;
		[MarshalAs(UnmanagedType.LPWStr)]
		public string Comment;
		public long LastWritten;
		public int CredentialBlobSize;
		public IntPtr CredentialBlob;
		public int Persist;
		public int AttributeCount;
		public IntPtr Attributes;
		[MarshalAs(UnmanagedType.LPWStr)]
		public string TargetAlias;
		[MarshalAs(UnmanagedType.LPWStr)]
		public string UserName;
	}
		
        class NativeMethods
        {

Related information

  • SonarLint for VisualStudio version 8.0.0.92083
  • Visual Studio 2022 version 17.9.6
  • dotnet version 8
@zsolt-kolbay-sonarsource
Copy link
Contributor

Thank you for reporting this. Confirmed as False Positive.

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