Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@CyrusNajmabadi
Copy link
Member

Fixes #48031.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 17, 2025 11:14
@CyrusNajmabadi CyrusNajmabadi changed the title Specialize 'fix all' for 'remove unnecessary null suppressions' Add analyzer/fixer to remove unnecessary 'unsafe' modifiers from members. Oct 17, 2025
@CyrusNajmabadi
Copy link
Member Author

@dibarbet ptal :)

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Net.Mime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this actually used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove.

foreach (var linkedSpans in linkedSpansArray)
{
if (!linkedSpans.Contains(expr.Span))
return WarningAnnotation.Create(CodeFixesResources.May_be_neccessary_in_other_project_this_document_is_linked_in);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

}
""",
BatchFixedCode = """
unsafe class C
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel extremely strongly about this - but it doesn't seem ideal that the batch fixer may remove a different unsafe as compared to the regular fixer for this scenario.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that's not abnormal. We have lots of features where we may report an individual info on many different nodes, but we can't actually remove all of them in batch fixing.

@CyrusNajmabadi CyrusNajmabadi enabled auto-merge (rebase) October 20, 2025 20:52
@CyrusNajmabadi CyrusNajmabadi merged commit 17c6281 into dotnet:main Oct 20, 2025
24 of 25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the removeUnsafe branch October 20, 2025 21:54
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 20, 2025
(original, current) => WithoutUnsafeModifier(current).WithAdditionalAnnotations(nodeToAnnotation[original])),
originalTree.Options);
var updateRoot = updatedTree.GetRoot(cancellationToken);
var updatedCompilation = semanticModel.Compilation
Copy link
Member

@Youssef1313 Youssef1313 Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it easier, possibly more efficient, to do this with TryGetSpeculativeSemanticModel?

i.e, get diagnostics of the original declaration. Skip analysis if there are any errors. Then get speculative semantic model with the unsafe keyword removed, and if it still doesn't have errors, then unsafe is unnecessary?

I haven't used speculative semantic model that much, so not sure if it's capable of doing the task here or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it easier, possibly more efficient, to do this with TryGetSpeculativeSemanticModel?

No. Speculative models don't let you change actual top level symbols (like types and members). They allow you to speculative about different executable code within existing symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzer suggestion: unnecessary use of unsafe

3 participants