<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// 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;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryDiscardDesignation
{
    [ExportCodeFixProvider(LanguageNames.CSharp,
        Name = nameof(RemoveUnnecessaryDiscardDesignationCodeFixProvider)), Shared]
    internal partial class RemoveUnnecessaryDiscardDesignationCodeFixProvider : SyntaxEditorBasedCodeFixProvider
    {
        [ImportingConstructor]
        [SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
        public RemoveUnnecessaryDiscardDesignationCodeFixProvider()
        {
        }

        public override ImmutableArray<string> FixableDiagnosticIds
            => ImmutableArray.Create(IDEDiagnosticIds.RemoveUnnecessaryDiscardDesignationDiagnosticId);

        internal sealed override CodeFixCategory CodeFixCategory => CodeFixCategory.CodeStyle;

        public override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            context.RegisterCodeFix(
                new MyCodeAction(c => FixAsync(context.Document, diagnostic, c)),
                diagnostic);

            return Task.CompletedTask;
        }

        protected override Task FixAllAsync(
            Document document, ImmutableArray<Diagnostic> diagnostics,
            SyntaxEditor editor, CancellationToken cancellationToken)
        {
            var generator = editor.Generator;

            foreach (var diagnostic in diagnostics)
            {
                var discard = diagnostic.Location.FindNode(getInnermostNodeForTie: true, cancellationToken);
                switch (discard.Parent)
                {
                    case DeclarationPatternSyntax declarationPattern:
                        if (declarationPattern.Parent is IsPatternExpressionSyntax isPattern)
                        {
                            editor.ReplaceNode(
                                isPattern,
                                (current, _) =>
                                {
                                    var currentIsPattern = (IsPatternExpressionSyntax)current;
                                    return SyntaxFactory.BinaryExpression(
                                        SyntaxKind.IsExpression,
                                        currentIsPattern.Expression,
                                        currentIsPattern.IsKeyword,
                                        ((DeclarationPatternSyntax)isPattern.Pattern).Type)
                                            .WithAdditionalAnnotations(Formatter.Annotation);
                                });
                        }
                        else
                        {
                            editor.ReplaceNode(
                                declarationPattern,
                                (current, _) =>
                                    SyntaxFactory.TypePattern(((DeclarationPatternSyntax)current).Type)
                                                 .WithAdditionalAnnotations(Formatter.Annotation));
                        }
                        break;
                    case RecursivePatternSyntax recursivePattern:
                        editor.ReplaceNode(
                            recursivePattern,
                            (current, _) =>
                                ((RecursivePatternSyntax)current).WithDesignation(null)
                                                                 .WithAdditionalAnnotations(Formatter.Annotation));
                        break;
                }
            }

            return Task.CompletedTask;
        }

        private class MyCodeAction : CustomCodeActions.DocumentChangeAction
        {
            public MyCodeAction(
                Func<CancellationToken, Task<Document>> createChangedDocument)
                : base(CSharpAnalyzersResources.Remove_unnessary_discard, createChangedDocument, CSharpAnalyzersResources.Remove_unnessary_discard)
            {
            }
        }
    }
}
<commit_msg>Make obsolete<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// 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;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryDiscardDesignation
{
    [ExportCodeFixProvider(LanguageNames.CSharp,
        Name = nameof(RemoveUnnecessaryDiscardDesignationCodeFixProvider)), Shared]
    internal partial class RemoveUnnecessaryDiscardDesignationCodeFixProvider : SyntaxEditorBasedCodeFixProvider
    {
        [ImportingConstructor]
        [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
        public RemoveUnnecessaryDiscardDesignationCodeFixProvider()
        {
        }

        public override ImmutableArray<string> FixableDiagnosticIds
            => ImmutableArray.Create(IDEDiagnosticIds.RemoveUnnecessaryDiscardDesignationDiagnosticId);

        internal sealed override CodeFixCategory CodeFixCategory => CodeFixCategory.CodeStyle;

        public override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();
            context.RegisterCodeFix(
                new MyCodeAction(c => FixAsync(context.Document, diagnostic, c)),
                diagnostic);

            return Task.CompletedTask;
        }

        protected override Task FixAllAsync(
            Document document, ImmutableArray<Diagnostic> diagnostics,
            SyntaxEditor editor, CancellationToken cancellationToken)
        {
            var generator = editor.Generator;

            foreach (var diagnostic in diagnostics)
            {
                var discard = diagnostic.Location.FindNode(getInnermostNodeForTie: true, cancellationToken);
                switch (discard.Parent)
                {
                    case DeclarationPatternSyntax declarationPattern:
                        if (declarationPattern.Parent is IsPatternExpressionSyntax isPattern)
                        {
                            editor.ReplaceNode(
                                isPattern,
                                (current, _) =>
                                {
                                    var currentIsPattern = (IsPatternExpressionSyntax)current;
                                    return SyntaxFactory.BinaryExpression(
                                        SyntaxKind.IsExpression,
                                        currentIsPattern.Expression,
                                        currentIsPattern.IsKeyword,
                                        ((DeclarationPatternSyntax)isPattern.Pattern).Type)
                                            .WithAdditionalAnnotations(Formatter.Annotation);
                                });
                        }
                        else
                        {
                            editor.ReplaceNode(
                                declarationPattern,
                                (current, _) =>
                                    SyntaxFactory.TypePattern(((DeclarationPatternSyntax)current).Type)
                                                 .WithAdditionalAnnotations(Formatter.Annotation));
                        }
                        break;
                    case RecursivePatternSyntax recursivePattern:
                        editor.ReplaceNode(
                            recursivePattern,
                            (current, _) =>
                                ((RecursivePatternSyntax)current).WithDesignation(null)
                                                                 .WithAdditionalAnnotations(Formatter.Annotation));
                        break;
                }
            }

            return Task.CompletedTask;
        }

        private class MyCodeAction : CustomCodeActions.DocumentChangeAction
        {
            public MyCodeAction(
                Func<CancellationToken, Task<Document>> createChangedDocument)
                : base(CSharpAnalyzersResources.Remove_unnessary_discard, createChangedDocument, CSharpAnalyzersResources.Remove_unnessary_discard)
            {
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.058031520718087884, 'fraction_numerical': 0.0009393591483143722, 'mean_word_length': 1.6698244636388966, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3639982', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1914, 'n_words': 386}