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

Do not report unused using/imports if not processing doc comments. #26425

Merged
merged 2 commits into from
May 3, 2018

Conversation

gafter
Copy link
Member

@gafter gafter commented Apr 26, 2018

Also silently process doc comments when parsing them so we can report unused usings.
Fixes #2773

Also silently process doc comments when parsing them so we can report unused usings.
Fixes dotnet#2773
@gafter gafter added this to the 15.8 milestone Apr 26, 2018
@gafter gafter self-assigned this Apr 26, 2018
@gafter gafter requested review from a team as code owners April 26, 2018 19:05
@@ -1890,7 +1890,7 @@ internal override void ReportUnusedImports(SyntaxTree filterTree, DiagnosticBag
cancellationToken.ThrowIfCancellationRequested();

SyntaxTree infoTree = info.Tree;
if (filterTree == null || filterTree == infoTree)
if ((filterTree == null || filterTree == infoTree) && infoTree.Options.DocumentationMode != DocumentationMode.None)
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Apr 26, 2018

Choose a reason for hiding this comment

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

just to check, this will not affect the IDE right? i.e. the user cannot select DocMode.None in the IDE?

otherwise, if they do, this change would mean they get no unused usings warnings. #Resolved

Copy link
Member

@jasonmalinowski jasonmalinowski Apr 26, 2018

Choose a reason for hiding this comment

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

Correct, the IDE has never passed .None. It has always passed Parse or Diagnose depending on what the underlying flag is, precisely for this reason if I remember correctly.

[WpfFact]
[Trait(Traits.Feature, Traits.Features.ProjectSystemShims)]
[WorkItem(530980, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530980")]
public void DocumentationModeSetToDiagnoseIfProducingDocFile()
{
using (var environment = new TestEnvironment())
{
var project = CSharpHelpers.CreateCSharpProject(environment, "Test");
project.SetOptionWithMarshaledValue(CompilerOptions.OPTID_XML_DOCFILE, "DocFile.xml");
var workspaceProject = environment.Workspace.CurrentSolution.Projects.Single();
var options = (CSharpParseOptions)workspaceProject.ParseOptions;
Assert.Equal(DocumentationMode.Diagnose, options.DocumentationMode);
}
}
[WpfFact]
[Trait(Traits.Feature, Traits.Features.ProjectSystemShims)]
[WorkItem(530980, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530980")]
public void DocumentationModeSetToParseIfNotProducingDocFile()
{
using (var environment = new TestEnvironment())
{
var project = CSharpHelpers.CreateCSharpProject(environment, "Test");
project.SetOptionWithMarshaledValue(CompilerOptions.OPTID_XML_DOCFILE, "");
var workspaceProject = environment.Workspace.CurrentSolution.Projects.Single();
var options = (CSharpParseOptions)workspaceProject.ParseOptions;
Assert.Equal(DocumentationMode.Parse, options.DocumentationMode);
}
}

#Resolved

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Apr 26, 2018

Choose a reason for hiding this comment

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

Interesting. how does this fix the bug hten? We'll continue passing along the variable. But the change i see above in neal's work still won't do the analysis. So we'll still think the usings are unused... right? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct: if the user selects DocMode.None, they will get no unused usings reported.


In reply to: 184510536 [](ancestors = 184510536)

Copy link
Member Author

Choose a reason for hiding this comment

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

We now do the analysis and drop the diagnostics on the floor.


In reply to: 184529384 [](ancestors = 184529384)

@@ -413,13 +411,6 @@ public override void DefaultVisit(Symbol symbol)

bool reportDiagnosticsForCurrentTrivia = trivia.SyntaxTree.ReportDocumentationCommentDiagnostics();
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Apr 27, 2018

Choose a reason for hiding this comment

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

is this variable needed anymore? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is used later.


In reply to: 184566009 [](ancestors = 184566009)

@@ -413,13 +411,6 @@ public override void DefaultVisit(Symbol symbol)

bool reportDiagnosticsForCurrentTrivia = trivia.SyntaxTree.ReportDocumentationCommentDiagnostics();

// If we're writing XML or we need to report diagnostics (either in this particular piece of trivia,
// or concerning undocumented [type] parameters), then we need to process this trivia node.
if (!(haveWriter || reportDiagnosticsForCurrentTrivia || reportParameterOrTypeParameterDiagnostics))
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Apr 27, 2018

Choose a reason for hiding this comment

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

can we remove reportParameterOrTypeParameterDiagnostics from the parameter list now? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Used on line 337.


In reply to: 184566077 [](ancestors = 184566077)

if (!(haveWriter || reportDiagnosticsForCurrentTrivia || reportParameterOrTypeParameterDiagnostics))
{
continue;
}
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Apr 27, 2018

Choose a reason for hiding this comment

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

Do we not need an equivalent VB change? DId it not have this problem? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

VB did not have this optimization, so part of the C# fix wasn't needed in VB.


In reply to: 184566119 [](ancestors = 184566119)

@gafter
Copy link
Member Author

gafter commented Apr 27, 2018

@dotnet/roslyn-compiler May I please have a couple of reviews of this number one most awaited bug fix?

@@ -1890,7 +1890,7 @@ internal override void ReportUnusedImports(SyntaxTree filterTree, DiagnosticBag
cancellationToken.ThrowIfCancellationRequested();

SyntaxTree infoTree = info.Tree;
if (filterTree == null || filterTree == infoTree)
if ((filterTree == null || filterTree == infoTree) && infoTree.Options.DocumentationMode != DocumentationMode.None)
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 27, 2018

Choose a reason for hiding this comment

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

infoTree.Options.DocumentationMode != DocumentationMode.None [](start = 74, length = 60)

Consider doing this check for filterTree early and bypassing the entire loop #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand this suggestion.


In reply to: 184746390 [](ancestors = 184746390)

Copy link
Contributor

@AlekseyTs AlekseyTs Apr 27, 2018

Choose a reason for hiding this comment

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

I don't understand this suggestion.

if filterTree is not null and its DocumentationMode is None, it looks like there is no reason to even doing the loop. I am suggesting to optimize for this situation.


In reply to: 184757896 [](ancestors = 184757896,184746390)

Copy link
Member Author

Choose a reason for hiding this comment

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

DocumentationMode is specific to each info.Tree.


In reply to: 184758853 [](ancestors = 184758853,184757896,184746390)

Copy link
Contributor

Choose a reason for hiding this comment

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

DocumentationMode is specific to each info.Tree.

But it looks like, if filterTree is not null, that is the only tree we are going to look at. Note, I am not suggesting to move the condition you added. I am suggesting to add an optimization in addition to what you have right now


In reply to: 184762886 [](ancestors = 184762886,184758853,184757896,184746390)

Copy link
Member Author

Choose a reason for hiding this comment

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

I still do not understand the proposed optimization. Even if filterTree is not null, there still may be zero, one, or many infos from that tree, which require a loop to iterate through.


In reply to: 184776699 [](ancestors = 184776699,184762886,184758853,184757896,184746390)

Copy link
Contributor

Choose a reason for hiding this comment

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

I still do not understand the proposed optimization. Even if filterTree is not null, there still may be zero, one, or many infos from that tree, which require a loop to iterate through.

Is this loop going to do anything useful if filterTree is not null and its DocumentationMode is None?


In reply to: 184786793 [](ancestors = 184786793,184776699,184762886,184758853,184757896,184746390)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, that's what you were suggesting. Yes, that could be done.

@@ -1585,7 +1585,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
cancellationToken.ThrowIfCancellationRequested()

Dim infoTree As SyntaxTree = info.Tree
If filterTree Is Nothing OrElse filterTree Is infoTree Then
If (filterTree Is Nothing OrElse filterTree Is infoTree) AndAlso infoTree.Options.DocumentationMode <> DocumentationMode.None Then
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 27, 2018

Choose a reason for hiding this comment

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

infoTree.Options.DocumentationMode <> DocumentationMode.None [](start = 85, length = 60)

Same suggestion as for C#. #Closed

// parsing doc comments as simple trivia; System is unnecessary
await TestInRegularAndScriptAsync(
// parsing doc comments as simple trivia; we don't know System is unnecessary
await TestMissingAsync(
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 27, 2018

Choose a reason for hiding this comment

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

TestMissingAsync [](start = 18, length = 16)

Are all three scenarios differ only by used DocumentationMode? Consider extracting extracting common parts to a method parameterized by DocumentationMode, or refactoring into a loop. #WontFix

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (iteration 1)

@gafter
Copy link
Member Author

gafter commented Apr 28, 2018

@dotnet/roslyn-compiler May I please have a second review of this number one most anticipated bug fix?

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (iteration 2)

@sharwell
Copy link
Member

The solution appears to address the worst case for this side effects of this situation, but I'm concerned about the other cases that will come up:

  • Will users be prompted to add a using if it's missing from a cref?
  • Analyzers which deal with documentation comments still won't work unless documentation comment output is enabled, which means many of the downstream issues linked to this one will not be improved by this change.

Are we just going to close those as by design (or limited by API compatibility concerns)? What do we tell analyzer authors and users about avoiding cases where analyzers are enabled but do not behave correctly? For example, consider the impact of DocumentationMode on SA1626 (Single line comments must not use documentation style slashes).

@gafter
Copy link
Member Author

gafter commented May 2, 2018

@sharwell This change has no impact on those scenarios, and I do not intend to take any position on those issues.

@gafter
Copy link
Member Author

gafter commented May 2, 2018

@dotnet/roslyn-compiler May I please have a second review?

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

Successfully merging this pull request may close these issues.

6 participants