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

Update VSMEF001PropertyMustHaveSetter.cs #345

Merged
merged 4 commits into from Nov 7, 2022

Conversation

Youssef1313
Copy link
Contributor

No description provided.

@@ -40,25 +40,25 @@ public class VSMEF001PropertyMustHaveSetter : DiagnosticAnalyzer
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the intent is to report diagnostics in generated code, then it should be Analyze | ReportDiagnostics

Copy link
Member

Choose a reason for hiding this comment

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

This strikes me as a bug fix then, is that correct? Is the idea that without this extra flag, we won't report diagnostics on generated code, despite the existing flag we set? It seems odd that ReportDiagnostics would be broken if used by itself. Is there documentation or other bugs that I can research to learn more about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AArnott To my understanding, the presence of Analyze determines whether or not you get callbacks in generated code, and the presence of ReportDiagnostics determines whether or not a reported diagnostic in generated code will be suppressed.

If you want to treat generated code the same as non-generated code (which appears to be the original intent here), you specify both.

There are cases where it's useful to only use Analyze, for example, consider an analyzer that reports unused methods, and you don't want to report diagnostics in generated code.

  • Analyze | ReportDiagnostic: will not work because it reports diagnostics in generated code.
  • None: will not work, a non-generated method may only be called in generated code. You'll report it as unused while it's used by generated code.
  • Analyze gives the correct behavior. You don't report unused methods in generated code, but you get callbacks for generated code so that you know whether a non-generated method is used by generated code.

I don't know if ReportDiagnostics alone is ever useful.

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if ReportDiagnostics alone is ever useful.

This is the crux of my question. The flag isn't documented as broken without Analyze also specified. It seems unlikely or impossible to report diagnostics on code you haven't analyzed. So why would ReportDiagnostics not imply (or even bitwise-OR in) the Analyze flag as well?
@CyrusNajmabadi can you advise?

Choose a reason for hiding this comment

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

I cannot. But @mavasani definitely can :)

Copy link
Member

Choose a reason for hiding this comment

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

It seems unlikely or impossible to report diagnostics on code you haven't analyzed

You definitely can do so. For example, if you have a CompilationEnd analyzer that wants to analyze only non-generated code, but report diagnostics anywhere in the compilation (including in generated code), then specifying only ReportDiagnostics would be required. I agree this is not a common analyzer configuration, but is theoretically possible.

We introduced GeneratedCodeAnalysisFlags as a Flags enum to provide complete flexibility to analyzer authors on whether or not to receive callbacks and/or report diagnostics for generated code.

@codecov-commenter
Copy link

codecov-commenter commented Nov 5, 2022

Codecov Report

Merging #345 (be05d75) into main (b655a40) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #345   +/-   ##
=======================================
  Coverage   76.17%   76.17%           
=======================================
  Files         110      110           
  Lines        7194     7194           
=======================================
  Hits         5480     5480           
  Misses       1714     1714           
Impacted Files Coverage Δ
...sition.Analyzers/VSMEF001PropertyMustHaveSetter.cs 97.14% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@AArnott AArnott left a comment

Choose a reason for hiding this comment

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

Thanks for your submission. However without a bug filed or PR description that explains how, why, etc. of your change, it's very difficult to know why we should merge this PR.
The only meaningful change appears to be related to the flags. If that's the only functional change, all other irrelevant changes should be reverted and the PR description set to explain the problem and why this is the appropriate solution.
If you can update the PR to fit this, I'll be happy to take another look.

@@ -14,7 +14,7 @@ namespace Microsoft.VisualStudio.Composition.Analyzers
/// Creates a diagnostic when `[Import]` is applied to a property with no setter.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class VSMEF001PropertyMustHaveSetter : DiagnosticAnalyzer
public sealed class VSMEF001PropertyMustHaveSetter : DiagnosticAnalyzer
Copy link
Member

Choose a reason for hiding this comment

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

Making this change to just one class deviates this class from the norm for this project.

Aside: sealing classes is way overdone, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AArnott I don't see any other analyzers in the repo. Can you point to them? I can update them all if you want (whether in this PR or a separate PR)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, my mistake then. Most of our analyzer projects have many analyzers. But we don't mark any of them as sealed. Unless there's a compelling reason to do so, please revert this change.

@@ -40,25 +40,25 @@ public class VSMEF001PropertyMustHaveSetter : DiagnosticAnalyzer
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
Copy link
Member

Choose a reason for hiding this comment

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

This strikes me as a bug fix then, is that correct? Is the idea that without this extra flag, we won't report diagnostics on generated code, despite the existing flag we set? It seems odd that ReportDiagnostics would be broken if used by itself. Is there documentation or other bugs that I can research to learn more about this?

Copy link
Member

@AArnott AArnott left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@AArnott AArnott merged commit f2baa1e into microsoft:main Nov 7, 2022
@Youssef1313 Youssef1313 deleted the patch-1 branch November 7, 2022 22:37
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.

None yet

5 participants