-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Conversation
Also silently process doc comments when parsing them so we can report unused usings. Fixes dotnet#2773
@@ -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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
roslyn/src/VisualStudio/CSharp/Test/ProjectSystemShim/LegacyProject/CSharpCompilerOptionsTests.cs
Lines 17 to 51 in 62bb55c
[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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!(haveWriter || reportDiagnosticsForCurrentTrivia || reportParameterOrTypeParameterDiagnostics)) | ||
{ | ||
continue; | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
@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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (iteration 1)
@dotnet/roslyn-compiler May I please have a second review of this number one most anticipated bug fix? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (iteration 2)
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:
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 |
@sharwell This change has no impact on those scenarios, and I do not intend to take any position on those issues. |
@dotnet/roslyn-compiler May I please have a second review? |
Also silently process doc comments when parsing them so we can report unused usings.
Fixes #2773