You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove redundant configuration from [DllImport] declaration #33808
E.g. [DllImport(..., SetLastError = false)] or [DllImport(..., CharSet = CharSet.Ansi)] (etc) are pointless because those are already the default values.
I'm curious about PreserveSig: it's a boolean, but the document says true is its default. Where is it set? I don't see any logic setting it to true in the ref file or in the src file.
Note that DllImportAttribute is one of a number of pseudo-attributes. It's actually understood by the compiler and emitted as special metadata. So Roslyn is really where you should be looking for the defaults.
Should this analyzer also apply to the new GeneratedDllImportAttribute? Unlike DllImportAttribute, this new class uses properties, but they are similarly named.
Suggested severity: Info
Suggested category: Style
Suggested milestone: Future
These are all the public fields in the DllImportAttribute class, and their documented default value, which the analyzer should flag:
publicsealedpartialclassDllImportAttribute:System.Attribute{// true by default// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.bestfitmapping?view=net-6.0#remarkspublicboolBestFitMapping;// The default value is Winapi on Windows, and Cdecl on all other platforms.// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.callingconvention?view=net-6.0#remarkspublicSystem.Runtime.InteropServices.CallingConventionCallingConvention;// If DefaultCharSetAttribute is applied on the module in C# or Visual Basic code,// then the C# or Visual Basic compiler will emit the provided CharSet by default instead of using CharSet.None.// F# does not support the DefaultCharSetAttribute, and always emits CharSet.None by default.// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/charsetpublicSystem.Runtime.InteropServices.CharSetCharSet;// Optional API name, the analyzer should ignore this fieldpublicstring?EntryPoint;// false by default// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settingspublicboolExactSpelling;// true by default// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settingspublicboolPreserveSig;// false by default// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settingspublicboolSetLastError;// false by default// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.throwonunmappablechar?view=net-6.0#remarkspublicboolThrowOnUnmappableChar;// This is mandatory, the analyzer should ignore this fieldpublicstringValue{get;}}
If we want to do the same with GenerateDllImport, then these are the properties exposed by this attribute:
We're not convinced that over-specification is a bad practice; these are expert-level APIs and many experts like being explicit instead of relying on defaults.
However, there is value in P/Invoke analyzers, which, could flag when people are calling Unicode APIs (*W) but specify ANSI encoding, for instance.
Let's close this one for now. If there are other suggestions, we can open new issues.
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
carlossanlop commentedon Dec 7, 2021
@terrajobst can you please clarify what you meant in the description? What does it mean "an attribute is set on a DllImport"?
cc @AaronRobinsonMSFT since you linked #34831 and maybe you understand what the description means.
alexrp commentedon Dec 7, 2021
E.g.
[DllImport(..., SetLastError = false)]or[DllImport(..., CharSet = CharSet.Ansi)](etc) are pointless because those are already the default values.AaronRobinsonMSFT commentedon Dec 7, 2021
@carlossanlop the response from @alexrp is what I interpreted as the intent of this issue.
carlossanlop commentedon Dec 7, 2021
Ok thanks. Then these are the default values set for the public fields of the
DllImportAttributeclass: https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settingsI'm curious about
PreserveSig: it's a boolean, but the document saystrueis its default. Where is it set? I don't see any logic setting it totruein the ref file or in the src file.alexrp commentedon Dec 7, 2021
Note that
DllImportAttributeis one of a number of pseudo-attributes. It's actually understood by the compiler and emitted as special metadata. So Roslyn is really where you should be looking for the defaults.Example: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAGAAkIEYA6AJQFcA7DbAWxioEkmYoIAHAMo8AbtjAwAzgG4AsAChCAZnIAmUgGFSAb3mk9pANoARXLg4s+0DAAoARLmzAAZhAi20pAAqwJImAOwAc1IAXlIMKDoYAEoAXV19JXIKJFIYBAweBnIUUhcIa2jZOQBfIA
AaronRobinsonMSFT commentedon Dec 7, 2021
@carlossanlop The
PreserveSigis a pseudo-attribute and is encoded directly in metadata. See section II.23.1.11 of ECMA-335.carlossanlop commentedon Dec 7, 2021
Thanks.
Should this analyzer also apply to the new
GeneratedDllImportAttribute? UnlikeDllImportAttribute, this new class uses properties, but they are similarly named.carlossanlop commentedon Dec 7, 2021
Suggested severity: Info
Suggested category: Style
Suggested milestone: Future
These are all the public fields in the
DllImportAttributeclass, and their documented default value, which the analyzer should flag:If we want to do the same with
GenerateDllImport, then these are the properties exposed by this attribute:terrajobst commentedon Dec 14, 2021
Video