提交 5253c586 编写于 作者: A Andrew Casey

Stop reporting unused usings in submissions

Since they can be used in subsequent submissions, they are always
considered to be used.

Fixes #4392
上级 a5b92bbe
......@@ -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).
......
......@@ -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;"));
}
}
}
......@@ -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]
......
......@@ -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);
......
......@@ -300,5 +300,24 @@ Imports System
' With doc comments.
CreateCompilationWithMscorlib(source, parseOptions:=New VisualBasicParseOptions(documentationMode:=DocumentationMode.Diagnose)).AssertTheseDiagnostics(<errors></errors>, suppressInfos:=False)
End Sub
<Fact()>
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
<Fact()>
Public Sub UnusedImportScript()
Dim tree = Parse("Imports System", options:=TestOptions.Script)
Dim compilation = CreateCompilationWithMscorlib(tree)
compilation.AssertTheseDiagnostics(
<errors>
BC50001: Unused import statement.
Imports System
~~~~~~~~~~~~~~
</errors>, suppressInfos:=False)
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册