Fix S1144 FP: Unused fields in class with StructLayout when struct is in deep hierarchy class
Closed this issue · 1 comments
tkouba commented
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 commented
Thank you for reporting this. Confirmed as False Positive.