-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1883,14 +1883,14 @@ private void CompleteTree(SyntaxTree tree) | |
|
||
internal override void ReportUnusedImports(SyntaxTree filterTree, DiagnosticBag diagnostics, CancellationToken cancellationToken) | ||
{ | ||
if (_lazyImportInfos != null) | ||
if (_lazyImportInfos != null && filterTree?.Options.DocumentationMode != DocumentationMode.None) | ||
{ | ||
foreach (ImportInfo info in _lazyImportInfos) | ||
{ | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
if In reply to: 184757896 [](ancestors = 184757896,184746390) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In reply to: 184758853 [](ancestors = 184758853,184757896,184746390) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But it looks like, if In reply to: 184762886 [](ancestors = 184762886,184758853,184757896,184746390) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Is this loop going to do anything useful if In reply to: 184786793 [](ancestors = 184786793,184776699,184762886,184758853,184757896,184746390) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that's what you were suggesting. Yes, that could be done. |
||
{ | ||
TextSpan infoSpan = info.Span; | ||
if (!this.IsImportDirectiveUsed(infoTree, infoSpan.Start)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,8 +392,6 @@ private bool TryProcessDocumentationCommentTriviaNodes( | |
{ | ||
Debug.Assert(!docCommentNodes.IsDefaultOrEmpty); | ||
|
||
bool haveWriter = _writer != null; | ||
|
||
bool processedDocComment = false; // Even if there are DocumentationCommentTriviaSyntax, we may not need to process any of them. | ||
|
||
ArrayBuilder<CSharpSyntaxNode> includeElementNodesBuilder = null; | ||
|
@@ -413,13 +411,6 @@ private bool TryProcessDocumentationCommentTriviaNodes( | |
|
||
bool reportDiagnosticsForCurrentTrivia = trivia.SyntaxTree.ReportDocumentationCommentDiagnostics(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
continue; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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) |
||
|
||
if (!processedDocComment) | ||
{ | ||
// Since we have to throw away all the parts if any part is bad, we need to write to an intermediate temp. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1578,14 +1578,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic | |
End Property | ||
|
||
Friend Overrides Sub ReportUnusedImports(filterTree As SyntaxTree, diagnostics As DiagnosticBag, cancellationToken As CancellationToken) | ||
If _lazyImportInfos IsNot Nothing Then | ||
If _lazyImportInfos IsNot Nothing AndAlso (filterTree Is Nothing OrElse filterTree.Options.DocumentationMode <> DocumentationMode.None) Then | ||
Dim unusedBuilder As ArrayBuilder(Of TextSpan) = Nothing | ||
|
||
For Each info As ImportInfo In _lazyImportInfos | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Same suggestion as for C#. #Closed |
||
Dim clauseSpans = info.ClauseSpans | ||
Dim numClauseSpans = clauseSpans.Length | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1307,19 +1307,23 @@ public static void Main() | |
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryImports)] | ||
public async Task TestReferenceInCref() | ||
{ | ||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more.
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 |
||
@"[|using System; | ||
/// <summary><see cref=""String"" /></summary> | ||
class C | ||
{ | ||
}|]", | ||
@"/// <summary><see cref=""String"" /></summary> | ||
}|]", new TestParameters(Options.Regular.WithDocumentationMode(DocumentationMode.None))); | ||
|
||
// fully parsing doc comments; System is necessary | ||
await TestMissingAsync( | ||
@"[|using System; | ||
/// <summary><see cref=""String"" /></summary> | ||
class C | ||
{ | ||
}"); | ||
}|]", new TestParameters(Options.Regular.WithDocumentationMode(DocumentationMode.Parse))); | ||
|
||
// fully parsing doc comments; System is necessary | ||
// fully parsing and diagnosing doc comments; System is necessary | ||
await TestMissingAsync( | ||
@"[|using System; | ||
/// <summary><see cref=""String"" /></summary> | ||
|
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
#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)