-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25875 from Neme12/allowUnsafe
Code fix to add unsafe option to C# project
- Loading branch information
Showing
59 changed files
with
854 additions
and
83 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...ures/CSharpTest/Diagnostics/UpdateProjectToAllowUnsafe/UpdateProjectToAllowUnsafeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.UpdateProjectToAllowUnsafe; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.UpdateProjectToAllowUnsafe | ||
{ | ||
[Trait(Traits.Feature, Traits.Features.CodeActionsUpdateProjectToAllowUnsafe)] | ||
public class UpdateProjectToAllowUnsafeTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest | ||
{ | ||
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace) | ||
=> (null, new CSharpUpdateProjectToAllowUnsafeCodeFixProvider()); | ||
|
||
private async Task TestAllowUnsafeEnabledIfDisabledAsync(string initialMarkup) | ||
{ | ||
var parameters = new TestParameters(); | ||
using (var workspace = CreateWorkspaceFromOptions(initialMarkup, parameters)) | ||
{ | ||
var (_, action) = await GetCodeActionsAsync(workspace, parameters); | ||
var operations = await VerifyActionAndGetOperationsAsync(action, default); | ||
|
||
var (oldSolution, newSolution) = ApplyOperationsAndGetSolution(workspace, operations); | ||
Assert.True(((CSharpCompilationOptions)newSolution.Projects.Single().CompilationOptions).AllowUnsafe); | ||
} | ||
|
||
// no action offered if unsafe was already enabled | ||
await TestMissingAsync(initialMarkup, new TestParameters(compilationOptions: | ||
new CSharpCompilationOptions(outputKind: default, allowUnsafe: true))); | ||
} | ||
|
||
[Fact] | ||
public async Task OnUnsafeClass() | ||
{ | ||
await TestAllowUnsafeEnabledIfDisabledAsync( | ||
@" | ||
unsafe class [|C|] // The compiler reports this on the name, not the 'unsafe' keyword. | ||
{ | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task OnUnsafeMethod() | ||
{ | ||
await TestAllowUnsafeEnabledIfDisabledAsync( | ||
@" | ||
class C | ||
{ | ||
unsafe void [|M|]() | ||
{ | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task OnUnsafeLocalFunction() | ||
{ | ||
await TestAllowUnsafeEnabledIfDisabledAsync( | ||
@" | ||
class C | ||
{ | ||
void M() | ||
{ | ||
unsafe void [|F|]() | ||
{ | ||
} | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task OnUnsafeBlock() | ||
{ | ||
await TestAllowUnsafeEnabledIfDisabledAsync( | ||
@" | ||
class C | ||
{ | ||
void M() | ||
{ | ||
[|unsafe|] | ||
{ | ||
} | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task NotInsideUnsafeBlock() | ||
{ | ||
await TestMissingAsync( | ||
@" | ||
class C | ||
{ | ||
void M() | ||
{ | ||
unsafe | ||
{ | ||
[|int * p;|] | ||
} | ||
} | ||
}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...rp/Portable/UpdateProjectToAllowUnsafe/CSharpUpdateProjectToAllowUnsafeCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.UpgradeProject; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.UpdateProjectToAllowUnsafe | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
internal class CSharpUpdateProjectToAllowUnsafeCodeFixProvider : CodeFixProvider | ||
{ | ||
private const string CS0227 = nameof(CS0227); // error CS0227: Unsafe code may only appear if compiling with /unsafe | ||
|
||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = | ||
ImmutableArray.Create(CS0227); | ||
|
||
public override FixAllProvider GetFixAllProvider() | ||
{ | ||
// We're OK with users having to explicitly opt in each project. Unsafe code is itself an edge case and we don't | ||
// need to make it easier to convert to it on a larger scale. It's also unlikely that anyone will need this. | ||
return null; | ||
} | ||
|
||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
context.RegisterCodeFix(new ProjectOptionsChangeAction(CSharpFeaturesResources.Allow_unsafe_code_in_this_project, | ||
_ => Task.FromResult(AllowUnsafeOnProject(context.Document.Project))), context.Diagnostics); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private static Solution AllowUnsafeOnProject(Project project) | ||
{ | ||
var compilationOptions = (CSharpCompilationOptions)project.CompilationOptions; | ||
return project.Solution.WithProjectCompilationOptions(project.Id, compilationOptions.WithAllowUnsafe(true)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.