diff --git a/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs b/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs index 4f2976d01d9f3..896ec474d22a7 100644 --- a/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs +++ b/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs @@ -1869,6 +1869,22 @@ static void Main(string[] args) }"); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryImports)] + [WorkItem(1323, "https://github.com/dotnet/roslyn/issues/1323")] + public async Task TestUsingsInPPRegionWithoutOtherMembers() + { + await VerifyCS.VerifyCodeFixAsync( +@" +#if true +[|{|IDE0005:using System;|}|] +#endif +", +@" +#if true +#endif +"); + } + [Theory] [InlineData(0)] [InlineData(1)] diff --git a/src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.vb b/src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.vb index 9829aea9793cc..3c1a0f3a09889 100644 --- a/src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.vb +++ b/src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.vb @@ -1192,8 +1192,22 @@ Namespace N End Namespace]]>.NormalizedValue) End Function - + Public Async Function TestImportsInPPRegionWithoutOtherMembers() As Task + Await TestInRegularAndScript1Async( +"[| +#If True +Imports System +#End If +|]", +" +#If True +#End If +") + End Function + + + Public Async Function TestMissingOnSynthesizedEventType() As Task Await TestMissingInRegularAndScriptAsync( "[|Class C diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.Rewriter.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.Rewriter.cs index 87c00ef11ed5c..ebacb57d65724 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.Rewriter.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.Rewriter.cs @@ -129,7 +129,7 @@ public override SyntaxNode VisitCompilationUnit(CompilationUnitSyntax node) // follows the usings. if (finalTrivia.Count > 0) { - var nextToken = compilationUnit.Usings.Last().GetLastToken().GetNextToken(); + var nextToken = compilationUnit.Usings.Last().GetLastToken().GetNextTokenOrEndOfFile(); compilationUnit = compilationUnit.ReplaceToken(nextToken, nextToken.WithPrependedLeadingTrivia(finalTrivia)); } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.Rewriter.vb b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.Rewriter.vb index e273c9db81a67..d04c47dba064e 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.Rewriter.vb +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.Rewriter.vb @@ -91,7 +91,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnnecessaryImports Dim newImports = SyntaxFactory.List(oldImports.WhereNotNull()) If remainingTrivia.Count > 0 Then - Dim nextToken = compilationUnit.Imports.Last().GetLastToken().GetNextToken() + Dim nextToken = compilationUnit.Imports.Last().GetLastToken().GetNextTokenOrEndOfFile() compilationUnit = compilationUnit.ReplaceToken(nextToken, nextToken.WithPrependedLeadingTrivia(remainingTrivia)) End If