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 more diagnostics with CA2263 #100490

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal static ComTypes.TYPEATTR GetTypeAttrForTypeInfo(ComTypes.ITypeInfo type

try
{
return (ComTypes.TYPEATTR)Marshal.PtrToStructure(pAttrs, typeof(ComTypes.TYPEATTR));
return Marshal.PtrToStructure<ComTypes.TYPEATTR>(pAttrs);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}
finally
{
Expand All @@ -200,7 +200,7 @@ internal static ComTypes.TYPELIBATTR GetTypeAttrForTypeLib(ComTypes.ITypeLib typ

try
{
return (ComTypes.TYPELIBATTR)Marshal.PtrToStructure(pAttrs, typeof(ComTypes.TYPELIBATTR));
return Marshal.PtrToStructure<ComTypes.TYPELIBATTR>(pAttrs);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal ComTypeEnumDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc

try
{
varDesc = (ComTypes.VARDESC)Marshal.PtrToStructure(p, typeof(ComTypes.VARDESC));
varDesc = Marshal.PtrToStructure<ComTypes.VARDESC>(p);

if (varDesc.varkind == ComTypes.VARKIND.VAR_CONST)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal struct ExcepInfo
#if DEBUG
static ExcepInfo()
{
Debug.Assert(Marshal.SizeOf(typeof(ExcepInfo)) == Marshal.SizeOf(typeof(ComTypes.EXCEPINFO)));
Debug.Assert(Marshal.SizeOf<ExcepInfo>() == Marshal.SizeOf<ComTypes.EXCEPINFO>());
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private static void GetFuncDescForDescIndex(ComTypes.ITypeInfo typeInfo, int fun
throw Error.CannotRetrieveTypeInformation();
}

funcDesc = (ComTypes.FUNCDESC)Marshal.PtrToStructure(pFuncDesc, typeof(ComTypes.FUNCDESC));
funcDesc = Marshal.PtrToStructure<ComTypes.FUNCDESC>(pFuncDesc);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
funcDescHandle = pFuncDesc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ private static void SetParameterDataForMethProp(MethodOrPropertySymbol methProp,
if (parameters.Length > 0)
{
// See if we have a param array.
if (parameters[parameters.Length - 1].GetCustomAttribute(typeof(ParamArrayAttribute), false) != null)
if (parameters[parameters.Length - 1].GetCustomAttribute<ParamArrayAttribute>(false) != null)
{
methProp.isParamArray = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static void TryAddKeyedTransient(
{
ThrowHelper.ThrowIfNull(collection);

TryAddKeyedTransient(collection, typeof(TService), serviceKey, typeof(TService));
TryAddKeyedTransient<TService, TService>(collection, serviceKey);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down Expand Up @@ -202,7 +202,7 @@ public static void TryAddKeyedScoped(
{
ThrowHelper.ThrowIfNull(collection);

TryAddKeyedScoped(collection, typeof(TService), serviceKey, typeof(TService));
TryAddKeyedScoped<TService, TService>(collection, serviceKey);
}

/// <summary>
Expand Down Expand Up @@ -318,7 +318,7 @@ public static void TryAddKeyedSingleton(
{
ThrowHelper.ThrowIfNull(collection);

TryAddKeyedSingleton(collection, typeof(TService), serviceKey, typeof(TService));
TryAddKeyedSingleton<TService, TService>(collection, serviceKey);
}

/// <summary>
Expand Down Expand Up @@ -354,7 +354,7 @@ public static void TryAddKeyedSingleton<TService>(this IServiceCollection collec
ThrowHelper.ThrowIfNull(collection);
ThrowHelper.ThrowIfNull(instance);

var descriptor = ServiceDescriptor.KeyedSingleton(serviceType: typeof(TService), serviceKey, implementationInstance: instance);
var descriptor = ServiceDescriptor.KeyedSingleton(serviceKey, implementationInstance: instance);
ServiceCollectionDescriptorExtensions.TryAdd(collection, descriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static void TryAddTransient(
{
ThrowHelper.ThrowIfNull(collection);

TryAddTransient(collection, typeof(TService), typeof(TService));
TryAddTransient<TService, TService>(collection);
}

/// <summary>
Expand Down Expand Up @@ -272,7 +272,7 @@ public static void TryAddScoped(
{
ThrowHelper.ThrowIfNull(collection);

TryAddScoped(collection, typeof(TService), typeof(TService));
TryAddScoped<TService, TService>(collection);
}

/// <summary>
Expand Down Expand Up @@ -378,7 +378,7 @@ public static void TryAddSingleton(
{
ThrowHelper.ThrowIfNull(collection);

TryAddSingleton(collection, typeof(TService), typeof(TService));
TryAddSingleton<TService, TService>(collection);
}

/// <summary>
Expand Down Expand Up @@ -412,7 +412,7 @@ public static void TryAddSingleton<TService>(this IServiceCollection collection,
ThrowHelper.ThrowIfNull(collection);
ThrowHelper.ThrowIfNull(instance);

var descriptor = ServiceDescriptor.Singleton(serviceType: typeof(TService), implementationInstance: instance);
var descriptor = ServiceDescriptor.Singleton(implementationInstance: instance);
TryAdd(collection, descriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static IServiceCollection AddKeyedTransient<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddKeyedTransient(typeof(TService), serviceKey, implementationFactory);
return services.AddKeyedTransient<TService>(serviceKey, implementationFactory);
}

/// <summary>
Expand Down Expand Up @@ -324,7 +324,7 @@ public static IServiceCollection AddKeyedScoped<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddKeyedScoped(typeof(TService), serviceKey, implementationFactory);
return services.AddKeyedScoped<TService>(serviceKey, implementationFactory);
}


Expand Down Expand Up @@ -434,7 +434,7 @@ public static IServiceCollection AddKeyedSingleton(
{
ThrowHelper.ThrowIfNull(services);

return services.AddKeyedSingleton(typeof(TService), serviceKey, typeof(TService));
return services.AddKeyedSingleton<TService, TService>(serviceKey);
}

/// <summary>
Expand Down Expand Up @@ -483,7 +483,7 @@ public static IServiceCollection AddKeyedSingleton<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddKeyedSingleton(typeof(TService), serviceKey, implementationFactory);
return services.AddKeyedSingleton<TService>(serviceKey, implementationFactory);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static IServiceCollection AddTransient<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddTransient(typeof(TService), implementationFactory);
return services.AddTransient<TService>(implementationFactory);
}

/// <summary>
Expand Down Expand Up @@ -292,7 +292,7 @@ public static IServiceCollection AddScoped<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddScoped(typeof(TService), implementationFactory);
return services.AddScoped<TService>(implementationFactory);
}


Expand Down Expand Up @@ -435,7 +435,7 @@ public static IServiceCollection AddSingleton<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddSingleton(typeof(TService), implementationFactory);
return services.AddSingleton<TService>(implementationFactory);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ Namespace Microsoft.VisualBasic.FileIO
Private Shared Sub CopyOrMoveDirectory(ByVal operation As CopyOrMove,
ByVal sourceDirectoryName As String, ByVal destinationDirectoryName As String,
ByVal overwrite As Boolean, ByVal showUI As UIOptionInternal, ByVal onUserCancel As UICancelOption)
Debug.Assert(System.Enum.IsDefined(GetType(CopyOrMove), operation), "Invalid Operation")
Debug.Assert([Enum].IsDefined(operation), "Invalid Operation")

' Verify enums.
VerifyUICancelOption("onUserCancel", onUserCancel)
Expand Down Expand Up @@ -961,7 +961,7 @@ Namespace Microsoft.VisualBasic.FileIO
Private Shared Sub FxCopyOrMoveDirectory(ByVal operation As CopyOrMove,
ByVal sourceDirectoryPath As String, ByVal targetDirectoryPath As String, ByVal overwrite As Boolean)

Debug.Assert(System.Enum.IsDefined(GetType(CopyOrMove), operation), "Invalid Operation")
Debug.Assert([Enum].IsDefined(operation), "Invalid Operation")
Debug.Assert(sourceDirectoryPath <> "" And IO.Path.IsPathRooted(sourceDirectoryPath), "Invalid Source")
Debug.Assert(targetDirectoryPath <> "" And IO.Path.IsPathRooted(targetDirectoryPath), "Invalid Target")

Expand Down Expand Up @@ -1010,7 +1010,7 @@ Namespace Microsoft.VisualBasic.FileIO
Private Shared Sub CopyOrMoveDirectoryNode(ByVal Operation As CopyOrMove,
ByVal SourceDirectoryNode As DirectoryNode, ByVal Overwrite As Boolean, ByVal Exceptions As ListDictionary)

Debug.Assert(System.Enum.IsDefined(GetType(CopyOrMove), Operation), "Invalid Operation")
Debug.Assert([Enum].IsDefined(Operation), "Invalid Operation")
Debug.Assert(Exceptions IsNot Nothing, "Null exception list")
Debug.Assert(SourceDirectoryNode IsNot Nothing, "Null source node")

Expand Down Expand Up @@ -1092,7 +1092,7 @@ Namespace Microsoft.VisualBasic.FileIO
ByVal sourceFileName As String, ByVal destinationFileName As String,
ByVal overwrite As Boolean, ByVal showUI As UIOptionInternal, ByVal onUserCancel As UICancelOption
)
Debug.Assert(System.Enum.IsDefined(GetType(CopyOrMove), operation), "Invalid Operation")
Debug.Assert([Enum].IsDefined(operation), "Invalid Operation")

' Verify enums.
VerifyUICancelOption("onUserCancel", onUserCancel)
Expand Down Expand Up @@ -1597,8 +1597,8 @@ Namespace Microsoft.VisualBasic.FileIO
''' </remarks>
Private Shared Sub ShellCopyOrMove(ByVal Operation As CopyOrMove, ByVal TargetType As FileOrDirectory,
ByVal FullSourcePath As String, ByVal FullTargetPath As String, ByVal ShowUI As UIOptionInternal, ByVal OnUserCancel As UICancelOption)
Debug.Assert(System.Enum.IsDefined(GetType(CopyOrMove), Operation))
Debug.Assert(System.Enum.IsDefined(GetType(FileOrDirectory), TargetType))
Debug.Assert([Enum].IsDefined(Operation))
Debug.Assert([Enum].IsDefined(TargetType))
Debug.Assert(FullSourcePath <> "" And IO.Path.IsPathRooted(FullSourcePath), "Invalid FullSourcePath")
Debug.Assert(FullTargetPath <> "" And IO.Path.IsPathRooted(FullTargetPath), "Invalid FullTargetPath")
Debug.Assert(ShowUI <> UIOptionInternal.NoUI, "Why call ShellDelete if ShowUI is NoUI???")
Expand Down Expand Up @@ -1693,7 +1693,7 @@ Namespace Microsoft.VisualBasic.FileIO
Private Shared Sub ShellFileOperation(ByVal OperationType As SHFileOperationType, ByVal OperationFlags As ShFileOperationFlags,
ByVal FullSource As String, ByVal FullTarget As String, ByVal OnUserCancel As UICancelOption, ByVal FileOrDirectory As FileOrDirectory)

Debug.Assert(System.Enum.IsDefined(GetType(SHFileOperationType), OperationType))
Debug.Assert([Enum].IsDefined(OperationType))
Debug.Assert(OperationType <> SHFileOperationType.FO_RENAME, "Don't call Shell to rename")
Debug.Assert(FullSource <> "" And IO.Path.IsPathRooted(FullSource), "Invalid FullSource path")
Debug.Assert(OperationType = SHFileOperationType.FO_DELETE OrElse (FullTarget <> "" And IO.Path.IsPathRooted(FullTarget)), "Invalid FullTarget path")
Expand Down Expand Up @@ -1750,7 +1750,7 @@ Namespace Microsoft.VisualBasic.FileIO
Private Shared Function GetShellOperationInfo(
ByVal OperationType As SHFileOperationType, ByVal OperationFlags As ShFileOperationFlags,
ByVal SourcePaths() As String, Optional ByVal TargetPath As String = Nothing) As SHFILEOPSTRUCT
Debug.Assert(System.Enum.IsDefined(GetType(SHFileOperationType), OperationType), "Invalid OperationType")
Debug.Assert([Enum].IsDefined(OperationType), "Invalid OperationType")
Debug.Assert(TargetPath = "" Or IO.Path.IsPathRooted(TargetPath), "Invalid TargetPath")
Debug.Assert(SourcePaths IsNot Nothing AndAlso SourcePaths.Length > 0, "Invalid SourcePaths")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public virtual void EndNew(int itemIndex)
{
// Allow event handler to supply the new item for us
// If event handler did not supply new item, create one ourselves
object? newItem = FireAddingNew() ?? Activator.CreateInstance(typeof(T));
object? newItem = FireAddingNew() ?? Activator.CreateInstance<T>();

// Add item to end of list. Note: If event handler returned an item not of type T,
// the cast below will trigger an InvalidCastException. This is by design.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ private void TranslateSids(string target, IntPtr[] pSids)

for (int i = 0; i < domainCount; i++)
{
domains[i] = (Interop.LSA_TRUST_INFORMATION)Marshal.PtrToStructure(pCurrentDomain, typeof(Interop.LSA_TRUST_INFORMATION));
pCurrentDomain = new IntPtr(pCurrentDomain.ToInt64() + Marshal.SizeOf(typeof(Interop.LSA_TRUST_INFORMATION)));
domains[i] = Marshal.PtrToStructure<Interop.LSA_TRUST_INFORMATION>(pCurrentDomain);
pCurrentDomain = new IntPtr(pCurrentDomain.ToInt64() + Marshal.SizeOf<Interop.LSA_TRUST_INFORMATION>());
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}

GlobalDebug.WriteLineIf(GlobalDebug.Info, "AuthZSet", "SidList: got {0} groups in {1} domains", sidCount, domainCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ out pClientContext

// Extract TOKEN_GROUPS.GroupCount

Interop.TOKEN_GROUPS tokenGroups = (Interop.TOKEN_GROUPS)Marshal.PtrToStructure(pBuffer, typeof(Interop.TOKEN_GROUPS));
Interop.TOKEN_GROUPS tokenGroups = Marshal.PtrToStructure<Interop.TOKEN_GROUPS>(pBuffer);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

uint groupCount = tokenGroups.GroupCount;

Expand All @@ -141,13 +141,13 @@ out pClientContext
// each native SID_AND_ATTRIBUTES into a managed SID_AND_ATTR.
Interop.SID_AND_ATTRIBUTES[] groups = new Interop.SID_AND_ATTRIBUTES[groupCount];

IntPtr currentItem = new IntPtr(pBuffer.ToInt64() + Marshal.SizeOf(typeof(Interop.TOKEN_GROUPS)) - sizeof(Interop.SID_AND_ATTRIBUTES));
IntPtr currentItem = new IntPtr(pBuffer.ToInt64() + Marshal.SizeOf<Interop.TOKEN_GROUPS>() - sizeof(Interop.SID_AND_ATTRIBUTES));
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 0; i < groupCount; i++)
{
groups[i] = (Interop.SID_AND_ATTRIBUTES)Marshal.PtrToStructure(currentItem, typeof(Interop.SID_AND_ATTRIBUTES));
groups[i] = Marshal.PtrToStructure<Interop.SID_AND_ATTRIBUTES>(currentItem);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

currentItem = new IntPtr(currentItem.ToInt64() + Marshal.SizeOf(typeof(Interop.SID_AND_ATTRIBUTES)));
currentItem = new IntPtr(currentItem.ToInt64() + Marshal.SizeOf<Interop.SID_AND_ATTRIBUTES>());
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}

_groupSidList = new SidList(groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ private void LoadComputerInfo()
if (err == 0)
{
UnsafeNativeMethods.WKSTA_INFO_100 wkstaInfo =
(UnsafeNativeMethods.WKSTA_INFO_100)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.WKSTA_INFO_100));
Marshal.PtrToStructure<UnsafeNativeMethods.WKSTA_INFO_100>(buffer);

_machineFlatName = wkstaInfo.wki100_computername;
GlobalDebug.WriteLineIf(GlobalDebug.Info, "SAMStoreCtx", "LoadComputerInfo: machineFlatName={0}", _machineFlatName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ internal static SidType ClassifySID(IntPtr pSid)
IntPtr pIdentAuth = Interop.Advapi32.GetSidIdentifierAuthority(pSid);

Interop.Advapi32.SID_IDENTIFIER_AUTHORITY identAuth =
(Interop.Advapi32.SID_IDENTIFIER_AUTHORITY)Marshal.PtrToStructure(pIdentAuth, typeof(Interop.Advapi32.SID_IDENTIFIER_AUTHORITY));
Marshal.PtrToStructure<Interop.Advapi32.SID_IDENTIFIER_AUTHORITY>(pIdentAuth);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

IntPtr pRid = Interop.Advapi32.GetSidSubAuthority(pSid, 0);
int rid = Marshal.ReadInt32(pRid);
Expand Down Expand Up @@ -425,7 +425,7 @@ out tokenHandle
}

// Retrieve the user's SID from the user info
Interop.TOKEN_USER tokenUser = (Interop.TOKEN_USER)Marshal.PtrToStructure(pBuffer, typeof(Interop.TOKEN_USER));
Interop.TOKEN_USER tokenUser = Marshal.PtrToStructure<Interop.TOKEN_USER>(pBuffer);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
IntPtr pUserSid = tokenUser.sidAndAttributes.Sid; // this is a reference into the NATIVE memory (into pBuffer)

Debug.Assert(Interop.Advapi32.IsValidSid(pUserSid));
Expand Down Expand Up @@ -496,8 +496,7 @@ internal static IntPtr GetMachineDomainSid()
}

Debug.Assert(pBuffer != IntPtr.Zero);
UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO info = (UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO)
Marshal.PtrToStructure(pBuffer, typeof(UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO));
UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO info = Marshal.PtrToStructure<UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO>(pBuffer);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

Debug.Assert(Interop.Advapi32.IsValidSid(info.DomainSid));

Expand Down Expand Up @@ -570,7 +569,7 @@ internal static UnsafeNativeMethods.DomainControllerInfo GetDcName(string comput
}

UnsafeNativeMethods.DomainControllerInfo domainControllerInfo =
(UnsafeNativeMethods.DomainControllerInfo)Marshal.PtrToStructure(domainControllerInfoPtr, typeof(UnsafeNativeMethods.DomainControllerInfo));
Marshal.PtrToStructure<UnsafeNativeMethods.DomainControllerInfo>(domainControllerInfoPtr);

return domainControllerInfo;
}
Expand Down Expand Up @@ -802,7 +801,7 @@ internal static bool IsMachineDC(string computerName)
}

UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC dsRolePrimaryDomainInfo =
(UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC)Marshal.PtrToStructure(dsRoleInfoPtr, typeof(UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
Marshal.PtrToStructure<UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC>(dsRoleInfoPtr);

return (dsRolePrimaryDomainInfo.MachineRole == UnsafeNativeMethods.DSROLE_MACHINE_ROLE.DsRole_RoleBackupDomainController ||
dsRolePrimaryDomainInfo.MachineRole == UnsafeNativeMethods.DSROLE_MACHINE_ROLE.DsRole_RolePrimaryDomainController);
Expand Down
Loading
Loading