From 5253c586ed89a6c9c302ff60b5b4e9c1d5e2c211 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 21 Aug 2015 14:23:36 -0700 Subject: [PATCH] Stop reporting unused usings in submissions Since they can be used in subsequent submissions, they are always considered to be used. Fixes #4392 --- .../CSharp/Test/Emit/Emit/ScriptTests.cs | 3 --- .../GetUnusedImportDirectivesTests.cs | 21 +++++++++++++++++++ .../Symbol/Symbols/Source/ExternAliasTests.cs | 5 +---- .../Core/Portable/Compilation/Compilation.cs | 10 +++++++-- .../GetUnusedImportDirectivesTests.vb | 19 +++++++++++++++++ 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs b/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs index 332a339f819..1d3695345c9 100644 --- a/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs +++ b/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs @@ -582,9 +582,6 @@ public void ErrorInUsing() // (1,7): error CS0246: The type or namespace name 'Unknown' could not be found (are you missing a using directive or an assembly reference?) // using Unknown; Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "Unknown").WithArguments("Unknown").WithLocation(1, 7), - // (1,1): hidden CS8019: Unnecessary using directive. - // using Unknown; - Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using Unknown;").WithLocation(1, 1), }; // Emit produces the same diagnostics as GetDiagnostics (below). diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs index d04a41c85a9..d41f6b31653 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs @@ -358,5 +358,26 @@ public void InfoAndWarnAsError() // using System; Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;").WithWarningAsError(false)); } + + [Fact] + public void UnusedUsingInteractive() + { + var tree = Parse("using System;", options: TestOptions.Interactive); + var comp = CSharpCompilation.CreateSubmission("sub1", tree, new[] { MscorlibRef_v4_0_30316_17626 }); + + comp.VerifyDiagnostics(); + } + + [Fact] + public void UnusedUsingScript() + { + var tree = Parse("using System;", options: TestOptions.Script); + var comp = CreateCompilationWithMscorlib(tree); + + comp.VerifyDiagnostics( + // (2,1): info CS8019: Unnecessary using directive. + // using System; + Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;")); + } } } diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs index f40b70b1b51..1125fdbbf1c 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs @@ -111,10 +111,7 @@ public void ExternAliasInInteractive_Error() comp.VerifyDiagnostics( // (1,1): error CS7015: 'extern alias' is not valid in this context // extern alias Bar; - Diagnostic(ErrorCode.ERR_ExternAliasNotAllowed, "extern alias Bar;"), - // (1,1): info CS8020: Unused extern alias. - // extern alias Bar; - Diagnostic(ErrorCode.HDN_UnusedExternAlias, "extern alias Bar;")); + Diagnostic(ErrorCode.ERR_ExternAliasNotAllowed, "extern alias Bar;")); } [Fact] diff --git a/src/Compilers/Core/Portable/Compilation/Compilation.cs b/src/Compilers/Core/Portable/Compilation/Compilation.cs index 68d13ae5007..79de2b6cfae 100644 --- a/src/Compilers/Core/Portable/Compilation/Compilation.cs +++ b/src/Compilers/Core/Portable/Compilation/Compilation.cs @@ -2098,7 +2098,8 @@ internal void MarkImportDirectiveAsUsed(SyntaxNode node) internal void MarkImportDirectiveAsUsed(SyntaxTree syntaxTree, int position) { - if (syntaxTree != null) + // Optimization: Don't initialize TreeToUsedImportDirectivesMap in submissions. + if (!IsSubmission && syntaxTree != null) { var set = TreeToUsedImportDirectivesMap.GetOrAdd(syntaxTree, s_createSetCallback); set.Add(position); @@ -2107,8 +2108,13 @@ internal void MarkImportDirectiveAsUsed(SyntaxTree syntaxTree, int position) internal bool IsImportDirectiveUsed(SyntaxTree syntaxTree, int position) { - SmallConcurrentSetOfInts usedImports; + if (IsSubmission) + { + // Since usings apply to subsequent submissions, we have to assume they are used. + return true; + } + SmallConcurrentSetOfInts usedImports; return syntaxTree != null && TreeToUsedImportDirectivesMap.TryGetValue(syntaxTree, out usedImports) && usedImports.Contains(position); diff --git a/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb b/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb index f1036164554..41d8519bd35 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb @@ -300,5 +300,24 @@ Imports System ' With doc comments. CreateCompilationWithMscorlib(source, parseOptions:=New VisualBasicParseOptions(documentationMode:=DocumentationMode.Diagnose)).AssertTheseDiagnostics(, suppressInfos:=False) End Sub + + + Public Sub UnusedImportInteractive() + Dim tree = Parse("Imports System", options:=TestOptions.Interactive) + Dim compilation = VisualBasicCompilation.CreateSubmission("sub1", tree, {MscorlibRef_v4_0_30316_17626}) + compilation.AssertNoDiagnostics(suppressInfos:=False) + End Sub + + + Public Sub UnusedImportScript() + Dim tree = Parse("Imports System", options:=TestOptions.Script) + Dim compilation = CreateCompilationWithMscorlib(tree) + compilation.AssertTheseDiagnostics( + +BC50001: Unused import statement. +Imports System +~~~~~~~~~~~~~~ + , suppressInfos:=False) + End Sub End Class End Namespace -- GitLab