Skip to content

Warn CA5392 on LibraryImport #6493

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

Merged
merged 6 commits into from
Mar 9, 2023
Merged

Warn CA5392 on LibraryImport #6493

merged 6 commits into from
Mar 9, 2023

Conversation

jtschuster
Copy link
Member

@jtschuster jtschuster commented Feb 17, 2023

Addresses part of dotnet/runtime#81939

Using DllImport without DllImportSearchPaths warns with CA5392. With generated LibraryImports that need to marshal to and from a local P/Invoke method, there is no warning because there is no DllImport on the partial method. This change warns on LibraryImports as well as DllImports, and only the Definition part of a partial method with both attributes are present.

The second part of the fix for this issue is to enforce the warning on local methods, and propagate any suppressions on the definition to the generated local P/Invoke. This would require looking at syntax nodes instead of symbols and would add a lot more complexity. If we do not warn on local methods, we wouldn't need to propagate suppression attributes to the generated local function either. I think there will be very few cases where someone would hand write a local method P/Invoke and want this warning to appear, so I'm not sure it's worth the effort of changing the analysis to use Syntax instead of Symbols, but let me know your thoughts.

@jtschuster jtschuster requested a review from a team as a code owner February 17, 2023 20:36
@jtschuster
Copy link
Member Author

cc @elinor-fung @AaronRobinsonMSFT I can't add reviewers.

@codecov
Copy link

codecov bot commented Feb 21, 2023

Codecov Report

Merging #6493 (106be2c) into main (15abfb9) will increase coverage by 0.01%.
The diff coverage is 99.69%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6493      +/-   ##
==========================================
+ Coverage   96.40%   96.41%   +0.01%     
==========================================
  Files        1371     1372       +1     
  Lines      318980   319657     +677     
  Branches    10265    10273       +8     
==========================================
+ Hits       307524   308212     +688     
+ Misses       8999     8987      -12     
- Partials     2457     2458       +1     

@jtschuster jtschuster requested review from AaronRobinsonMSFT and elinor-fung and removed request for AaronRobinsonMSFT and elinor-fung February 21, 2023 19:38
@@ -79,21 +90,25 @@ public override void Initialize(AnalysisContext context)
defaultValue: UnsafeBits);
var defaultDllImportSearchPathsAttributeOnAssembly = compilation.Assembly.GetAttributes().FirstOrDefault(o => o.AttributeClass.Equals(defaultDllImportSearchPathsAttributeTypeSymbol));

// Does not analyze local functions. To analyze local functions, we'll need to use RegisterSyntaxAction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this decision. We should update the doc to mention that local functions will not be analyzed if others are okay with this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (!symbol.IsExtern || !symbol.IsStatic)
// LibraryImport methods will be partial, and the generated implementation will have a non-null PartialDefinitionPart property
if (!symbol.IsStatic || symbol.PartialDefinitionPart != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally, this looks fine, but I think we should still check for the method being either IsExtern or IsPartialDefinition for performance. Otherwise, we're doing the more costly analysis on every static method that isn't a partial implementation.

@jtschuster
Copy link
Member Author

@dotnet/roslyn-analysis Can you take a look at this?

@jtschuster
Copy link
Member Author

@dotnet/roslyn-analysis Can you take a look and approve?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants